One of the more difficult concepts of Google Tag Manager is data model. Basically, the data model is what Google Tag Manager uses to populate the data layer variable. You might be tempted to think it’s the same thing as
dataLayer group, but it is not.
A data model is a representation of your keys and values Pay to me
dataLayer. Whenever you press any key
dataLayer, GTM takes this key and updates the corresponding key in its data model with the new value, or in the case of objects and arrays, it merges the old and new value together.
In this #GTMTips article, I am totally indebted to Mr. Jethro Nederhof from Snowflake Analytics. He posted these two tricks on Measure Slack, and I asked if it was OK to post them on my blog. He agreed, which is no surprise, because his generosity also produced our co-authored article on tracking browsing behavior a few months ago.
Simmer . Newsletter
Subscribe to the Simmer newsletter to get the latest news and content from Simo Ahava right in your inbox!
Tip 85: Two simple data model tricks
Both texts relate to how the data model handles the keys and values pushed into it.
The first trick lets you prevent Recursive merge from happening (useful in single-page applications where you don’t want pushed values to persist on previous pages, for example).
The second trick allows you to access the current state of the data model. This can be useful for a number of things, but in particular if you’re debugging a setup, it can be helpful to see all the keys and values currently in the data model table.
Trick 1: Use
_clear: true To prevent keys from being merged into the object
Let’s say you push some product impressions into a file
dataLayer. The first batch looks like this:
window.dataLayer = window.dataLayer || ; window.dataLayer.push( products: [ sku: '123', name: 'Thick as a Brick' , sku: '234', name: 'Aqualung' ] );
At this point, the key
products In the data model it contains two objects that represent the products that have been paid. If you want to create a data layer variable for
products, that’s what you’ll also get as the return value of the variable.
Next, let’s say the user goes to another section of the site without loading a page (so it’s a one-page app), and on that page there is only one product appearance, so the site runs the following code:
window.dataLayer = window.dataLayer || ; window.dataLayer.push( products: [ sku: '345', name: 'Close to the Edge' ] );
You would expect the data model to now contain only one product in
products group, but because of how recursive merge It works, the data model looks like this:
products: [ sku: '345', name: 'Close to the Edge' , sku: '234', name: 'Aqualung' ]
As you can see, the second product continues for the first batch, because the new product is simply combined with the old one
products matrix array.
and here information. To prevent this recursive merge from happening, you need to press the key
_clear with value
true At the same time as you press the keys don’t They want to integrate with their peers. So the second batch becomes:
window.dataLayer = window.dataLayer || ; window.dataLayer.push( products: [ sku: '345', name: 'Close to the Edge' ], _clear: true );
So far the key is in
products An array of a data model that has only one object inside it.
Trick 2: Get the object representation of the current state of the data model
You can always use files
window.google_tag_manager['GTM-XXXXX'].dataLayer.get(key) To fetch the given current value
key from the data model. But if you want to get full The contents of the current data model, you need to run the following command:
var dataModel = window.google_tag_manager['GTM-XXXXX'].dataLayer.get( split: function() return ; ); console.table(dataModel);
What a simple way to find out the current situation.
I’ll be the first to admit: These are very technical tricks. You probably won’t need it daily, or even monthly, in your implementations.
However, I firmly believe that understanding how the data model works is one of the keys to unlocking the true power of Google Tag Manager.
And again, a leaked Thanks to Jethro Nederhof for revealing these simple tricks. He received full credit for the content of this article.