- Save time! import/merge the required configuration for the new GTM UI!
- improved in-line documentation and minor bug fixes
- track onunload event
- minor fix to error reporting
- now supports multiple players on the same page
- includes optional code to add the necessary enablejsapi=1 to the video URL
- now track the video title instead of the video id
During our GTM+UA webinar I was asked about tracking YouTube videos with the help of Google Tag Manager and Universal Analytics. I quickly demonstrated how we’re doing it in our WASP playground.
Here’s a step by step guide to the macros, tags and rules required to track YouTube video embeds on your website.
A note for Google Analytics users: For sake of demonstration I’m showing Universal Analytics, but you could easily replace the Universal Analytics Event by a regular Google Analytics one and everything will work fine.
Prerequisites
We will use the YouTube JavaScript API to expose user interactions with the video. In order for this to work, each of your YouTube embeds will need to include “enablejsapi=1” to the link of the video, for example:
Note: the “Is Youtube present” macro could be modified to automatically add the enablejsapi if it’s not there, but this will result in a quick flash of the Youtube frame.
Note: by default, related videos will be shown and tracked. If you want to disable this, add “&rel=0” to the Youtube embed link.
What you’ll get
Under Behavior/Events/Top Events, you will see an event category named “video”, with event actions named “pause”, “play”, 0%, 25%, 50%, 75% and 100%. The event label will be the unique identifier of the video.
Custom Report for Video Performance
If you want to get details about how individual videos are performing, follow those steps:
- Go in yout Behavior/Events/Top Events/video report;
- Click on “Customize” in the top left of the report;
- Under Dimension Drilldown, remove the “Event Category” dimension and switch “Event Action” with “Event Label”, so it now reads Event Label/Event Action;
- Modify the filter so it now reads “Event Category equals video”, so only videos are shown in this new custom report;
- Voilà! You now have detailed performance info about each individual video embedded on your website.
Macros
Is Youtube present?
You have two options here, either use this macro to automatically detect when a video is embedded on a page, or use a rule to include the Youtube Listener tag only on pages where you know there’s a video.
Macro Name: | Youtube is present |
Macro Type: | Custom JavaScript |
Custom JavaScript: | // Return "true" if there is at least one Youtube video on the page function () { for (var e = document.getElementsByTagName('iframe'), x = e.length; x--;) if (/youtube.com\/embed/.test(e[x].src)) return true; return false; } |
event
Macro Name: | event |
Macro Type: | Custom Event |
dataLayer action
Macro Name: | dataLayer action |
Macro Type: | Data Layer Variable |
Data Layer Variable Name: | action |
dataLayer label
Macro Name: | dataLayer label |
Macro Type: | Data Layer Variable |
Data Layer Variable Name: | label |
Tags
Listen for Youtube activity
Tag Name: | Youtube Listener |
Tag Type: | Custom HTML Tag |
Firing Rule: | Youtube present |
HTML: |
UA Youtube event
If you are using Google Analytics instead of Universal Analytics, simply change the Tag Type to Google Analytics and everything will work fine.
Tag Name: | Youtube Event |
Tag Type: | Universal Analytics |
Tracking ID: | UA-XXXXXX-Y |
Track Type: | Event |
Event Tracking Parameters: | |
Category: | {{event}} |
Action: | {{dataLayer action}} |
Label: | {{dataLayer label}} |
Firing rule: | Youtube event |
Rules
Youtube embed found on a page
Rule Name: | Youtube present |
Conditions: | {{event}} equals gtm.dom AND {{youtube is present}} equals true |
Youtube is telling us something
Rule Name: | Youtube event |
Conditions: | {{event}} equals youtube |
Putting it all together
That’s it! Now you can track Youtube video embeds on your own website thanks to Google Tag Manager and Universal Analytics.
Don’t forget to use Google Tag Manager “debug & preview” with WASP to make sure everything works fine before you publish.
This topic, and many more, are covered in our GTM+UA Master Class. Please visit the Cardinal Path training page for free and custom training options.
Check out the new, and up to date version by Nicky Yuen.
Hungry for more? Check out our annual Analytics Rising Virtual Conference. Sign up today!