Data Layer Picker - Custom Variable Template

The Data Layer Picker custom variable template is a variable template for Google Tag Manager’s community template gallery.

Resource
Blog post
Gallery entry
GitHub repo

Description

The Data Layer Picker variable allows you to access keys and values in the dataLayer.push() that caused the tag to fire for which the variable is evaluated.

This is different to how the Data Layer Variable works, as the DLV doesn’t care (or remember) in what push() the key was added to GTM. Instead, it merges everything together into its data model.

With Data Layer Picker, however, you can access the exact contents of the object that was pushed into dataLayer.

There are many use cases for this, such as the need to access a specific state of the dataLayer message prior to it being merged with the other keys in GTM’s data model.

To use the variable, you have two options. Either you retrieve the Entire object that was pushed into dataLayer, or you fetch the value for a Specific key. You can use dot notation when specifying the property name.

The template will first try to get the nested structure at the end of the dot notation path (e.g. gtm.elementId will look for the value at the end of {gtm: {elementId: 'somevalue'}}). If it doesn’t match anything, it will then try to get the value as if the dot notation path were an entire key name in itself (e.g. gtm.elementId will look for the value at the end of {'gtm.elementId': 'somevalue'}). If no match is made, then undefined is returned.

The template also works with objects pushed to dataLayer by other custom templates.

Release notes

Date Changeset
26 May 2021 Fix issue with undefined objects breaking the variable.
26 February 2021 Initial release.