I decided to post some help on this script because I really struggled find a EU Cookie Law Free script that works with an Opt-in navbar that lets the user activate Google Analytics.

The original script comes from:

https://cookieconsent.insites.com

but I have seen many people struggling with it, this is a working solution.

By using this script, your user will have to click on “Allow cookies” to activate the Google analytics on your website(or any other types of third party scripts).

If the user clicks on “Decline”, Google analytics tracking will be disabled. If nothing if clicked, the analytics will be disabled as default.

Do not forget to create a Google analytic account.

Just below the above code add the below code:

<!DOCTYPE html>
<html lang="en">


<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">


<title>Demo 5 - Opt-in notification with Cookie Consent</title>
<meta name="description" content="An example of an opt out Cookie Notice using Cookie Consent" />
<link rel="stylesheet" type="text/css"
href="http://cdnjs.cloudflare.com/ajax/libs/cookieconsent2/3.0.3/cookieconsent.min.css" />


<body class="example5" role="document">


<div class="center-outer fit">
<div class="center-inner">
<div class="example-window" role="main">
<h1>
<span>Demo</span> Opt-in</h1>
<ul class="example-selector"></ul>
</div>
</div>
</div>
<script async src="https://www.googletagmanager.com/gtag/js?id=120233746-1"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/cookieconsent.min.js" data-cfasync="false"></script>
<script>
function startAnalytics() {
window.dataLayer = window.dataLayer || [];


function gtag() {
dataLayer.push(arguments);
}
window['ga-disable-UA-xxxxxxxxx-x'] = false;
gtag('config', 'UA-xxxxxxxxx-x', { // Set your Google UA code here
'anonymize_ip': true
});
}


function stopAnalytics() {
window['ga-disable-UA-xxxxxxxxx-x'] = true; // Set your Google UA code here
deleteCookie('_ga');
deleteCookie('_gat_gtag_UA_xxxxxxxxx-x'); // Set your Google UA code here
deleteCookie('_gid');
}


function setCookie(name, value, days) {
var d = new Date;
d.setTime(d.getTime() + 24*60*60*1000*days);
document.cookie = name + "=" + value + ";domain=yourdomain.com;path=/;expires=" + d.toGMTString(); // Set your domain name here
}


function deleteCookie(name) { setCookie(name, '', -1); }


cookieconsent.initialise({
type: 'opt-out',
palette: {
"popup": {
"background": "#eaf7f7",
"text": "#5c7291"
},
"button": {
"background": "#56cbdb",
"text": "#ffffff"
},
},
content: {
header: 'Cookies used on the website',
message: 'This website uses cookies to ensure you get the best experience on our website.',
deny: 'Decline',
allow: 'Allow cookies',
link: 'Learn more',
href: 'https://YourWebsite.com/privacy-policy',
close: '❌'
},
onInitialise: function (status) {
var didConsent = this.hasConsented();


if (status == 'allow' && didConsent) {
startAnalytics();
}
if (status == 'deny' && !didConsent) {
stopAnalytics();
}
},
onStatusChange: function (status, chosenBefore) {
var didConsent = this.hasConsented();

if (status == 'allow' && didConsent) {
startAnalytics();
}
if (status == 'deny' && !didConsent) {
stopAnalytics();
}
},
onRevokeChoice: function () {


if (status == 'allow') {
startAnalytics();
}
if (status == 'deny') {
stopAnalytics();
}
}
});
</script>

If this helped you please leave us a message!

Click here to add your own text