Google Tag Manager Data Layer for WordPress

[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.

<!– 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 = [];
dataLayer.push({
// 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!
</script>

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!

 

Google Tag Manager Data Layer for WordPress was last modified: October 31st, 2016 by Julien Coquet

Julien Coquet

Expert de la mesure d’audience sur Internet depuis plus de 15 ans, Julien Coquet est consultant senior digital analytics et responsable produit et évangélisation pour Hub’Scan, une solution d’assurance qualité du marquage analytics.

A propos de Julien Coquet

Loading Facebook Comments ...
Loading Disqus Comments ...

3 thoughts on “Google Tag Manager Data Layer for WordPress

  1. Chip Oglesby

    Julien,

    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?

  2. Chip Oglesby

    Julien,

    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.

Comments are closed.

No Trackbacks.