YouTube Video Tracking

Tara in the Maldives by mark l chaves
Tara in the Maldives by mark l chaves

Upgraded to use gtag.js API on 14 March 2021.

A WordPress plugin for tracking YouTube Video Events in Google Analytics

With support for tracking logged-in user IDs

This is a demo of the caught my eye GA4YT WordPress plugin. GA4YT supports tracking by WordPress user ID.

To get the full effect, open your dev tools console window first (feel free to use the GA debugger or Omnibug instead). Then, play/pause the video. The plugin outputs messages that start with [YouTube].

If you’re logged in, you’ll see the user ID and custom dimension index printed to the console.

Connect with me on my main site to ask me for a temporary login 😉

The YouTube Video

GA4YT Plugin Usage

Before You Start

Make sure you’ve already enabled user ID tracking in Google Analytics and on your WordPress site.

You can add user ID tracking to WordPress by using a plugin such as MonsterInsights (Pro version) or by manually inserting gtag.js code.


  1. Download the zip file from GitHub.
  2. Log in to your WordPress site as an admin.
  3. Navigate to Plugins > Add New.
  4. Click Upload Plugin.
  5. Select the zip file downloaded in step 1.
  6. Click Install Now.
  7. Click Activate.

Next: Write your filter hook for your user ID custom dimension index.

PHP Filter Hook

Add a call to the cme_user_id_custom_dimension_index filter hook to your child theme’s functions.php file. Change the custom dimension index to match the user ID custom dimension index in your Google Analytics property.

add_filter( 'cme_user_id_custom_dimension_index', function($custom_dimension_index) {
	return '5'; // Change this to match your CD index number that's set in GA.
} );

Now, Go Have Fun!

That’s it. Now, Google Analytics will track your YouTube video events. If a logged-in user plays your YouTube videos, their user ID is recorded along with each video event.

Screen Captures

cme GA4YT plugin Google Analytics real-time report
Realtime Report
cme GA4YT plugin Google Analytics custom report
Custom Report for YouTube and hTML5

Wrapping Up

Download or fork your copy on GitHub.

See a live demo of the caught my eye GA4HTML5VID plugin that tracks HTML5 video events by user ID.

See a live demo of HTML5 video tracking by user ID using GTM.

The JavaScript source for tracking YouTube events was originally written by LunaMetrics. The code was forked from the Bounteous repo.