How to Track YouTube Videos in Google Tag Manager

How to Track YouTube Videos in Google Tag Manager

YouTube video tracking has now become easier since the release of built-in video trigger in Google Tag Manager. If you are looking to track embedded YouTube videos, this guide is for you. 

Without wasting time explaining the same things, let's face the new video trigger and variables. Follow the steps below:

 

Step1: Enable Built-in Video Variables

Before doing anything specific, click on variables and enable all the new video built-in variables.

enable GTM video variables

Step2: Create a Custom JavaScript Variable to Track Action

Within the variables section, after enabling the built-in video variables. The second step is to create a user-defined custom javascript variable.

This variable will capture the action a user takes on your video(play/pause).

Click New button under user-defined variables.

enable user-defined variables

Enter the name of variable i.e JS - Get Video Action

Click variable configuration and select the variable type as custom javascript

Paste the below code written by Simo Ahava into the custom JavaScript field

function() {
  var status = {{Video Status}};
  switch (status) {
    case 'start':
      return 'Start playing';
    case 'pause':
      return 'Pause';
    case 'buffering':
      return 'Buffering';
    case 'progress':
      return 'Reached ' + {{Video Percent}} + '%';
    case 'complete':
      return 'Reached the end';
  }
}
custom javascript variables

Save this variable.

 

Step3: Create & Configure Video Trigger

Open triggers tab and click on the new button to create a new trigger.

Enter a unique name for the trigger, click trigger configuration, and select YouTube Video as the trigger type.

Configure the trigger as displayed in the image below. You can uncheck the options you don’t want to track.

youtube video trigger

The first three things in the above image are understood.

You can track the video progress in two ways. The first is by percentages, and second by time thresholds. Both are used to track how long a video has been played.

In the advanced section, by checking the ‘Add JavaScript API support to all YouTube videos’ will fire the trigger on videos that have JS API support.

Once you configure this, save the trigger.

 

Step4: Create YouTube Video Tag

Now, navigate to tags section and create a new Google Analytics tag.

Configure the tag as displayed in the image below.

youtube tag

You will use the video variables you have enabled in the step to send event data to Google Analytics.

Add the trigger we created in the third step to this tag.

Save tag and open preview mode. Open your web page containing the video in a new tab.

If you have done the above steps carefully, you the YouTube event will fire.

preview and debug tag

To see the data in Google Analytics, open real time >> events.

google analytics real time events

Now that we have confirmed video tracking is working successfully. You can now publish changes in Google Tag Manager.


If you have any questions related to this article, please ping us on Twitter or share if you find this helpful.