Cookie Consent Script Error

User
Uncaught SyntaxError: Failed to execute 'insertBefore' on 'Node': Invalid or unexpected token
at dequeueNonAsyncScripts (uc.js:2:30055)
at window.CookieControl.Cookie.dequeueNonAsyncScripts (uc.js:2:108046)
at stopMutationObserver (uc.js:2:23800)
at window.CookieControl.Cookie.stopMutationObserver (uc.js:2:108346)
at signalWindowLoad (uc.js:2:61112)

function solaron_add_cookie_consent_script() {
echo '

<script>

</script>

';
}
add_action('wp_head', 'solaron_add_cookie_consent_script');

// Add Google Tag Manager script after the opening body tag
function solaron_add_gtm_script() {
echo "

<script>
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-N2WK2N2P');
</script>

";
}
add_action('wp_body_open', 'solaron_add_gtm_script');

Assistant
The error **"Uncaught SyntaxError: Failed to execute 'insertBefore' on 'Node': Invalid or unexpected token"** usually indicates a problem with dynamically inserted scripts. Given that the issue occurs in `uc.js`, which is part of **Cookiebot**, it is likely caused by how Cookiebot is handling script injections.

### Possible Causes & Solutions:

#### 1. **Escaping Issue in GTM Script**
Your **Google Tag Manager (GTM)** script uses **single quotes (`'`) inside a double-quoted string (`"`)**, which can sometimes cause syntax errors, especially in PHP. Try changing it to **heredoc syntax** or properly escape the quotes:

##### ✅ Fixed GTM Script (Using `heredoc` for better readability)

function solaron_add_gtm_script() {
    echo <<<EOT
    <!-- Google Tag Manager -->
    <script>
    (function(w,d,s,l,i){
        w[l]=w[l]||[];
        w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});
        var f=d.getElementsByTagName(s)[0],
        j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';
        j.async=true;
        j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;
        f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-N2WK2N2P');
    </script>
    <!-- End Google Tag Manager -->
    EOT;
}
add_action('wp_body_open', 'solaron_add_gtm_script');

#### 2. **Cookiebot's Script Blocking**
Since Cookiebot blocks scripts based on user consent, **it may be interfering with GTM or other JavaScript execution**. To fix this:

– Ensure that the **Google Tag Manager script is not being blocked** by Cookiebot.
– Modify Cookiebot settings in the **Cookiebot dashboard** to whitelist GTM.
– Try adding `data-cookieconsent="ignore"` to the GTM script:

function solaron_add_gtm_script() {
    echo <<<EOT
    <!-- Google Tag Manager -->
    <script data-cookieconsent="ignore">
    (function(w,d,s,l,i){
        w[l]=w[l]||[];
        w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});
        var f=d.getElementsByTagName(s)[0],
        j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';
        j.async=true;
        j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;
        f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-N2WK2N2P');
    </script>
    <!-- End Google Tag Manager -->
    EOT;
}

#### 3. **Check for Console Errors in DevTools**
– Open **Chrome DevTools (F12) → Console** and check for other JavaScript errors.
– If Cookiebot is **blocking GTM**, you may see an error like:

Cookiebot: GTM script is blocked due to missing consent.

– In this case, manually allow GTM in **Cookiebot's settings**.

#### 4. **Try Using the Recommended Cookiebot GTM Integration**
Instead of injecting GTM manually, Cookiebot provides an official GTM integration. Try following their [official GTM integration guide](https://support.cookiebot.com/hc/en-us/articles/360003793854-Google-Tag-Manager-GTM-integration).

### Summary
✅ **Fix the GTM script syntax** (use heredoc or escape quotes).
✅ **Check Cookiebot settings** to ensure GTM isn't blocked.
✅ **Use `data-cookieconsent="ignore"`** if necessary.
✅ **Check DevTools Console** for additional errors.

Let me know if the issue persists! 🚀

Leave a Comment

Your email address will not be published. Required fields are marked *