A very recent addition to Google Tag Manager is the Format Value option in all of GTM’s variables. With Format Value, you can modify the output of the variable with a number of pre-defined transformations.
undefined values to fallback strings (e.g.
Tip 88: Format Value option in GTM’s Variables
When you create or modify any variable in GTM, you now see a new option at the bottom of the configuration screen. The option is titled Format Value. By expanding it, you will see the following options:
Change Case to… - This lets you change the case of the string output of the variable to either
nullto… - With this, you can convert
nullvalues to some other string (by typing text directly into the field), or you can have
nulloutput fall back to another variable by picking it from the variable selector.
undefinedto… - Same as with
null, you can now have
undefinedvalues fall back to strings or the returned value of other variables.
trueto… - Same as above, except now you can convert Boolean
trueto a string or the returned value of some other variable.
falseto… - Same as above, except now you can convert Boolean
falseto a string or the returned value of some other variable.
So how is this useful? Well, take the Case conversion as an example. By forcing the variable output to lowercase, for example, you can normalize all the following strings:
"I love GTM",
"i LoVe gTM",
"I LOVE GTM".
With Case conversion set to Lowercase, the output of all the three strings above would be
"i love gtm".
Normalization is absolutely vital in tools such as Google Analytics, which treat each case format of any string as unique and distinct from the others.
Being able to convert falsy values such as
false to string representations is handy, too, since you can now use something like
"(not set)" to represent situations where the variable did not return a proper value. Again, in Google Analytics this is relevant, because GA drops
undefined fields from hits.
The Format Value option is a very useful addition to Google Tag Manager’s arsenal. I only hope the number of available formatting options is increased in the future, perhaps even allowing us to write simple anonymous functions directly in the UI through which each varible output is passed.