This article is related to Google Tag Manager (GTM). If you are brand new to GTM then I would suggest reading this article first: Beginners guide to Google Tag Manager.
Follow the steps below to install Google Tag Manager YouTube Video Tracking:
Step #1: Embed YouTube video players on your website and use the API for iframe embeds
Before you can track YouTube videos, you need to embed them on your web page(s):
While this is a no-brainer, I have seen many people trying to track YouTube videos when no such videos have been embedded on their website or trying to track Vimeo videos using the YouTube video tracking script.
This article is only about tracking YouTube videos that are embedded on your website. You can’t track the YouTube videos embedded on YouTube.com in Google Analytics via my video tracking script.
This article is not about tracking videos hosted by other video hosting players like: ‘Vimeo’ or ‘Wistia’.
However, you don’t need to worry about understanding the YouTube Player API. I have done all of the hard work for you. You just need to understand and use my video tracking script.
Note: Instead of writing the whole video tracking script from scratch, I modified the video tracking script which was originally developed by ‘Cardinal Path’. Being an ex-programmer, I understand how difficult it can be to write a script from scratch.
Despite its shortcomings, this is the best video tracking script I have found so far. I am just building upon the work done by ‘Cardinal Path’ and give them the full credit for it.
Step-2: Check whether YouTube video is embedded on a web page
#2.1 Login to your GTM account and create following new variable named ‘Is YouTube video embedded on a web page‘ of type ‘Custom JavaScript‘ which checks, whether YouTube video is embedded on a web page. This variable returns ‘true’ if YouTube video is embedded on a web page, otherwise it returns ‘false’.
Following is the code, which you need to copy-paste, into the ‘custom JavaScript’ variable text box:
// Return "true" if YouTube video is embedded on a web page otherwise false
function () {
for (var e = document.getElementsByTagName('iframe'), x = e.length; x--;)
if (/youtube.com\/embed/.test(e[x].src)) return true;
return false;
}
#2.2 Create a new folder named ‘YouTube Video Tracking’ and add this variable to it:
By adding the variable to a folder, it becomes easy to find this variable later for editing/debugging purposes.
#2.3 Click on the ‘Save’ button to complete the process of creating the new variable.
Step-3: Find web pages which contain one or more YouTube videos
Create following new trigger named ‘Pages on which one or more YouTube videos have been embedded‘, which fires on a DOM load (Pageview – DOM Ready) and when a YouTube video is embedded on a web page:
Here, ‘Is YouTube video embedded on a web page‘ is the custom JavaScript variable we created earlier. This trigger is fired only when YouTube video is present on a web page.
Add the trigger ‘Pages on which one or more YouTube videos have been embedded‘ to the ‘YouTube Video Tracking’ folder so that it becomes easy to find/edit it later:
Step-4: Create / Customize video tracking script
Here is what my video tracking script does, in a nutshell:
Tie YouTube videos to the page on which they are embedded.
Track single/multiple video players (same as with the original script)
Tie player state (play, pause and the percentage of video played) to the video title thus making it easy to understand the performance of a particular video without drilling down further.
Does not append Video ID to the video title, thus making it easy to read the video title
Creates an ‘event category‘ called ‘Youtube Videos’ which store all of the YouTube video events.
Use ‘event action‘ to store the list of all the web pages where users interacted with one or more videos.
Use ‘event label‘ to show the player current state and the title of the video being watched.
Event Category: Youtube Videos
Event Action: <URI of the web page on which a user interacted with one or more videos>
Event Label: <Player Current State> + Title of the Video
Here is how the output from my video tracking script will look in Google Analytics, once the video tracking has been implemented:
Note: I have deliberately put the player state before the video title so that they are always visible in GA reports even when the video title is too long. Otherwise, a very long video title can hide the player state in the event reports.
Copy the YouTube Video tracking code from here. You may need to customise this script if it doesn’t work the way you would like to, on your website.
Step-5: Push video player states as events into data layer whenever a video is played or paused
Create a new custom HTML tagnamed ‘Push YouTube Video Player States as events into dataLayer‘. This tag will push YouTube video player states (play, pause, percentage of video played) into a data layer, whenever a YouTube video is played or paused on a web page which contains the video player:
Copy-paste the video tracking script we created/customised in step 4, into the HTML text box as shown above.
Fire this tag on pages on which one or more YouTube videos have been embedded by adding the trigger we created earlier i.e. ‘Pages on which one or more YouTube videos have been embedded‘:
Click on the ‘save’ button to complete the custom HTML tag creation process. Add this tag to the ‘YouTube Video Tracking’ folder so that it becomes easy to find/edit it later.
Step-6: Pull YouTube videos event data from the data layer into the container tag
In order to pull YouTube videos event data (event category, event action and event label) from the data layer into the container tag, create the following three user-defined variables of type ‘data layer’:
#1 ‘Pull event category YouTube Video Player State from dataLayer’
#2 ‘Pull event Action YouTube Video Player State from dataLayer’:
#3 ‘Pull event Label YouTube Video Player State from dataLayer’:
Add all of these variables to the ‘YouTube Video Tracking’ folder so that it becomes easy to find/edit them later:
Step-7: Find web pages where the YouTube Player states were tracked
Create the following new trigger of type ‘custom event’ which looks for ‘youtube’ event key in a data layer.
The ‘youtube‘ event key is pushed into the data layer only when the YouTube video player state is tracked. This trigger will later be required while sending YouTube video event data to Google Analytics.
Add this trigger to the ‘YouTube Video Tracking’ folder so that it becomes easy to find/edit it later.
Step-8: Send YouTube player states as events to Google Analytics
Create a new tag named ‘Send YouTube player states as events to Google Analytics‘ of type ‘Universal Analytics’ which send YouTube player states as events to Google Analytics on pages where the YouTube Player states were tracked. Use the following tag configuration:
Fire this tag on pages where YouTube player states were tracked by using the trigger named ‘Pages where YouTube player states were tracked‘ we created earlier:
This is the tag that will send all the interactions with YouTube video player as events to your Google Analytics reports. Add this tag to the ‘YouTube Video Tracking’ folder so that it becomes easy to find/edit it later.
Note: {{Pull event category YouTube Video Player State from dataLayer}}, {{Pull event Action YouTube Video Player State from dataLayer}} and {{Pull event Label YouTube Video Player State from dataLayer}} are all user-defined variables of type data layers which we just created earlier. Follow the steps below to use these data layer variables while creating this tag:
#8.1 Click on the plus button next to ‘Category’ text field:
#8.2 Select the ‘Pull Event Category from the data layer’ variable:
Similarly, you can select other data layer variables. Once you have selected all the three data layer variables, your final set up will look like the one below:
"How to use Digital Analytics to generate floods of new Sales and Customers without spending years figuring everything out on your own."
Here’s what we’re going to cover in this training…
#1 Why digital analytics is the key to online business success.
#2 The number 1 reason why most marketers are not able to scale their advertising and maximize sales.
#3 Why Google and Facebook ads don’t work for most businesses & how to make them work.
#4 Why you won’t get any competitive advantage in the marketplace just by knowing Google Analytics.
#5 The number 1 reason why conversion optimization is not working for your business.
#6 How to advertise on any marketing platform for FREE with an unlimited budget.
#7 How to learn and master digital analytics and conversion optimization in record time.
My best selling books on Digital Analytics and Conversion Optimization
Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.
Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.
Attribution Modelling in Google Analytics and BeyondSECOND EDITION OUT NOW!
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.
Attribution Modelling in Google Ads and Facebook
This book has been written to help you implement attribution modelling in Google Ads (Google AdWords) and Facebook. It will teach you, how to leverage the knowledge of attribution modelling in order to understand the customer purchasing journey and determine the most effective marketing channels for investment.
About the Author
Himanshu Sharma
Founder, OptimizeSmart.com
Over 15 years of experience in digital analytics and marketing
Author of four best-selling books on digital analytics and conversion optimization
Nominated for Digital Analytics Association Awards for Excellence
Runs one of the most popular blogs in the world on digital analytics
Consultant to countless small and big businesses over the decade
Get My Step-By-Step Blueprint For Finding The Best KPIs (32 pages ebook)
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
Cookie
Duration
Description
cookielawinfo-checkbox-analytics
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional
11 months
The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance
11 months
This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy
11 months
The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.