"Tracking doesn’t have to come at the cost of privacy"
What is the Problem?
Google Tag Manager (GTM) provides built-in ways to track YouTube videos, but things get tricky when you’re using privacy-friendly youtube-nocookie.com
. This method enhances privacy by preventing cookies, but it also removes the traditional tracking abilities provided by YouTube's standard embed code.
So how can you still track important metrics like play, pause, and complete events without violating privacy regulations?
Solution:
Using GTM and the YouTube Iframe API, you can monitor key video events without violating privacy policies. Here's my solution:
We would need to define a variable for tracking one of the youtube elements. Let's choose video label, which will be pushed to the Data Layer when activated.
We need a trigger for capturing youtube event from our future listener. Let's name it YT Event with youtube event name. This trigger will link to our GA4 Tag with the same event name and our variable as a parameter that we created in our first step.
Now, we go to the most intriguing and difficult part: the listener. There are some internet sources with their own versions of listener, however if your Youtube videos src container appears only after clicking on it and not on the load of the page, you would have to think about your own listener. Worry not! Here is the solution: We need to set up a listener tag, which will always try to capture youtube presence on the page. The way to do it is to trigger it on every click on your website (or you can come up with your own more effective way) with the following JS code:
How It Works:
API Activation: The YouTube Iframe API is loaded and constantly monitors for YouTube iframes.
Event Tracking: The custom listener tracks key video interactions (play, pause, and percentage watched) and pushes them to the Data Layer.
Triggering Tags: GTM uses these Data Layer pushes to trigger tags for each video event.
With this setup, you can track YouTube interactions while complying with privacy regulations, as no cookies are involved.
And there you have it! With this setup, you can keep an eye on how users interact with your YouTube videos while still playing by the privacy rules. No cookies, no worries—just the data you need. I spent a fair bit of time researching and tweaking outdated methods to come up with this solution, so I hope it saves you some headaches.
Reference:
As I mentioned, here are some of the sources I used for trying to implement it and ended up enhancing some outdated methods :)