Faq How can avoid to double fire Google Analytics/Tag Manager?
How can avoid to double fire Google Analytics/Tag Manager?

A well known side effect of blocking Google Analytics/Tag Manager could be that after confirming the cookie consent the page is reloaded. This page reload could cause a URL to be submitted twice, it means: PageViews in GA are all double counted because the page is reloaded.

To avoid this issue it's enough to keep all auto reloads off, although in this way the first page on which the user lands won't be tracked if the Google Analytics/Tag Manager script is blocked by default.

To restore the normal behavior of Google Analytics/Tag Manager, in the simplest case it's recommended to exclude Google Analytics/Tag Manager and use it with the anonymization mode to still be compliant with the cookie law: https://storejextensions.org/faq/how-do-i-exclude-google-analytics-from-being-blocked.html

As an altenative, it's possible to use a more advanced approach:

  • Create a cookie accepted variable in the Google Tag Manager as described here: https://securiti.ai/cookie-consent-integrations/google-tag-manager/ so that it works based on the GDPR cookie 'cookieconsent_status' = 'allow'

  • OR

  • Use a bit of custom Javascript executed when the 'Allow cookie' button is clicked so that a GTM JavaScript event (callback) is trigged to enable or load it

For example:

const script = document.createElement('script');
script.src = `https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX`;
script.async = true;
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');

That can be added to custom scripts for category 3 'Statistics cookies':

Google Analytics