Skip to main content

Cookiebot and tracking embedded YouTube videos in Google Tag Manager

Comments

11 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
  • davidwarner

    To make the GTM YouTube video trigger work alongside Cookiebot, ensure that the Cookiebot script is loaded before the GTM script. Consider using a combination of Cookiebot and GTM, enabling the GTM video trigger only when the user gives consent to YouTube cookies. If the issue persists, contact Cookiebot's support for further assistance. Test the implementation thoroughly.

    0
  • Emma Jackson

    Subject: GTM YouTube Trigger and Cookiebot Compatibility

    I've faced a similar challenge with GTM YouTube triggers and Cookiebot. To resolve the issue, consider adjusting the script loading sequence. Ensure GTM initializes after Cookiebot obtains consent. Try this basic script:

    if (Cookiebot.consents.given && Cookiebot.consents.given.indexOf('cookie_category') > -1) {
        // Initialize GTM video trigger here
        // ...
    }

     

    Replace 'cookie_category' with your actual Cookiebot category. Test thoroughly to ensure compatibility.

    Hope this helps,

    0
  • Quick Graphics

    Yes, integrating Google Tag Manager (GTM) YouTube video triggers with Cookiebot or other consent management platforms can sometimes lead to conflicts due to the way these tools manage consent and loading of embedded content such as YouTube videos.

    Cookiebot, like many other consent management platforms, operates by preventing the loading of certain scripts or iframes until the user gives their consent to tracking cookies or other functionalities. This behavior can interfere with GTM triggers that rely on the immediate loading and interaction with YouTube videos.

    When you remove the code that prevents the iframe from loading until consent is given, the trigger works because the YouTube iframe loads immediately along with the associated GTM trigger.

    To make GTM YouTube video triggers work alongside Cookiebot, you might need to consider a few approaches:

    1. Delay Trigger Setup: Some consent management platforms allow you to trigger events or load specific content only after consent has been given. You might explore setting up your GTM triggers to activate after Cookiebot has confirmed user consent.

    2. Customization in Cookiebot: Check if Cookiebot allows custom configurations or exceptions for specific scripts or iframes. You could whitelist the necessary YouTube iframe or GTM scripts related to video triggers.

    3. Custom JavaScript Solution: Develop a custom JavaScript solution that waits for the user's consent through Cookiebot before enabling the YouTube video trigger in GTM.

    4. Utilize GTM Custom Events: Instead of relying solely on the YouTube video trigger, you might create custom events in GTM that fire when consent is given. These events can then trigger the desired actions related to YouTube videos.

    5. Consult Documentation and Support: Check the documentation of both GTM and Cookiebot for any specific instructions or known compatibility issues. If necessary, reach out to their support teams for guidance

    0
  • mrhsnnnnn
    This code will help you just try it 
    <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>
    -1
  • jackma

    Did you got any solution I am facing same issue

    0
  • Josh Buttler

    I have also faced the same, Kindly update me if you have a solution for this

    0

Please sign in to leave a comment.