Cookiebot and tracking embedded YouTube videos in Google Tag Manager
Has anyone successfully set up the GTM YouTube video trigger and gotten it to work successfully alongside Cookiebot? I am having issues getting the trigger to fire. After some troubleshooting, I isolated Cookiebot as the issue. When I remove the code that prevents the iframe from loading until the visitor has consented, the GTM YouTube trigger works. (https://support.cookiebot.com/hc/en-us/articles/360003790854-Iframe-cookie-consent-with-YouTube-example)
I think the issue is that the iframe is being reloaded after Google Tag Manager has already initialized the video trigger.
Is there a way to get this GTM trigger to work alongside Cookiebot?
Thanks!
-
Official comment
Hi Elena,
For implementing the same solution in a GTM custom HTML tag, you should use the following code:<script>
window.addEventListener('CookiebotOnAccept', function () {
var iframes = document.getElementsByTagName('iframe');
for (var i = 0; i < iframes.length; i++)
for (var a = 0; a < i.attributes.length; a++) {
var t = /data-gtm-yt-inspected-\w+/.exec(a.name);
if (t) i.removeAttribute(t[0]);
}})
</script>
A regular 'Page View' trigger should be fine to use for this custom tag. -
Hi! I'm having this same issue did you ever figure out a solution?
0 -
I submitted a support ticket, and Cookiebot support was able to come up with the following solution for me:
We've found a way to re-trigger the GTM YouTube check, to make GTM also check the YouTube iframe(s) AFTER Cookiebot changes the 'data-src' to 'src', meaning that GTM will then identify the iframe as a valid YouTube iframe and therefore allow the YouTube trigger to work.
Can you try adding this script on your website?:<script>
window.addEventListener('CookiebotOnAccept', function () {
var iframes = document.getElementsByTagName('iframe');
for (var i of iframes)
for (var a of i.attributes) {
var t = /data-gtm-yt-inspected-\w+/.exec(a.name);
if (t) i.removeAttribute(t[0]);
}})
</script>This will look for the inspection attribute added by GTM, and remove it once valid consent has been loaded from a Cookiebot cookie (either via an existing cookie or by your visitors accepting cookies, thus creating the Cookiebot cookie). This will trigger the GTM YT check to be done again, but this time it will be done after 'data-src' is changed to 'src', if the visitor has consented to the required cookie categories.
You should not have to change anything to your existing setup - only add the above code snippet anywhere in your source code (at least I've been able to get it to work both in the <head> section and in the bottom of the <body> section of your website, via local overrides).0 -
When I add that custom html tag on GTM and publish I get this error:
0 -
There's no way I can track youtube engagement of embedded videos in GA4, even when video engagement is a built-in event on GA. We also use cookiebot. eg. https://beebole.com/blog/finance-controller-kpis/
What are we doing wrong? thank YOU0
Please sign in to leave a comment.
Comments
5 comments