rc-consent
v1.2.0
Published
Cookie consent a Vanilla JS plugin which meets General Data Protection Regulations (GDPR).
Downloads
50
Maintainers
Readme
Cookie Consent
A Vanilla JS plugin which meets General Data Protection Regulations (GDPR)
bower install rc-consent
npm install rc-consent
<script type="text/javascript" src="dist/rc-consent.js"></script>
<script type="text/javascript" id="rcc_consent_GA">
window.addEventListener("DOMContentLoaded", function() {
window.rcc.addProvider({
id: 'ga',
trackingId: 'UA-XXXXX-Y',
anonymizeIp: true,
load: function() {
if (typeof ga === 'undefined') {
(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');
}
},
onInitialise: function (rcc, status) {
console.log('GA Consent init');
if (rcc.hasConsented(this.category)) {
console.log('hasConsented: true');
ga('create', this.trackingId, 'auto');
ga('set', 'anonymizeIp', this.anonymizeIp);
ga('send', 'pageview');
}
else {
console.log('hasConsented: false');
}
},
onAllow: function (rcc) {
console.log('GA consent allow:' + this.category);
this.load();
ga('create', this.trackingId, 'auto');
ga('set', 'anonymizeIp', this.anonymizeIp);
ga('send', 'event', 'Cookie Consent', 'Accepted');
ga('send', 'pageview');
},
onRevoke: function (rcc) {
console.log('GA consent revoke:' + this.category);
this.load();
ga('create', this.trackingId, {'storage': 'none'});
ga('send', 'event', 'Cookie Consent', 'Declined');
}
});
});
</script>
<script>
window.addEventListener("DOMContentLoaded", function() {
window.rcc.initialise({
cookie: {name: 'consent'},
defaultStatus: {required: true}
});
});
</script>
//Simple example jQuery with modal foundation
(function($, rcc) {
"use strict";
var $consentModal = $('#consentModal');
var $consentModalInstance = new Foundation.Reveal($consentModal);
//Check has consented
$( document ).ready(function() {
if (!rcc.hasConsented()) {
$consentModal.foundation('open');
}
});
//On Submit close modal
$('#consent_form').submit(function (e) {
rcc.setConsent($consentModal.get(0));
$consentModal.foundation('close');
return false;
});
//Init form field on Modal Open
$consentModal.on('open.zf.reveal', function () {
rcc.setForm($consentModal.get(0));
});
})(jQuery, rcc);