Google Analytics and facebook cookie in custom mode
You only have a Google Analytics cookie and don't want to use Google Tag to install it.
Here is the code that loads Google Analytics :
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');ga('create', 'UA-00000000-1', 'auto');ga('send', 'pageview');
Here is the code that loads Facebook Pixel :
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '159702201342042');
fbq('set','agent','tmgoogletagmanager', '159702201342042');
fbq('track', "PageView");
They're located in the site header and we must delete them.

We must delete or deactivate the code.
We're going to transform the script and encapsulate it in a function that will launch the script. First for Google Analytics :
function launchGA(){
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-00000000-1', 'auto');
ga('send', 'pageview');
}
Then for Facebook Pixel :
function launchGoogleAnalytics(){
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-00000000-1', 'auto');
ga('send', 'pageview');
}
Then we take back the function that allows the launch of scripts located in the Axeptio back office and we configure it so that the function will only be called if the user choose Facebook Pixel or Google Analytics
void 0 === window._axcb && (window._axcb = []);
window._axcb.push(function(axeptio) {
axeptio.on("cookies:complete", function(choices) {
if(choices.facebook_pixel) {
launchFB();
}
if(choices.google_analytics) {
launchGoogleAnalytics();
}
});
});
We will now just add the full script right after the SDK which will look like this
<script type="text/javascript">
//Le sdk qui permet de lancer le widget Axeptio et de sauvegarder les acceptations
var el = document.createElement('script');
el.setAttribute('src', 'https://static.axept.io/sdk.js');
el.setAttribute('type', 'text/javascript');
el.setAttribute('async', true);
el.setAttribute('data-id', '5da587fd629c0c21f3249270');
el.setAttribute('data-cookies-version', 'croquorico_VersionProdFR');
if (document.body !== null) {
document.body.appendChild(el);
}
// La fonction qui permet de lancer Google Analytics
function launchGoogleAnalytics(){
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-124590402-1', 'auto');
ga('send', 'pageview');
}
// La fonction qui permet de lancer le Facebook Pixel
function launchFB(){
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
document,'script','https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '1380554325421888');
fbq('track', "PageView");
}
// La fonction qui relie Axeptio au lancement du cookie le Facebook Pixel et à Google Analytics
void 0 === window._axcb && (window._axcb = []);
window._axcb.push(function(axeptio) {
axeptio.on("cookies:complete", function(choices) {
if(choices.facebook_pixel) {
launchFB();
}
if(choices.google_analytics) {
launchGoogleAnalytics();
}
});
});
</script>
Congrats ! Now your Google Analytics and Facebook Pixel cookies only deposit if the user agrees to them
Last modified 2yr ago