Skip to main content

Cookiebot and tracking embedded YouTube videos in Google Tag Manager

Comments

5 comments

  • Official comment
    Christian Degn Andreasen

    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. 

  • Domain Admin

    Hi! I'm having this same issue did you ever figure out a solution?

    0
  • Pam Hardy

    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
  • Elena Benito Ruiz

    When I add that custom html tag on GTM and publish I get this error:

    0
  • Elena Benito Ruiz

    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 YOU

    0

Please sign in to leave a comment.