Creating Variables
Often, when we activate states based on datalayer events, we also want to read data from the same source (window.dataLayer
and its object pushed with that event). We need to create a JTM variable that reads those values.

Choose “GTM data layer value” on the next page as the variable template type.

The value to be read from the dataLayer in our example is the parameter key “event_category”:

This will retrieve the value of “event_category” that was pushed to the dataLayer object accordingly in this example:
window.dataLayer.push({
event:"gtm.init",
event_category:"category_value"
});
This is just for the visual example. Our JTM variable would report the value from this according dataLayer.push (“category_value”).
Watch out! This configuration might be ambiguous, though.
This is where the “Occurrence pointer” comes into play. The options here are:
first
last
state context
Meaning that in an array (dataLayer) a property might be named multiple times. The event
property, for example, appears with each push in that array. So it is ambiguous. You can select “last” and refer to the most recent event (latest push, highest index in that array), however, this might not work in all situations.
Hence, the “event_object” is submitted to the state in the code above. This way, you can point exactly to the property of the event object. Specifically with race conditions, this option might be a good idea.
Read more on this topic of contextual states here.
Last updated
Was this helpful?