YouTube Video tracking via Google Tag Manager

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 YouTube video tracking via Google Tag Manager:

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’.

I used the YouTube Player API for iframe embeds to access and control the YouTube player which has been embedded via iframe.

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.

Get the E-Book (62 Pages)

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:

  1. Tie YouTube videos to the page on which they are embedded.
  2. Track single/multiple video players (same as with the original script)
  3. 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.
  4. Does not append Video ID to the video title, thus making it easy to read the video title
  5. Creates an ‘event category‘ called ‘Youtube Videos’ which store all of the YouTube video events.
  6. Use ‘event action‘ to store the list of all the web pages where users interacted with one or more videos.
  7. 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:

youtube videos event category

event action report

youtube videos event label

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 tag named ‘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:

plus button#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:

Step-9: Preview, debug and publish your tags

Preview and debug your new tags, create a new container versions and then publish it.

# Go to a web page on which you have embedded a YouTube video and then play and pause it.

Check the real-time ‘Events‘ report to see whether your Google Analytics is capturing the video events:

real time events report

Related Article: Video Tracking via YouTube Video Trigger In Google Tag Manager

Integrating YouTube with Google Analytics

If you want to integrate your YouTube Channel with your Google Analytics account and track channel views then read this article: Google Analytics and YouTube Integration Guide

Other articles on specialized tracking in Google Analytics

  1. Ecommerce Tracking in Google Analytics – Tutorial
  2. Event Tracking via Google Tag Manager – Tutorial
  3. Event Tracking in Google Analytics – Tutorial
  4. Guide to Google Analytics Store Visits Tracking
  5. Offline Conversion Tracking in Google Analytics – Tutorial
  6. Implementing E-Commerce Tracking via Google Tag Manager
  7. Tracking Virtual Pageviews in Google Tag Manager – Tutorial
  8. YouTube Video tracking via Google Tag Manager
  9. How to Use Keyword Hero to Reveal Not Provided Keywords in Google Analytics
  10. Virtual pageviews in Google Analytics – Tutorial
  11. Google Analytics and YouTube Integration Tutorial
  12. Google Analytics for Facebook Tutorial
  13. Google Analytics Cross Domain Tracking Explained Like Never Before
  14. Using multiple Google Analytics tracking codes on web pages
  15. The one thing that you don’t know about PayPal.com and the referral exclusion list
  16. Calculated Metrics in Google Analytics – Tutorial
  17. Creating your own Google Analytics Tag Auditing System
  18. Tracking Site Search without Query Parameter in Google Tag Manager
  19. Tracking true referrals in Google Analytics when using PayPal and other payment gateways
  20. Phone Call Tracking in Google Analytics and Beyond
  21. Learn to Track Qualified and Won Leads in Google Analytics
  22. Introduction to Postbacks in Google Analytics
  23. Google Analytics Recurring Revenue and Subscriptions Tracking Tutorial
  24. How to track the impact of cookie consent on website traffic in Google Analytics
  25. Tracking Offline Conversions in Google Ads
  26. Implementing Scroll Tracking via Google Tag Manager
  27. Scroll Tracking via Scroll Depth Trigger in Google Tag Manager
  28. Site Search Tracking In Google Analytics Without Query Parameters
  29. Video Tracking via YouTube Video Trigger In Google Tag Manager
  30. How to Correctly Measure Conversion Date & Time in Google Analytics
  31. Google Analytics Social Tracking – Twitter, Facebook, Google Plus and LinkedIn
  32. Google Analytics Cross Domain Tracking (ga.js)
  33. Tracking Twitter and Linkedin Social Interactions in Google Analytics
  34. Creating Content Group in Google Analytics via tracking code using gtag.js
  35. Tracking Site Search in Google Analytics with Query Parameters
  36. Understanding site search tracking in Google Analytics
  37. Creating and Using Site Search Funnel in Google Analytics
  38. Learn to Setup Facebook Pixel Tracking via Google Tag Manager
  39. Setting up & Tracking AMP Pages in Google Analytics
  40. Setting up Sales Funnel across websites in Google Analytics
  41. Regular Expressions (Regex) for Google Analytics & Google Tag Manager – Tutorial

Register for the FREE TRAINING...

"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 Beyond
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.

Himanshu Sharma

Digital Marketing Consultant and Founder of Optimizesmart.com

Himanshu helps business owners and marketing professionals in generating more sales and ROI by fixing their website tracking issues, helping them understand their true customers' purchase journey and helping them determine the most effective marketing channels for investment.

He has over 12 years of experience in digital analytics and digital marketing.

He was nominated for the Digital Analytics Association's Awards for Excellence. The Digital Analytics Association is a world-renowned not-for-profit association that helps organisations overcome the challenges of data acquisition and application.

He is the author of four best-selling books on analytics and conversion optimization:

error: Alert: Content is protected !!