Search code examples
google-tag-manager

If condition in tag manager - to change the value of a variable


In tag manager, I'm looking to use some kind of IF condition to vary the value of a variable based on the input.

So for example, in my form, a user could submit a lead which is worth £100. Anything under this we class as a bad lead and I'd like to set the value as £1 in the variable.

In my head it is;

If {{eventValue}} > £100 - variable value = £100 (or the actual form submission amount collected dynamically)

If {{eventValue}} < £100 - variable value £1 (static number)

I'm a bit lost in not sure if I need to use a regex table or if there is an easier way to do this?

Update: After doing some searching, I realised I probably needed to use JS.

I've came up with:

function() {
    var eventValue = {{eventValue}};
    if (eventValue > 9999) {
        return eventValue;
    } else {
        return 1;
    }
}

I'll test to see if this works, but would be interested to know if there is a better solution.


Solution

  • function() {
        var eventValue = {{eventValue}};
        // add this line to make sure it can be parse as number
        eventValue = parseInt(eventValue) || 0;
        if (eventValue > 9999) {
            return eventValue;
        } else {
            return 1;
        }
    }
    

    It can do what you need at this case.

    But it might be a better idea to return good and bad and remain the original value for future analysis.