-
-
Save ajmalafif/de6206946f7cd41971b78f4f3b975c69 to your computer and use it in GitHub Desktop.
Intrusive GDPR-dialog script. Variables are used by Statamic, so you might need to define your own. Uses a11y-dialog, linked at the top.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdn.jsdelivr.net/npm/[email protected]/a11y-dialog.min.js"></script> | |
<script async src="https://www.googletagmanager.com/gtag/js?id=ADD-ID-HERE"></script> | |
<script> | |
'use strict'; | |
var cookieName = 'company-gdpr'; | |
var redirect = '{{ gdpr_cta_decline_url }}'; // Redirect for decline | |
var expiration = {{ gdpr_expiry_time }}; // Expiration time in days | |
var animDuration = 200; // Animation duration in milliseconds | |
var animDelay = 1000; // Animation duration in milliseconds | |
window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; | |
function initGA() { | |
window.dataLayer = window.dataLayer || []; | |
function gtag(){dataLayer.push(arguments);} | |
gtag('js', new Date()); | |
gtag('config', 'ADD-ID-HERE'); | |
} | |
$(document).ready(function () { | |
var dialogEl = document.getElementById('gdpr'); | |
var mainEl = document.getElementById('main'); | |
if (dialogEl) { | |
var keypressHandler = window.A11yDialog.prototype._bindKeypress; | |
var dialog = new window.A11yDialog(dialogEl, mainEl); | |
dialog._bindKeypress = function (event) { | |
// Capture escape key. | |
if (event.key === "Escape" || event.keyCode === 27) return; | |
keypressHandler.bind(undefined, event); | |
}; | |
var hideDialog = function hideDialog(element) { | |
$(dialogEl).fadeOut(animDuration, function () { | |
dialog.hide(); | |
}); | |
}; | |
var clearCookies = function clearCookies() { | |
$.each(Cookies.get(), function (key) { | |
return Cookies.remove(key); | |
}); | |
}; | |
dialog.on('show', function (dialogEl, triggerEl) { | |
$(dialogEl).children(".dialog-wrapper").hide(); | |
setTimeout(function () { | |
$(dialogEl).children(".dialog-wrapper").fadeIn(animDuration); | |
setTimeout(function () { | |
$('#gdpr .dialog-content > div').scrollTop(0); | |
}, 0); | |
}, animDelay); | |
// Once the accept button is clicked, | |
// set the cookie and close dialog | |
$(".dialog-accept").click(function (e) { | |
e.preventDefault(); | |
Cookies.set(cookieName, true, { | |
expires: expiration, | |
secure: true | |
}); | |
hideDialog(dialogEl); | |
}); | |
$(".dialog-decline").click(function (e) { | |
e.preventDefault(); | |
hideDialog(dialogEl); | |
}); | |
}); | |
dialog.on('hide', function () { | |
$('body').removeClass('overflow-hidden'); | |
// The dialog was closed without cookie set | |
if (!Cookies.get(cookieName)) { | |
clearCookies(); | |
// Redirect to new page on decline | |
window.location = redirect; | |
} | |
else { | |
initGA(); | |
} | |
}); | |
// To manually control the dialog: | |
if (!Cookies.get(cookieName)) { | |
dialog.show(); | |
$('body').addClass('overflow-hidden'); | |
} | |
else { | |
initGA(); | |
} | |
} | |
// Clear cookies when button with class .gdpr-clear is pushed | |
$(".gdpr-clear").click(function (e) { | |
e.preventDefault(); | |
clearCookies(); | |
// Redirect to new page on decline | |
window.location = redirect; | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment