I'm going to share some of the tips and tricks I've learnt (and that I use on my daily basics work) in the almost past 3 years since Google Tag Manager was released.
Tip #1 - Grabbing a dataLayer value from the console
If we want to grab the current value in the Google Tag Manager's dataLayer, it offers a built-in get method for it. When GTM loads a global object is created named google_tag_manager, that has a key that is the current GTM Id value.
And it the we can get a dataLayer value using dataLayer.get('value')
This will return the current value for the 'event' key, at the time we execute it. As all sites have a different GTM Id string, we could use it this way to it works on any site, as the Id will be grabbed dinamically:
Tip #2 - Using console.table for a fancy view of your objects
Most common use of the "console" command is to print values into your console tab, but we can do some other nice things, like easily printing a full object in a table using the "console.table" command.
For example for printing the whole dataLayer into a fancy table like this:
But this has became a must for me, when trying to debug my Enhanced Ecommerce implementations, for example for the products impressions, cart content, or for the transactions. We could view with a quick look if all our data is there, and in the format we really want. It's so easy to detect any misspelling on the key's, or some wrong typed key in some of the pushes, or whatever.
Combining it with the previous tip is even more helpful. Here is an example for checking the sent product items within an standard ecommerce push for Google Tag Manager:
Or here is goes an example to see all the product impression details sent within a category page using Enhanced Ecommerce:
isn't it nice, clear and helpful?
Tip #3 - Printing the dataLayer in a plain text format
Any browsers allows you to print your dataLayer just typing it into the console, but when you have a large pushes it's a bit difficult to search for the data, or if we have a lot of pushes you may end going crazy trying to look for something.
All browsers have the JSON object that can easy and help us debugging and checking our dataLayer. We can get a plain text version of our dataLayer using the following command:
Today by a lucky change, I've found that Chrome DevTools already has an in-built code beautifier feature!.
— David Vallejo (@thyng) August 12, 2015
Do you have any other trick/tip that makes your daily work easier?, Please share them.
Did you find those tips are useful?, Let me know about it.