codeninjasuk.github.io/_includes/cookie.html

67 lines
2.1 KiB
HTML
Raw Permalink Normal View History

2023-07-27 15:28:47 -04:00
<!-- Only add all this in production, why would I analyse myself when writing the post anyway. -->
<!-- Add cookie consent css & js -->
<link rel="stylesheet"
type="text/css"
href="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.css"/>
<script src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js"
data-cfasync="false"></script>
<!-- Add google analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id={{site.ganalytics}}"></script>
<script>
// Reads the value of a cookie by name or returns empty string
function getCookie(name) {
var b = document.cookie.match('(^|[^;]+)\\s*' + name + '\\s*=\\s*([^;]+)');
return b ? b.pop() : '';
}
// Actually starting analytics up
function addAnalytics() {
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', '{{site.ganalytics}}');
}
// Waiting for the load event
window.addEventListener("load", function () {
// Reading "cookieconsent_status" cookie
const cookieConsent = getCookie('cookieconsent_status');
// Start analytics if user consented or did not deny
if (cookieConsent === 'allow' || cookieConsent === '') {
addAnalytics();
}
// Initialise cookie consent banner
window.cookieconsent.initialise({
"palette": {
"popup": {
"background": "#efefef",
"text": "#404040"
},
"button": {
"background": "#8ec760",
"text": "#ffffff"
}
},
"type": "opt-out",
"content": {
"allow": "Approve",
"dismiss": "Approve",
"deny": "Reject"
},
// Reload the page on user choice to make sure cookie is set
onStatusChange: function (status, chosenBefore) {
location.reload();
}
})
});
</script>