One of the advantages for businesses operating in the digital space is the sheer amount of data available, from search data to help you identify the types of keywords your target market is using or the topics and news your users are sharing on social media.
While you can collect this type of data from third parties, there is also a wealth of data that you can collect yourselves.
For many years now, Google Analytics (GA) has been the standard platform for on-site data collection and tracking. By default, it collects a significant amount of information, such as where visitors come from, the countries they are in, how long they spend on a web page, etc. However, if you want to get more specific information, such as individual links users are clicking or how long they view a video, then this would often require the assistance of a developer to add some JavaScript to the page. This would often slow down or completely prevent the tracking of some very useful data due to a lack of development resources.
With the advent of Google Tag Manager (GTM), this is no longer the case. This is an incredibly simple platform to set up on your site and from the admin interface you can track a significant amount of on-site user actions. For example, you can track all clicks on external links, mailto: links and clicks on downloadable files such as PDFs. You can even get as granular as to track the number of times visitors play, pause and watch your YouTube video embeds to the end.
Once the Google Tag Manager script is setup on your site you will very rarely, if ever, have to change the code on your site to set up tracking.
Here’s how to set it up.
Signing up to Google Analytics and Google Tag Manager
Firstly, if you don’t have a Google Analytics account set up you’ll need to sign up for that (it’s free). It’s fairly straightforward so I won’t guide you through that. Once that is set up, visit Google Tag Manager.
Create a Google Tag Manager Account & Container
In Google Tag Manager, you will need to create an account before you do anything else. If this is the first time you are using GTM, then you should already be presented with an Add a New Account screen.
- Add your account name, such as your business name
- Type your business website address for the container name
- Select Web for Where to use container
- Accept the terms and conditions (if you agree, of course!)
Installing Google Tag Manager in a WordPress Theme
Once you have created you Google Tag Manager account, you will be presented with a snippet of code.
Installing Google Tag Manager into a WordPress theme is really easy. You can use a plugin; however, I still think it is best to paste the code snippet into your theme. Please note that if you are using a theme created by somebody else, then it is recommended to create a child theme to make edits such as this. If you don’t, then you risk losing any changes you have made if the theme gets updated.
Open up your header.php file and copy and paste the code snippet from Google Tag Manager just after the opening <body>
tag as shown below:
https://gist.github.com/raewrites/b8702d8b021d9085adf5284a4f551ae3
The code above is an example of a Google Tag Manager snippet. Replace GTM-XXXXXX with your tracking ID.
Easy done! You now have Google Tag Manager Installed on your WordPress site.
Google Tag Manager Interface
The Google Tag Manager interface can be a little confusing if you haven’t used it before. Here is a breakdown of each of the sections.
Accounts
This is where you just set up your account and container. Here you can add more accounts if you manage multiple websites and you can also create new containers. For example, if you also have a mobile app.
Container
This is where all of the fun stuff happens. Here you can see a dashboard with an overview of your tag manager activity and you can also navigate to tags, triggers, variables and folders.
Tags
These are snippets of code that execute on a page, often to send data to a third party application like Google Analytics. Generally, tags will fire when a page loads, however, this can be changed within each tag’s settings.
Triggers
These are conditions that are either true or false depending on how variables compare with values you set when defining the trigger. Triggers are then used to fire tags.
Variables
Variables can be virtually any value and some are predefined. For example, the “url” variable always contains the URL of the page that is currently loaded.
Folders
These are simply a way for you to organise your tags, triggers and variables. You can create whichever folders you like and organise them how you see fit.
Versions
For those that have used any service like Git, this is essentially GTM’s version control. Imagine you make some changes to Google Tag Manager and you end up breaking something, but you aren’t sure what. Here you can re-publish one of the prior, working versions of GTM.
Admin
This section is what you would expect it to be. Here you can manage which users have access to your account, import containers, export containers as well as find your Google Tag Manager code snippet.
The Publish Button
One of the most important parts of the interface. None of the changes you make to your tags will be live until you press the “Publish” button.
Set up Google Analytics with Google Tag Manager
Now you know what each of the interface elements are, let’s set up Google Tag Manager so Google Analytics will track visits to your site.
Setting up Google Analytics with Google Tag Manager to track standard pageviews is easy:
- Open up your GTM dashboard
- Click on Create Tag and select the Google Analytics template
- Name your tag something like “GA Pageview” as this will be tracking pageviews and you can set up other tags for GA. Don’t call it just “GA.”
- Add your Google Analytics property ID, e.g. UA-XXXXXXXX-X
- Select the track type Page Views
- Choose your firing triggers, which generally will be “All Pages” for this tag, but feel free to select something else if you don’t want to track pageviews with GA on every page
Now onto the fun stuff!
Event Tracking with Google Tag Manager
Event tracking in GA is a really useful feature for tracking user actions on your site. It can provide insights that will help you to make UX, UI and marketing decisions. You can track all manner of events, including external link clicks, video play button clicks and form submissions.
Events are made up of four fields: eventCategory, eventAction, eventLabel and eventValue. The first three fields are text and the eventValue field is an integer.
- eventCategory – this is required and is generally the object that has been interacted with, i.e. a video
- eventAction – this is required and should be how the user interacted, i.e. a click
- eventLabel – this is optional and can be used as a form of categorising, i.e. the name of a video
- eventValue – this is optional and is an integer used to represent some form of value, i.e. each lead gen enquiry on average is worth £50
Tracking mailto: Link Clicks
Let’s get into our first event to track.
We’ll track clicks on all email links, i.e. all links that contain “mailto:” within the “href” attribute. Why would we want to track email clicks? Well, we can’t be certain an email is actually sent, but we can at least track which users have made an intention to send an email.
Setting this up is pretty easy and allows us to get some practice creating triggers and tags:
Create the VariableNavigate to “Variables” within your container
- Navigate to Variables within your container
- Ensure that Click URL (within Clicks) is checked
Create the Trigger
- Navigate to Triggers
- Click New
- Name your trigger, i.e. Email Click Trigger
- Under Choose event, select Click
- Under Configure Trigger select Just Links
- Leave Wait for tags checked and with a value of 2000 milliseconds
- Leave Check Validation checked
- Under Enable When select “Page URL” “matches RegEx” “.*”
- Under Fire On select ‘Click URL” contains “mailto:”
- Click Save Trigger
- Create the Tag
- Navigate to Tags
- Click New
- Name this new tag, i.e. Email Click Tag
- Under Choose Product select Google Analytics
- Under Choose Tag Type select Universal Analytics if that is how you have set up your GA
- Under Configure Tag choose/type the following:
- Tracking ID is your GA tracking ID
- Track Type select Event
- Category can be anything you like but I would recommend something like “Email”
- Action can be anything but I would recommend “Click”
- Label should be {{Click URL}} but can be anything you like
- Value – If you have an idea of how much value you would attribute to each email enquiry that you receive, then you can add that here
- Leave the other settings as they are
- Click Continue and under Fire On select the trigger that we have just created for mailto: links
- Click Create Tag
That’s it! It seems like a lot of steps but it’s actually really quick.
Quick tip: You can track telephone number clicks the same way. Simply set up a new trigger for clicks on links that contain tel: and then create a tag to track the event in Google Analytics.
SITE MANAGEMENT
Manage multiple WordPress websites with The Hub
The Hub is your mission control for monitoring the vital stats of all your sites, including uptime, performance and security. Add as many sites as you want – including Multisite networks – and receive instant security alerts, run performance scans, and get notifications when any of your plugins or themes need to be updated.
Tracking Internal Link Clicks
We can track whether people are clicking on mailto: or tel: links, but what if we want to know whether people are clicking on a banner or whether they are clicking on a text link within an article? We can do that really easy within WordPress, GA and GTM.
There are actually a number of ways that we can set this up, but a really easy way is to make use of either class or id attributes. Before we set up anything in GTM, we need to make sure that the link we want to track either has a unique ID attribute or if it is multiple links of the same type, e.g. all footer links, then they should all have a class that is specific to them as a group (recommended) or each one would require a unique ID.
Depending on how your theme is built will determine the next step. If you can edit the links you want to track with the WordPress text editor, then it is an easy process to add an id attribute or class to your links. If you can’t edit the link in the WordPress text editor then you may need to edit your actual template files to add the id or class attributes. Remember to create a child theme when editing your theme template files.
Create the Variable
- In your GTM container dashboard, navigate to Variables
- Make sure that you have Click Classes and/or Click ID checked
Create the Trigger
- Navigate to Triggers
- Click New
- Type in a name for this trigger, i.e Banner Click Trigger
- Under Choose Event select Click
- Under Configure Trigger select Just Links and leave Wait for tags and Check Validation checked
- Under Enable When select “Page URL” “matches RegEx” “.*”
- Under Fire On click Some Links
- Select either Click ID or Click Classes (depending on whether you used ID or class attributes)
- Select equals if using IDs or contains if using classes (sometimes you may want contains or matches RegEx for IDs as well)
- Add either the ID or class of the link that you want to track into the text box
- Click Create Trigger
Create the Tag
- Navigate to Tags
- Click New
- Name this new tag, i.e. Banner Click Tag
- Under Choose Product select Google Analytics
- Under Choose Tag Type select Universal Analytics if that is how you have set up your GA
- Under Configure Tag choose/type the following:
- Tracking ID is your GA tracking ID
- Track Type select Event
- Category can be anything you like but I would recommend something like “Banner”
- Action can be anything but I would recommend “Click”
- Label should be {{Click URL}} but can be anything you like
- Value – If you have an idea of how much value you would attribute to each banner click, then you can add that here
- Leave the other setting as they are
- Click Continue and under Fire On select the trigger that we have just created for clicks with the specified ID attribute
- Click Create Tag
Easy. We now have events being fired whenever our specified banner is being clicked.
1.6 million WordPress Superheroes read and trust our blog. Join them and get daily posts delivered to your inbox – free!
Quick tip: Click events can be tracked on any element, not just links. When choosing the options for Configure Trigger, select All Elements instead of Just Links.
Tracking External Link Clicks
What if we want to track all clicks on external links? Well, this requires a slightly different approach as we will need to create a new variable, but it’s still very simple to set up.
Create the Variable
- Go to your GTM container dashboard
- Navigate to Variables
- Within User-Defined Variables, click New
- Name your variable, i.e. Click URL Host Name
- Select Auto-Event Variable
- Within Configure Variable select Element URL
- Select Host Name from Component Type
- Click Create Variable
Create the Trigger
- Navigate to Triggers
- Click New
- Name your variable, i.e. External Link Click Trigger
- Under Choose Event select Click
- Under Configure Trigger and Targets select Just Links
- Leave Wait for Tags and Check Validation checked
- Click Continue
- Under Enable When select “Page URL” “matches RegEx” “.*”
- Under Fires On select the Click URL Host Name variable that you had created previously
- Select does not contain and then enter your website’s domain name, i.e. example.com
- Click Save Trigger
Create the Tag
- Navigate to Tags
- Click New
- Name your tag, i.e. External Link Click Tag
- Under Choose Product select Google Analytics
- Under Choose Tag Type select whether you set up GA with Universal Analytics or Classic Google Analytics
- Under Configure Tag choose/type the following:
- Tracking ID is your GA tracking ID
- Track Type select Event
- Category can be anything you like but I would recommend something like ‘External Link’’
- Action can be anything but I would recommend ‘Click’
- Label should be {{Click URL}} but can be anything you like
- Value – If you have an idea of how much value you would attribute to each click (if any), then you can add that here
- Leave the other setting as they are
- Click Continue and under Fire On select the trigger that you have just created
- Click Save Tag
Easy. Google Analytics will now track every time someone clicks on an external link as an event.
Tracking YouTube Video Events
Tracking embedded YouTube events can provide useful information regarding which videos are actually engaging your visitors. Setting up YouTube embed event tracking is a little more complex than any of the GTM examples so far, but luckily there is a great repository on GitHub which solves many of our problems.
There are actually two ways to get event tracking working with YouTube video embeds using this repo. We will set this up using the more manual approach so that we can understand each of the steps involved. If you would like to use the other method, which involves importing a container into GTM, then please follow the guidelines in this repository’s readme.
Create the Custom HTML Tag
- Navigate to Tags
- Click New and name your tag, i.e. YouTube Embed API Tag
- Under Choose Product click Custom HTML Tag
- Under Configure Tag copy and paste everything from this GitHub file into the HTML text input box (make sure you wrap this code with
<script></script>
tags) - Click Continue
- Under Fire On select All Pages
- Click Create Tag
Create the videoUrl Variable
- Navigate to Variables
- Click New and give this variable the name “videoUrl”
- Under Choose Type select Data Layer Variable and click Continue
- Under Configure Variable in the Data Layer Variable Name text input, type “attributes.videoUrl”
- Click Create Variable
Create the videoAction Variable
- Navigate to Variables again, unless you are already there
- Click New and give this variable the name “videoAction”
- Under Choose Type select Data Layer Variable and click Continue
- Under Configure Variable in the Data Layer Variable Name text input, type “attributes.videoAction”
- Click Create Variable
Create the Trigger
- Navigate to Triggers
- Click New and name the trigger “YouTube Video Event”
- Under Choose Event click Custom Event and click Continue
- Under Fire On in the Event Name text input type “youTubeTrack”
- Click Add Filters
- Enter “videoAction” “matches RegEx” “Play|Pause|Watch to End|25%|50%|75%”
- Click Create Trigger
Create the Event Tag
- Navigate to Tags
- Click New and name your tag, i.e. YouTube Embed Event Tag
- Under Choose Product select Google Analytics
- Under Choose a Tag Type select whichever yours is
- Under Configure Tag choose/type the following:
- Tracking ID is your GA tracking ID
- Track Type select Event
- Category can be anything you like but I would recommend something like ‘Video’’
- Action must be {{videoAction}}
- Label must be {{videoUrl}}
- Value – If you have an idea of how much value you would attribute to each video event (if any), then you can add that here
- Click Continue
- Under Fire On select All Pages
- Click Create Tag
That’s it! Publish your changes and then go to any page on your website that has a YouTube video embed. Play the video, pause and watch it to the end, then check your Google Analytics Real-Time events and you should see those events being recorded straight away.
Virtual Pageviews and Goal Tracking
Did you know that you can track virtual pageviews with GA? What are virtual pageviews? Well, the “standard” GA pageview is generally triggered when a web page initially loads; however, it is possible to create a virtual pageview in Google Analytics. This is tracked in GA as a pageview and you can set the URL path as whatever you would like.
You may be wondering why you would want to create virtual pageviews. Imagine you have a single page application (SPA) and a lot of the content is loaded dynamically via AJAX, then you are unlikely to be able to track this with a standard GA pageview. Virtual pageviews could be used to track AJAX content generation in this instance.
Virtual pageviews are also great for tracking GA goals that are set up for destination URLs. It is possible to set up GA goals for events, but you cannot set up funnels for them. Funnels can only be used with destination goals and sometimes a goal may not result in the visitor loading a new page.
A great example would be PDF files that visitors can download. In this instance you would have to set up virtual pageviews to track this download as a goal if you wanted to use a funnel. Let’s take a look at how we would set that up.
Create Click URL Path Variable
- Navigate to Variables
- Click New and name this variable Click URL Path
- Select Auto-Event Variable
- Within Configure Variable’ select Element URL
- Select Path from Component Type
- Click Create Variable
Create PDF Download Trigger
- Navigate to Triggers
- Click New and name this trigger, i.e. PDF Download Trigger
- Under Configure Trigger choose Just Links for Target
- Click Continue
- Under Enable When select “Page URL” “matches RegEx” and type “.*”
- Click Continue
- Under Fire On click Some Clicks
- Select “Click URL” “matches RegEx” and type “\.pdf$”
- Click Create Trigger
Create PDF Download Tag
- Navigate to Tags
- Click New and name your tag, i.e. PDF Download Tag
- Under Choose Product select Google Analytics and click Continue
- Under Choose a Tag Type select the type of GA tag you are using and click Continue
- Under Configure Tag, “Tracking ID” is your GA tracking ID
- Track Type select Page View
- Click More Settings
- Click Fields to Set
- Select “Page” as the Field Name and {{Click URL Path}} as the Value
- You can optionally set campaignMedium; however, if you don’t set this, “medium” will display as “none” in GA
- You can optionally set campaignSource; however, if you don’t set this “source” will display as “direct” in GA
- You can optionally set title; however, if you don’t set this the title in GA will be set to the title of the page the PDF download link is on
- Value – If you have an idea of how much value you would attribute to each PDF download (if any), then you can add that here
- Under Fire On click More and select the trigger we have just created
- Click Create Tag
Publish your changes and you’re finished.
If you click on any links to PDF files on your site, then these will trigger virtual page views that you can use to set up GA goals with funnels.