[UPDATE] Google Tag Manager Data Layer for WordPress is now available on GitHub

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 for WordPress implementation. In this post, I am sharing my setup with you.

Ready? Let’s modify your theme!

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.

[code language=”htmlscript”]<!– Google Tag Manager Data Layer –>

<script type="text/javascript">
// URL toolbox – helps grabbing elements in the URL
var _d = document;
var _dl = _d.location;
var _dlp = _dl.pathname;
var _dls = _dl.search;
var _dr = _d.referrer;

// Initialize your data layer and start pushing variables from custom WordPress PHP data layer
dataLayer = [];
// add your Google Analytics property ID here if you haven’t defined it as a constant macro in GTM
‘GTM_WP_UA’: ‘UA-1234567-1’,
<?php if (is_404()){ // 404 pages, handled with a /404/ prefix as well as the referrer ?>
‘GTM_WP_404’: ‘<?php print is_404(); ?>’,
‘GTM_WP_404_URL’: ‘/404’ + _dlp + ‘/’+ _dr,
<?php } ?>
<?php if(is_home()){ // Home page is tagged manually here but can be done directly in GTM ?>
‘GTM_WP_post_type’: ‘home’,
‘GTM_WP_Category’: ‘Home’,
<?php } ?>

<?php if (is_single()||is_page()){ /* Content pages: either a post or a page *  Query the WP API to retrieve post/page type, author, number of comments, tag, or even custom variables */ $gtm_cat = get_the_category(); // post/page tags being passed as one big string separated by spaces $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { $gtm_tags .= $tag->name . ‘ ‘;

// Now we populate the Javascript data layer with our PHP variables
‘GTM_WP_authorname’: ‘<?php the_author(); ?>’,
‘GTM_WP_post_type’: ‘<?php print get_post_type(); ?>’,
‘GTM_WP_Number_Comments’: ‘<?php print get_comments_number(); ?>’,
‘GTM_WP_Category’: ‘<?php print $gtm_cat[0]->cat_name; ?>’,
‘GTM_WP_Tags’: ‘<?php print trim($gtm_tags); ?>’,
<?php } // Done with WordPress page type/conditions, you can add more default dataLayer variables below ?>
‘GTM_WP_dummy’: ”
// Don’t forget to terminate your data layer correctly!

Fairly basic PHP and Javascript code all in all. Once that code is in place, you need to create macros in GTM to handle the datalayer variables we defined.

gtm for wordpress macros

Now I need to update my GTM page tracking call, in this case with Universal Analytics, in which I’ve already defined custom variables.

gtm for wordpress tag

And of course, these are custom dimensions I’ve already defined in Google Universal Analytics:

gtm for wordpress custom varsGreat, now how do I get the data out? Build a custom report, like so:

gtm for wordpress custom report google universal analytics


Which turns into the following drill-down report:

gtm for wordpress custom report step 1

Then click on “post” to get author names.

gtm for wordpress custom report step 2

Great, just me. Then again, I had it coming 🙂
Let’s click on:

gtm for wordpress custom report step 3


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.

gtm for wordpress custom report step 4

And there you have it, folks! Powerful hierarchical reports based on your WordPress data.

Enjoyed this post? Let me know in the comments and don’t hesitate to share your results!