Google Tag Manager Tracking Guide for WordPress

This guide shows you how to track:

  • External Link Clicks
  • Internal Link Clicks
  • Affiliate Link Clicks
  • Anchor Link Clicks
  • 404 Errors
  • Comment Form Submissions
  • Contact Form 7 Submissions
  • Subscriber Opt-Ins
  • Social Follows
  • Media Shares
  • Social Shares

How to setup Google Tag Manager Tracking in WordPress

  1. Create Google Account or Login to your existing Google Account
  2. Sign into Google Tag Manager
  3. Create a new Google Tag Manager Account (e.g. Penguin Initiatives)
  4. Google Tag Manager Account Setup Screen

  5. Create a new Google Tag Manager Container for your website (e.g. penguininitiatives.com)
  6. Setup Container Google Tag Manager

  7. Copy your new Google Tag Manager Container’s GTM ID (e.g. GTM-P3XHZF):
  8. Copy New Google Tag Manager Container's GTM ID

  9. Login to your WordPress website’s Admin Dashboard and go to Plugins > Add New
  10. Search for “DuracellTomi”, click the “Install Now” button for “DuracellTomi’s Google Tag Manager for WordPress” and then activate the plugin
  11. Go to Settings > Google Tag Manager
  12. Paste in your Google Tag Manager ID and click the “Save Changes” button
  13. DuracellTomi's Google Tag Manager for WordPress General Settings Screen

    Add Google’s Tag Manager Container Code Without a Plugin

    If you don’t want to use a plugin to add your Google Tag Manager code to your WordPress website, then you can of course alternatively copy and paste the code snippet right below your opening body tag in your WordPress theme’s header.php file if you know how to do so. Note that if you do this instead you won’t get the WordPress dataLayer variables that DuracellTomi’s Google Tag Manager for WordPress plugin creates

  14. Right click and save this Google Tag Manager Container Template for WordPress
  15. In your Google Tag Manager account click on the “Admin” section
  16. Select your new Google Tag Manager Account and Container
  17. Click on “Import Container”
  18. Import Container Google Tag Manager

  19. Then select the Google Tag Manager Container Template for WordPress JSON file you downloaded and import it
  20. Now replace “yourdomain.com” with your website’s domain in the following Triggers: Anchor Link Click, External Link Click and Internal Link Click (you may have to refresh your container to see the newly imported Tags, Triggers and Variables)
  21. Next replace “affiliatelinkpattern” with whatever URL pattern is consistent for your affiliate links, if your site doesn’t have affiliate links or they don’t use a consistent URL pattern you can skip this step
  22. Finally replace the “Tracking ID” Variable with your website’s Google Universal Analytics Tracking ID (e.g. UA-61808155-1)
  23. Save all your changes and Publish your Container
  24. Login to your WordPress website’s Admin Dashboard and go to Plugins > Add New
  25. Search for “WP Ajaxify Comments”, click the “Install Now” button for “WP Ajaxify Comments” and then activate the plugin
  26. Go to Settings > WP Ajaxify Comments
  27. Check the checkbox next to “Enable plugin”
  28. Scroll down to the ‘OnAfterUpdateComments’ callback field”, then copy & paste in the following JavaScript code:
  29. dataLayer.push({'event' : 'newComment'});

    WP Ajaxify Comments newComment dataLayer event callback

  30. Scroll to the bottom and click the “Save Changes” button
  31. If you would like to track Contact Form Submissions, simply use the Contact Form 7 WordPress Plugin to power your contact form
  32. If you use Elegant Theme’s Bloom Email Opt-In WordPress Plugin and want to track new Opt-Ins you’ll need to do the following:
  33. Download Bloom’s custom.js file from this location: /wp-content/plugins/bloom/js/custom.js and open the file in a text editor
  34. Search for the following code (on line 405 in version 1.0.3):
  35. set_cookie( 365, 'et_bloom_subscribed_to_' + optin_id + list_id + '=true' );
  36. Hit return after this line of code to create an empty line below it, then copy and paste the following code:
  37. dataLayer.push({'event' : 'newSubscriber', bloomOptin : this_button.data( 'optin_id' ) });
  38. Now save your modified custom.js file and overwrite it via FTP
  39. If you use Elegant Theme’s Monarch Social Sharing WordPress Plugin and want to track social follows, media shares and social shares you’ll need to do the following:
  40. Download Monarch’s custom.js file from this location: /wp-content/plugins/monarch/js/custom.js and open the file in a text editor
  41. Search for the following code (on line 11 in version 1.2.2):
  42. share_link = 'media' == social_type ? $this_el.data( 'social_link' ) : $this_el.prop( 'href' );
  43. Hit return after this line of code to create an empty line below it, then copy and paste the following code:
  44. ( media_url ) ? dataLayer.push({'event' : 'mediaShare', shareNetwork : $this_el.data( 'social_name' ), shareMedia : 'media' == social_type ? $this_el.closest( '.et_social_media_wrapper' ).find( 'img' ).attr( 'src' ) : '' }) : dataLayer.push({'event' : 'socialShare', shareNetwork : $this_el.data( 'social_name' )});
  45. Search for the following code (on line 35 in version 1.2.2):
  46. post_id = $this_el.data( 'post_id' );
  47. Hit return after this line of code to create an empty line below it, then copy and paste the following code:
  48. dataLayer.push({'event' : 'socialFollow', followNetwork : $this_el.data( 'social_name' )});
  49. Now save your modified custom.js file and overwrite it via FTP
  50. Finally clear your cache and purge your CDN if needed, if you followed this guide correctly you should see the following in your Google Analytics > Real-Time > Events Report:
  51. Google Analytics Google Tag Manager for WordPress Real-Time Events

  52. For slicing and dicing this data further I’ve also created this Google Analytics Advanced Segments Template you can import into your Google Analytics account

Additional Google Tag Manager Resources

Want to track something else?

If you want to track something not covered here and need help getting it to work, please post a request in the comments and I’ll do my best to help you out.

13 COMMENTS

  1. Jignesh,

    That’s great to hear!

    Did you have any trouble implementing anything and/or is there anything else you wanted to track that I could help with?

  2. Awesome tutorial!

    I was wondering what do these do across the json file?

    “accountId”: “71556451”,
    “containerId”: “1193060”,

  3. This is a useful find, thank you.
    What does the plug-in, “WP Ajaxify Comments” do? Do we need to add/us this?

    I’m thinking I can skip this step on down if we’re looking to start out with tracking External/Internal/Anchor links and 404. Correct?

    TIA,
    Erica

    • You’re welcome!

      That plugin allows you to track only comment submissions that pass validation as events and prevent comment submissions that fail validation from being logged as comment submission events. That way you only get comment submissions logged as events when comments were truly processed and submitted to your WordPress website, making the accuracy of your event tracking for comments significantly better. I hope that makes sense.

      Yes you can skip that step if you don’t care about less accurate comment submission tracking. If you skip that step you will still track a comment submission as a custom event, it will just be every time someone clicks the comment button regardless of whether or not they see an error afterwards.

      I like that plugin more so because it provides a better user experience for visitors and increases the likelihood that visitors will ultimately leave a comment. AJAX validation on any form is almost always preferable to the alternative.

      Cheers,
      Andy

  4. Ohh thank you so so much this is just what I was looking for 😀

    I do have a question – I would like to gather author information to be able to let’s say track how many page views each author gets. Do you know how I could acheive this?

    Again thanks again.

  5. Hello, thank you very much for your work and your explanations! I have several CTA forms with contact form 7 on the site but I don’t know which one is activated. I can see the events in real time but how do I get my form id back when someone clicks on the submit button? Thank you in advance !

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.