Elizabeth R
Elizabeth R

Reputation: 1

How to convert a Data Layer Variable from number to string in Google Tag Manager

I am implementing a custom HTML tag in GTM that should return certain values in the data layer. However, I’d like to encode one of the values as text.

I have a customer status variable that keeps track of how many orders a user has placed. I’d like to set up a JavaScript function that assigns a value of ‘New Customer’ when the customer status count = 0, and ‘Return Customer’ if the count is > 0.

Here is the code I have so far:

var returnCustomer = {{Shopify - Transaction - CustomerStatus}};

var returnStatus = returnCustomer(function(){
  if (returnCustomer > 0) {
    return 'Return Customer'
  } else {
    return 'New Customer'
  }
});

I know this isn’t quite right-- anyone have ideas on how to solve this problem?

Upvotes: 0

Views: 2787

Answers (1)

Eike Pierstorff
Eike Pierstorff

Reputation: 32760

I won't comment in particular on your code (you call returnCustomer as a function - are you really sure {{Shopify - Transaction - CustomerStatus}} returns a function? Also your if clause does the opposite of what you describe in the text) because GTM has a built-in way to solve this without hand written code.

It's called a lookup table, and it allows you to return an output value depending on the value of an input variable.

Your requirement was:

I’d like to set up a JavaScript function that assigns a value of ‘New Customer’ when the customer status count = 0, and ‘Return Customer’ if the count is > 0.

So you have an input variable that's either 0 or 1, and you want to return a text output based on those values.

Go to Variables, New, and from the utilities sections chose "lookup table". I have named my Variable "My Output", you will probably use a better name.

Choose your input variable - I presume that's "{{Shopify - Transaction - CustomerStatus}}" in your case, I have named mine "My Input" for demonstration purposes.

Now click the "add row" buttons to add two rows. Each row will have two input fields.

Into the left hand side go the expected values from the input. Right hand side maps an output value to the input value.

Lookup Table Configuration

Now, if "{{My Input}}" has a value of "0", "{{My Output}}" will assume a value of "New Customer". If it's "1", "{{My Output}}" will assume a value of "Return Customer". If it's neither, "{{My Output}}" will be undefined (you can set a default value, though).

Upvotes: 1

Related Questions