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.
This is extremely handy, because you no longer need to create Custom JavaScript variables whose sole purpose of existence it to change the output of other variables to lowercase, or to change undefined
values to fallback strings (e.g. (not set)
).
The Simmer Newsletter
Follow this link to subscribe to the Simmer Newsletter! Stay up-to-date with the latest content from Simo Ahava and the Simmer online course platform.
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
lowercase
orUPPERCASE
. -
Convert
null
to… - With this, you can convertnull
values to some other string (by typing text directly into the field), or you can havenull
output fall back to another variable by picking it from the variable selector. -
Convert
undefined
to… - Same as withnull
, you can now haveundefined
values fall back to strings or the returned value of other variables. -
Convert
true
to… - Same as above, except now you can convert Booleantrue
to a string or the returned value of some other variable. -
Convert
false
to… - Same as above, except now you can convert Booleanfalse
to 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 undefined
, null
and false
to string representations is handy, too, since you can now use something like "N/A"
or "(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.