Cookie Google Analytics

You only have a Google Analytics cookie and you don't want to use Google Tag Manager to install it.

A - Locate it on your website

Here is the code that activates Google Analytics and that you should have in your site Header

<script 

B - The Analytics function

We'll now transform the script and encapsulate it in a function that will launch the script (the launchGA() function).

function launchGA(){
var el = document.createElement('script');
el.setAttribute('type', 'text/javascript');
el.setAttribute('async', true);
el.setAttribute('src', '//www.googletagmanager.com/gtag/js?id=UA-234217-21');
document.body.append(el); window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-234217-21', { 'anonymize_ip': true });
gtag('config', 'UA-234217-21', {'link_attribution': true});
}

C - Integration on your website

You'll just have to add it

function launchGA(){
var el = document.createElement('script');
el.setAttribute('type', 'text/javascript');
el.setAttribute('async', true);
el.setAttribute('src', '//www.googletagmanager.com/gtag/js?id=UA-234217-21');
document.body.append(el); window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-234217-21', { 'anonymize_ip': true });
gtag('config', 'UA-234217-21', {'link_attribution': true});
}


// La fonction qui relie Axeptio au lancement du cookie Google Analytics
void 0 === window._axcb && (window._axcb = []);
window._axcb.push(function(axeptio) {
     axeptio.on("cookies:complete", function(choices) {
    if(choices.google_analytics) {
       launchGA();
      }
     });
});

Great ! Everything works 👌

Last updated