Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Google Tag Manager interpret a variable with string value 'false' as a boolean false?

I set up a new data layer variable in Tag Manager that will represent whether an event is a non-interaction hit. This is supposed to be a boolean true/false value. I set the default value of false (without quotes).

I noticed when I explicitly include this variable as a boolean in my events, eg:

dataLayer.push({'event': 'gaEvent', ..., 'nonInteractionHit': false});

Tag Manager's preview mode shows in the event properties Non-Interaction Hit: false as a boolean (without quotes). This is expected.

However, when I don't specify a value for nonInteractionHit, eg:

dataLayer.push({'event': 'gaEvent', ...});

The preview mode shows the event fired with Non-Interaction Hit: 'false' as as a string value (with quotes). Basically, it seems to have interpreted my default value as a string instead of a boolean. This is concerning, because JavaScript is awesome and the string 'false' is actually truthy.

So my question is: Does GTM consider string representations of 'false' or 'true' as proper booleans, or just strings, specifically when used in the context of a property that is supposed to be a boolean? Does 'false' actually evaluate to false as I need it, in my case?

Extra credit: is there a way to specify a variable's default value as a proper boolean? The UI doesn't seem to allow it - you just get a textbox to enter a value which apparently is interpreted as a string.

like image 371
Kurt Schindler Avatar asked Sep 11 '25 21:09

Kurt Schindler


1 Answers

I believe I've found the general answer to my question, but I'll leave this open if anyone wants to provide a better answer.

I found my answer here: https://www.simoahava.com/analytics/fun-google-tag-manager-part-2/#true-and-false

I'm not certain exactly how 'false' is interpreted in my situation setting Non-Interaction Hit, but it does seem certain GTM variables do require a boolean type, and entering true or false in a field (without quotes) is interpreted as a string, and may not work as expected. And unfortunately you can't specify boolean true/false in GTM text fields.

Simo's elegant workaround is to instead also define {{true}} and {{false}} variables based on a Javascript snippet, which is written as function(){return true;} and function(){return false;}, respectively. Then, in my case I can set the default value to the variable {{false}}. I've verified that in preview mode, this shows up as a proper, un-quoted boolean value.

Further proof: When I had the default value entered as the text false entered in the text box, and then set a formatting override for "Convert false to..." and specified some arbitrary new value. When I fired the event, it was not overridden. It showed the value as 'false'. Hence, GTM's own override system didn't interpret the text false as a boolean. It also didn't interpret it as true, either...

Take away: use your own {{true}} and {{false}} variables to be sure you are assigning a proper boolean.

like image 199
Kurt Schindler Avatar answered Sep 14 '25 11:09

Kurt Schindler