Skip to content

Instantly share code, notes, and snippets.

@dotherightthing
Last active September 12, 2019 02:01
Show Gist options
  • Save dotherightthing/9451447f5e5049b42aef78e70d4f136a to your computer and use it in GitHub Desktop.
Save dotherightthing/9451447f5e5049b42aef78e70d4f136a to your computer and use it in GitHub Desktop.
[Google Tag Manager (GTM)] Notes from Practical Google Analytics and Google Tag Manager for Developers. #gtm #seo #wordpress #analytics #adwords

Google Tag Manager (GTM)

Notes from LunaMetrics' "Practical Google Analytics and Google Tag Manager for Developers".

The Google Tag Manager (GTM) manages tracking scripts from both Google and Third-Party providers:

  • Google Analytics
  • Adwords
  • DoubleClick
  • etc

There are other tag managers around, but GTM is free.

GTM has two parts:

  1. A web based interface for Managing GTM
  2. A two part container script that loads the tags, in the <head> (asynchronous loading of JavaScript tags) and at the start of the <body> (<noscript> loading of supported tags). These are customised with the GTM container ID GTM-XXXXXX

Help information is available at https://developers.google.com/tag-manager/.

Managing GTM

Google Tag Manager

https://tagmanager.google.com

  • set up tracking tags
  • create containers containing collections of tags
  • use permissions to control what gets published

Set up Google Analytics

  1. Choose a container, e.g. dontbelievethehype.co.nz
  2. New Tag > Add a new tag > Edit
  3. Choose Google Analytics
  4. Track Type: Page view
  5. Trigger: Page View
  6. Name the tag Pageview
  7. Save

Then:

  • Create a variable: Google Analytics
  • Enter GA ID (see Get a tracking ID, below)
  • Save
  1. Back on the dashboard, the Workspace Changes show that PageView and Google Analytics have been added.
  2. Click Submit (top right of UI) to version and release the changes

Google Analytics

https://analytics.google.com/

Get a tracking ID

  • Cog/Admin > Create account

Notes:

Google Adwords

https://adwords.google.com

WordPress integration

Issues

At first glance, integration is not straightforward. WordPress allows developers to inject (enqueue) inline scripts into the <head>, but only at the bottom/end of the <body> - not at the top/start of it.

The Affectivia Hack achieves this by injecting a closing tag into the WP body_class filter. But it's a bit presumptuous and could break some themes.

DuracellTomi's Google Tag Manager for WordPress

DuracellTomi's Google Tag Manager for WordPress doesn't follow the WordPress way of adding scripts.

For the <head> portion, it hooks into the wp_head action to print a block of HTML (which contains the GTM script). See frontend.php.

For the <body> portion, it implements the Affectivia Hack as an experimental option, falling back to outputting the <noscript> part at the bottom of the <body>. The Affectivia Hack allows the plugin to inject the necessary code without requiring manual template integration.

DuracellTomi's Google Tag Manager does also expose a lot of WordPress data to GTM, however this comes at the cost of learning how to implement this manually, and why.

The WordPress Way

Google's instructions often conflict with the WordPress way of doing things. But if are to be WordPress developers, we must evolve the WordPress way, rather than madly hacking our way around it. Hacks create code debt which breaks interoperability between plugins and themes.

For the <head> portion, register an empty placeholder script, then load GTM as an inline script, optionally passing the GTM container ID as a localized variable.

For this to work, existing script loading actions must be left intact for wp_head, which means resolving render blocking on a script-by-script basis rather than by brute force

For the <body> portion, include the noscript block as a template-part.

Use GTM to track clicks on an element

From Google Tag Manager Button Click Tracking (2018 version) for Google Analytics, Facebook and AdWords

  1. GTM listens for a trigger type, e.g. Click
  2. Pinpoint the element you wish to track clicks on, by using Preview and inspecting the trigger Variable
  3. Focus the trigger type to the element by refining the trigger type to use the element's most unique attribute
  4. Connect the refined trigger type to a tag which fires an event in GA
  5. Check that the click is being tracked in GA, by using Preview and inspecting the trigger Variable
  6. Verify by using the Google Tag Assistant browser extension > Google Analytics > Events

A) Figure out what to track

  1. Go to https://tagmanager.google.com
  2. Select a property
  3. Select Triggers
  4. Create a new trigger, eg Click - Generic
  5. Click on the new trigger, click the Pencil (Edit) icon
  6. Trigger type: Click > All Elements, All Clicks > Save
  7. Variables (left nav) > Configure Built-In Variables > Check all the Click checkboxes
  8. Click Preview
  9. In a new tab, load your site and navigate to the page you want to test
  10. A Tag Manager toolbar will appear above Chrome Dev Tools
  11. In GTM, click Refresh
  12. Click on elements to test the listener
  13. Events will be logged to the Tag Manager toolbar's Summary pane
  14. To inspect an event, click on Variables, then click on an event
  15. Identify the unique feature of the element you want to track clicks on, e.g. Click Classes or Click ID

B) Refine the click trigger

  1. Select Triggers
  2. Click on the trigger, click the Pencil (Edit) icon
  3. Rename the trigger to reflect the element that should listen for clicks, e.g. Click - Gallery Thumbnail
  4. Trigger type: Click > All Elements, Some Clicks > Specify unique identifier > Save

It's also possible to use data- attributes as listeners, see Track More Click Detail With Data Attributes And Google Tag Manager.

C) Connect the refined click trigger to a tag

  1. Click on Tags (sidebar) > New
  2. Name the event, e.g. GA - Event - DTRT Gallery - Send Image to Viewer
  3. Click the Pencil (Edit) icon > select Universal Analytics (GA, if appropriate)
  4. Track Type > Event
  5. Category > Click
  6. Action > DTRT Gallery - Send Image to Viewer
  7. Google Analytics settings > {{Google Analytics}}
  8. Triggering > click the Pencil (Edit) icon > select Click - Gallery Thumbnail > Save
  9. Click Refresh
  10. Switch to the tab containing your webpage and reload it
  11. Click on the tracked element

D) Check that the click is being tracked

  1. https://analytics.google.com/ > Select the appropriate account
  2. Select Real Time (on the left) > Events
  3. Over time these events will be viewable under Behaviour > Events > Overview

E) Save the new configuration

  1. Switch back to the GTM console
  2. Submit > Version Name DTRT Gallery - thumbnail click tracking for GA > Publish
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment