[UPDATE] Google Tag Manager Data Layer for WordPress is now available on GitHub
[UPDATE] You guys are too lazy and prefer Duracell Tomi’s plugin 🙂
This year again, I was invited to talk at Superweek, the premier Analytics/PPC conference in Eastern Europe.
I talked about tag management best practices and mentioned my Google Tag Manager data layer for WordPress implementation. In this post, I am sharing my setup with you.
Ready? Let’s modify your theme!
Setting up the data layer for WordPress themes
Note that the code below should go in your WordPress header, ideally in a child theme and before you insert your Google Tag Manager container snippet.
But my theme layout changes a lot, won’t I break my data layer?
That’s a great point. We’ve already mentioned we want to set up the data layer for WordPress in a child theme to minimize the risk of breaking things. This is especially true when a developer or designer handles your site and is not necessarily aware of your tracking requirements.
In this case, let’s mitigate risk by placing the code in your child theme’s functions.php script.
Create a PHP function that will generate the data layer for WordPress, with similar code from the above example:
Notice that the add_action call with trigger the function and insert its contents (the data layer for WordPress as well as the Google Tag Manager container snippet) within the HEAD tag, as recommended.
Setting up GTM to use the data layer for WordPress
Once that code is in place, you need to create macros in GTM to handle the datalayer variables we defined.
Now I need to update my GTM page tracking call, in this case with Universal Analytics, in which I’ve already defined custom variables.
And of course, these are custom dimensions I’ve already defined in Google Universal Analytics:
What will my reports look like?
Great, now how do I get the data out? Build a custom report, like so:
Which turns into the following drill-down report:
Then click on “post” to get author names.
Great, just me. Then again, I had it coming 🙂
Let’s click on:
And now we’re down to content categories. Our next click is going to take us to the page level (URL or title depending on your custom report setup.
OK that was way too much coding for me. Got anything simpler?
Not all of us are technically minded, but then again, maybe we should be if we work in digital marketing. If you don’t want to modify your WordPress blog because you’re not confident in your technical abilities, that’s fine too.
In that case, look no further and install my esteemed collegue Tomas Geiger’s (he goes by Duracell Tomi) Google Tag Manager for WordPress plugin. The wizard-like setup will guide through most of the steps I showed you in the above sections.
And there you have it, folks! With this, you can install your data layer for WordPress and get powerful hierarchical reports in Google Analytics.
Enjoyed this post? Let me know in the comments and don’t hesitate to share your results!
3 thoughts on “Google Tag Manager Data Layer for WordPress”
I tried your script and it worked for the most part, but I was not able to get “php the_author” to work. I also added “php the_date” as a variable but I wasn’t able to get that to work either. Any ideas?
I found that “php the_author” has to be called inside of the loop. The same goes for “php the_date.” I know it’s less than ideal, so I moved your code to the end of the single.php page and then loaded GTM in the footer. It seems to be working fine now.
Isn’t there a rule that will delay the loading of a tag until after the data layer has loaded? I seem to remember reading that on someone’s site once.
Great script Julien!
I saw also your presentation from Superweek (Yehoshua Koren uploaded it to dropbox) and its really really fascinating.