orestbida / cookieconsent

:cookie: Simple cross-browser cookie-consent plugin written in vanilla js
https://playground.cookieconsent.orestbida.com/
MIT License
3.91k stars 407 forks source link

[Bug]: Not all scripts in the page are loaded after giving consent #570

Closed ioweb-gr closed 1 year ago

ioweb-gr commented 1 year ago

Expected Behavior

All scripts are triggered based on their category

Current Behavior

In my page I have multiple scripts like this

    <script type="text/plain" data-cookiecategory="marketing">
        try {

            <!-- Event snippet for Purchase_sale conversion page -->
            setTimeout(function () {
                gtag('event', 'conversion', {
                    'send_to': 'AW-XXXXXX',
                    'value': 17.48,
                    'currency': 'EUR',
                    'transaction_id': 'XXXXXXXXX'
                });
            }, 2000)

        } catch (e) {
            console.log(e);
        }
    </script>

However, I notice not all of them trigger after accepting the cookies and the selected categories. Some of them are, some of them are staying as text/plain

Steps to reproduce

I'm not sure how to reproduce that and looking for ideas on what to check so that I can get to the bottom of this.

Proposed fix or additional info.

No response

Version

v2.9.2

On which browser do you see the issue?

Chrome

ioweb-gr commented 1 year ago

As a notice, the scripts that aren't loaded appear to be the ones located in the page after the initCookieConsent function invocation.

What's more weird, is that it's selecting items like this script[data-cookiecategory] and it's finding only the first 4 elements, but when I do the same in the inspector it finds 6+ items.

ioweb-gr commented 1 year ago

After adding the init on dom loaded event , it started catching all the scripts. So order priority matters.

document.addEventListener('DOMContentLoaded', function() {
  //init cookie consent
});
orestbida commented 1 year ago

Hi @ioweb-gr,

script order doesn't really matter; you just need to make sure the plugin is loaded after the page is fully fetched. You can do this by using the defer keyword (for external scripts) or your approach for inline scripts.