3

I have added cookiebot script into head tag,

here is the script

<script id="Cookiebot" src="https://consent.cookiebot.com/uc.js" data-cbid="0000-0000-0000-0000" data-blockingmode="auto" type="text/javascript"></script>

After adding this script, I am getting are many console errors,

Uncaught Error: Mismatched anonymous define() module: function ( $ ) {

Tried contacting support, but could not succeed in that, please advise me how to resolve this error,

Thanks in Advance!!

7
  • Cookiebot can be a drama, but that depends mainly by your project customizations / plugins... I have seen lot of issues in M1 projects, but thankfully didn't have any in M2 (recently added it in a 2.3 with no issues). How are you including it in the head... Through backoffice or inside some template? – Raul Sanchez Jun 15 '20 at 18:29
  • @RaulSanchez, I am including via backoffice, also tried with layout, but both are showing the same error – Manjunath Jun 16 '20 at 4:49
  • Which section? Head? or Footer? – Raul Sanchez Jun 16 '20 at 7:42
  • Tried in both, head as well footer, same issue wherever we add – Manjunath Jun 16 '20 at 7:43
  • Can you provide online url to check console? – Raul Sanchez Jun 16 '20 at 7:47
3
+100
  1. Create a JS file here replacing VENDOR/THEME with your theme name - app/design/frontend/VENDOR/THEME/Magento_Theme/web/js/cookiebot.js and populate it with the below content:
require([
    'https://consent.cookiebot.com/uc.js?cbid=00000000-0000-0000-0000-000000000000',
    'domReady!'
], function () {
    'use strict';
    console.log(Cookiebot);
});
  1. Create app/design/frontend/VENDOR/THEME/Magento_Theme/layout/default_head_blocks.xml with the following content:
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <script src="Magento_Theme::js/cookiebot.js"/>
    </head>
</page>
  1. Flush all caches

The network request to show it working: enter image description here

The console warning because I do not have a key: enter image description here

You can then access CookieBot with Cookiebot:

enter image description here

The Cookiebot script loads and constructs a client-side JavaScript object named 'Cookiebot' which exposes the public properties, methods, events and callback functions. For more info see their docs - https://www.cookiebot.com/en/developer/.

5
  • hi @Ben Crook, you mean i have to create new js file using requirejs? – Manjunath Jun 17 '20 at 4:31
  • @Manjunath Yeah, loading this outside of Require is the reason for your error. – Ben Crook Jun 17 '20 at 13:21
  • hi @Ben Crook, possible to update me the steps to create the file please? – Manjunath Jun 17 '20 at 16:21
  • Sure, I have added those steps now @Manjunath – Ben Crook Jun 17 '20 at 18:00
  • Thanks @Ben Crook, but i need small help, can we make enable/disable feature for this? like in admin we should able to control, as now we loaded from js, so its loading always unless we comment the lines. – Manjunath Jun 18 '20 at 16:40
1

You need to use requirejs concept for including this file .

Can you use following link for the same ?

https://webkul.com/blog/call-configurable-external-js-url-magento2/

4
  • hi @Parul, I am not including any external js, got the script from cookiebot and tried to add before the body end, but getting the errors in console. I hope the above answer is not useful for me. – Manjunath Jun 15 '20 at 7:20
  • yes but you added script js file that generally not allowing to accept jqury and thats why it give such a error – ParulThakkar Jun 15 '20 at 7:21
  • I am not created any js file, just added that script inside the footer section, what is the solution to avoid that, can you please update me clearly, not getting from the suggested answer, I just need to add above script to get cookie pop up – Manjunath Jun 15 '20 at 7:24
  • can you check my second answer ? – ParulThakkar Jun 16 '20 at 15:25
1

This line is worth a try:

<script src="https://consent.cookiebot.com/uc.js" id="Cookiebot" data-cbid="0000-0000-0000-0000" async defer></script>

then flush the cache ensure you are in developer mode: php bin/magento deploy:mode:set developer

if you have doubts whether the cache was not flushed, do ensure the cookie line is present or not.

6
  • hi @Herve, same error after adding this code – Manjunath Jun 16 '20 at 5:20
  • Have you flushed the cache? I’ll be around in 1h30 and can dig properly then – Herve Tribouilloy Jun 16 '20 at 6:04
  • yes i did cache flush, but still same error – Manjunath Jun 16 '20 at 6:18
  • hello @Herve did you ge time to check it? – Manjunath Jun 16 '20 at 8:55
  • yes, did not want to spoil the comments.. was keen to start a chat.. I have updated my answer too. Have you seen? – Herve Tribouilloy Jun 16 '20 at 8:56
0

enter image description hereCan you try in magento backend following thing ?

Admin >> content >> design >> configuration >> Select Store >> html HEAD/Footer (Miscellaneous HTML)

But this script will be displayed on all pages or else if you want to add on particular page than add following code in your layout xml file

    <referenceContainer name="before.body.end">
        <block class="Magento\Framework\View\Element\Template" template="Namespace_Modulename::before.phtml" name="before_body_js"/>
</referenceContainer>

add template file before.phtml add below code

<script src="https://consent.cookiebot.com/uc.js" id="Cookiebot" data-cbid="0000-0000-0000-0000" async defer></script>
3
0

Assumption:

  • You are using a Custom Magento Theme.
  • You need the Cookiebot JS in all Frontend pages.

(Step 1) Update Entries

If you already have a app/design/frontend/VENDOR/THEME/Magento_Theme/layout/default_head_blocks.xml file in place - add the code-snippet shown below to it:

Note: If you don't already have a default_head_blocks.xml file in place - i.e. inside your Custom Theme - copy the file vendor/magento/theme-frontend-blank/Magento_Theme/layout/default_head_blocks.xml to the location app/design/frontend/VENDOR/THEME/Magento_Theme/layout/ and then add the following entry to the end of it:

<link src="js/includes/Cookiebot.js"/>

(Step 2) Create JS Payload File

Create the File app/design/frontend/VENDOR/THEME/web/js/includes/Cookiebot.js with the following content:

require([
    'jquery',
    'https://consent.cookiebot.com/uc.js?cbid=00000000-0000-0000-0000-000000000000'
], function ($) {
    'use strict';

    $(document).ready(function() {
        console.log(">>> CookieBot has been initialized ...");
    });
});

Step 3) Clear Caches:

Depending on the kind of caching-mechanisms you have deployed on your server (File/Varnish/Redis/etc) you may need to run some or all of these:

Clear Magento-Caches:

bin/magento cache:flush
bin/magento cache:clean

Remove Cached-Files:

# TO-DO: Please replace $MAGENTO_HOME/ - in the commands below - with the path to your Magento-Home-Folder on the server
rm -rfv $MAGENTO_HOME/var/cache/
rm -rfv $MAGENTO_HOME/var/page_cache/
rm -rfv $MAGENTO_HOME/var/session/*
rm -rfv $MAGENTO_HOME/generated/*

Remove Static-Content:

# TO-DO: Please replace $MAGENTO_HOME/ - in the commands below - with the path to your Magento-Home-Folder on the server
rm -rfv $MAGENTO_HOME/pub/static/frontend
rm -rfv $MAGENTO_HOME/pub/static/deployed_version.txt
rm -rfv $MAGENTO_HOME/pub/static/adminhtml

Remove Cached Content in your Redis:

# To Check if Redis is Running
sudo systemctl status redis.service ; 

# To Check Restart Redis
sudo systemctl restart redis.service ; 

# To Check if Redis is back up and running
# Hint: The PID in Previous and Current `Status` commands shoudl be different.
sudo systemctl -l status redis.service ;

Remove Cached Content in your Varnish:

# To Check if Varnish is Running on your Server
sudo systemctl status varnish.service ; 

# To Restart Redis on your Server
sudo systemctl restart varnish.service ; 

# To Check if Varnish is back up and running
# Hint: The PID in Previous and Current `Status` commands should be different.
sudo systemctl -l status varnish.service ;

Step 4) Re-deploy

Execute the following CLI Commands:

# IF you're running `Developer Mode` use the command below:
bin/magento setup:static-content:deploy -f --strategy standard

# IF you're running `Production Mode` use the command below:
bin/magento setup:static-content:deploy

# If you have `Developer Mode` Enabled - you shouldn't really need to run this. 
# But, if you're running `Production Mode` - you should do so.
bin/magento setup:di:compile

Step 5) Clear Your Browser Caches

To avoid the possibility of Browser-Caching issues - please follow the Instructions specific to your Browser to clear It's Caches.

Step 6) Test on Frontend

Open a New Browser Window (Preferrably in Incognito-Mode) and go to your Magento Instances HomePage.

  • VALIDATION CRITERIA # 1:

Check the Homepage's source code - i.e. on your Browser - for the following Code Snippet:

<link src="js/includes/Cookiebot.js"/>
  • VALIDATION CRITERIA # 2:

Once the Homepage has finished loading - Check the Browsers Console for following Log message:

>>> CookieBot has been initialized ...

At this point, your Cookiebot snippet should be working as expected.

I hope this helps!

Cheers,

Sharath

4
  • can we handle it from admin side? instead of files – Manjunath Jun 19 '20 at 4:33
  • @Manjunath: Please try this: 1. Login to Magento Admin Admin Panel -> Content > Design : Configuration. 2. Click on the "Edit" link of the Row that corresponds to the StoreView you want the head tag to be changed on OR Select Global if you need this change on EVERY StoreView. 3. Find the HTML Head section and add your JS code in the Scripts and Style Sheets field of that section/form 4. Once the desired changes have been made, click the Save Configuration button to commit these changes into the Database layer. 5. Flush Magento Cache(s). 6. Test on the Frontend. – Sharath Kumar Jun 22 '20 at 21:40
  • hi @Sharath if editedd from admin section, there will be a console errors – Manjunath Jun 23 '20 at 4:54
  • @Manjunath Could you post screenshot / StackTrace of the error(s) you run into ? – Sharath Kumar Jun 23 '20 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.