-
-
Notifications
You must be signed in to change notification settings - Fork 420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug]: Uncaught (in promise) TypeError: Cannot read properties of null (reading 'appendChild') #676
Comments
Also, if I run |
Should I move all my tags from |
By default the plugin is loaded asynchronously, but you can change this behaviour via the lazyHtmlGeneration option.
Yes, that's the expected behaviour; the only method that can be run before the plugin's own initialization is getCookie. You can access the acceptedCategories before the plugin's initialization like this: const acceptedCategories = CookieConsent.getCookie('categories', 'cc_cookie') || [];
// acceptedCategories.includes('analytics')
CookieConsent.run({...}); |
Maybe I'm not understanding correctly, CookieConsent.run({
autoShow: false,
lazyHtmlGeneration: true,
...
});
Thanks, I guess that will do, but I think it would be nice to decouple |
Let me know If you want me to close this or you'd like to keep it open for doc/exemple update. <script src="https://cdn.jsdelivr.net/gh/orestbida/cookieconsent@3.0.1/dist/cookieconsent.umd.js"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { window.dataLayer.push(arguments); }
gtag('consent', 'default', {
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
analytics_storage: 'denied',
functionality_storage: 'denied',
personalization_storage: 'denied',
security_storage: 'denied',
});
if(CookieConsent.getCookie('categories', 'cc_cookie')) {
const acceptedCategories = CookieConsent.getCookie('categories', 'cc_cookie') || [];
gtag('consent', 'update', {
functionality_storage: acceptedCategories.includes('functionality') ? 'granted' : 'denied',
ad_storage: acceptedCategories.includes('marketing') ? 'granted' : 'denied',
ad_user_data: acceptedCategories.includes('marketing') ? 'granted' : 'denied',
ad_personalization: acceptedCategories.includes('marketing') ? 'granted' : 'denied',
analytics_storage: acceptedCategories.includes('analytics') ? 'granted' : 'denied',
personalization_storage: acceptedCategories.includes('personalization') ? 'granted' : 'denied',
security_storage: acceptedCategories.includes('security') ? 'granted' : 'denied',
});
}
window.addEventListener('load', function () {
CookieConsent.run({
guiOptions: {
consentModal: {
layout: 'box wide',
position: 'bottom right',
equalWeightButtons: true,
flipButtons: true,
},
preferencesModal: {
layout: 'box',
position: 'right',
equalWeightButtons: true,
flipButtons: false,
},
},
categories: {
necessary: {
readOnly: true,
},
marketing: {},
analytics: {},
functionality: {},
personalization: {},
security: {},
},
language: { ... },
onFirstConsent: (...args) => {
manageConsent(args);
window.dataLayer.push({ event: 'consent_ready' });
},
onChange: (...args) => {
manageConsent(args);
window.dataLayer.push({ event: 'consent_update' });
},
});
});
function manageConsent() {
gtag('consent', 'update', {
functionality_storage: CookieConsent.acceptedCategory('functionality') ? 'granted' : 'denied',
ad_storage: CookieConsent.acceptedCategory('marketing') ? 'granted' : 'denied',
ad_user_data: CookieConsent.acceptedCategory('marketing') ? 'granted' : 'denied',
ad_personalization: CookieConsent.acceptedCategory('marketing') ? 'granted' : 'denied',
analytics_storage: CookieConsent.acceptedCategory('analytics') ? 'granted' : 'denied',
personalization_storage: CookieConsent.acceptedCategory('personalization') ? 'granted' : 'denied',
security_storage: CookieConsent.acceptedCategory('security') ? 'granted' : 'denied',
});
}
</script>
<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-XXXXXXXX');</script> |
Perhaps I wasn't clear, you should set We can leave this open, until there is a docs. section on how to set up GTM. |
@orestbida I tried Thanks again for your help |
Expected Behavior
I'm trying to implement CookieConsent with GTM Consent Mode.
Here is my config:
Current Behavior
I understand that the errors is because the plugin try to insert the banner into the DOM, while the page has not finished loading (DOMContentLoaded).
My issue is that if I put
CookieConsent.run(...)
inside awindow.addEventListener('load', () => { ... })
, theContainer Loaded
event will be fired before GTM receive the consent value fromgtag()
insideonConsent
, which mean Facebook Pixel is blocked even if the user clickedAccept All
.Steps to reproduce
.
Proposed fix or additional info.
Would it be possible to execute the "cc.run()" command as soon as possible, and only delay the insertion of the banner into the DOM once the page is loaded ? Currently, I need to wrap the whole
cc.run()
inside thewindow.addEventListener('load', () => { ... })
which delay the plugin initialization which does not require access to DOM and could be executed earlier.Version
3.0.1
On which browser do you see the issue?
Chrome
The text was updated successfully, but these errors were encountered: