Cast your GTM variables to another type with this new GTM Tag Template

Cast your GTM variables to another type with this new GTM Tag Template

At DPG Media we work with Snowplow and are in the middle of converting our Custom HTML tags to Templates. For this we use the official Snowplow GTM Template.

(As soon as it's published I'll post the link here)

We use this to send our pageviews and unstructured events to Snowplow. It works amazingly well, and we're happy that Snowplow created such a easy to use template.

However, there was one issue we bumped into. We have a custom selfdescribing event we use for SearchEvents (instead of the built-in search event by snowplow). This event accepts a parameter called terms, which is a single-element array. So if you search for "gtm template", the terms should be

["gtm templates"],

not ["gtm", "templates"].

In the official template, you can see at this line that when you use the built-in trackSiteSearch event, terms will be split into an array if it comes in as a string. But for the self describing events there's no such logic.

Terms are split into an array in the built-in trackSiteSearch event

Introducing the Type Caster

That's why we created the Type Caster variable template.

You enter an an input variable and select to what it should be cast. The options are string, boolean, integer, array and a single element array.

This is what it looks like in the GTM preview mode:

The rows with the black dots in front are the variables as if they would come from the dataLayer or from the HTML, as a string.

In the Blue example you can see that there are 2 ways to cast to an array: a normal split of the words in a string or a single element array.

In the Red, Green, Orange, and Purple example you see that there are several ways to cast to a boolean. If the initial value is "true", "1", "aye", "treu", it will be cast to true, otherwise to false

You'll also see in the Green example that strings of numbers "1", will be cast to a number 1.

So that's it. I hope the Variable Caster can be of help to you. If you have feature requests, feel free to open an issue on Github or create a PR.

Source code can be found on Github:

Cover photo credit: