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.

1. Video presentation

2. Google Analytics and Facebook integration in custom mode

B - Locate Google Analytics and Facebook Pixel in your website

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

C - Trigger it with Axeptio

We're going to transform the script and encapsulate it in a function that will launch the script. First for Google Analytics :
1
function launchGA(){
2
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
3
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
4
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
5
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
6
ga('create', 'UA-00000000-1', 'auto');
7
ga('send', 'pageview');
8
}
Copied!
Then for Facebook Pixel :
1
function launchGoogleAnalytics(){
2
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
3
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
4
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
5
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
6
ga('create', 'UA-00000000-1', 'auto');
7
ga('send', 'pageview');
8
}
9
Copied!
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
1
void 0 === window._axcb && (window._axcb = []);
2
window._axcb.push(function(axeptio) {
3
axeptio.on("cookies:complete", function(choices) {
4
if(choices.facebook_pixel) {
5
launchFB();
6
}
7
if(choices.google_analytics) {
8
launchGoogleAnalytics();
9
}
10
});
11
});
Copied!
We will now just add the full script right after the SDK which will look like this
1
<script type="text/javascript">
2
//Le sdk qui permet de lancer le widget Axeptio et de sauvegarder les acceptations
3
var el = document.createElement('script');
4
el.setAttribute('src', 'https://static.axept.io/sdk.js');
5
el.setAttribute('type', 'text/javascript');
6
el.setAttribute('async', true);
7
el.setAttribute('data-id', '5da587fd629c0c21f3249270');
8
el.setAttribute('data-cookies-version', 'croquorico_VersionProdFR');
9
if (document.body !== null) {
10
document.body.appendChild(el);
11
}
12
13
// La fonction qui permet de lancer Google Analytics
14
function launchGoogleAnalytics(){
15
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
16
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
17
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
18
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
19
ga('create', 'UA-124590402-1', 'auto');
20
ga('send', 'pageview');
21
}
22
23
// La fonction qui permet de lancer le Facebook Pixel
24
function launchFB(){
25
!function(f,b,e,v,n,t,s){if(f.fbq)return;n=f.fbq=function(){n.callMethod?
26
n.callMethod.apply(n,arguments):n.queue.push(arguments)};if(!f._fbq)f._fbq=n;
27
n.push=n;n.loaded=!0;n.version='2.0';n.queue=[];t=b.createElement(e);t.async=!0;
28
t.src=v;s=b.getElementsByTagName(e)[0];s.parentNode.insertBefore(t,s)}(window,
29
document,'script','https://connect.facebook.net/en_US/fbevents.js');
30
31
fbq('init', '1380554325421888');
32
fbq('track', "PageView");
33
34
}
35
36
// La fonction qui relie Axeptio au lancement du cookie le Facebook Pixel et à Google Analytics
37
38
void 0 === window._axcb && (window._axcb = []);
39
window._axcb.push(function(axeptio) {
40
axeptio.on("cookies:complete", function(choices) {
41
if(choices.facebook_pixel) {
42
launchFB();
43
}
44
if(choices.google_analytics) {
45
launchGoogleAnalytics();
46
}
47
});
48
});
49
</script>
Copied!
Congrats ! Now your Google Analytics and Facebook Pixel cookies only deposit if the user agrees to them
Last modified 8mo ago