Skip to content

Commit

Permalink
Merge pull request #309 from City-of-Helsinki/matomo-logic-fix
Browse files Browse the repository at this point in the history
fix: refactoring matomo logic
  • Loading branch information
jannetalvio committed Nov 29, 2023
2 parents b5c991f + 0b55961 commit 53e4c60
Showing 1 changed file with 63 additions and 58 deletions.
121 changes: 63 additions & 58 deletions hkm/static/hkm/js/cookie-consent.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,78 @@
(function($) {
var cookieName = "hki_consent"
var cookie = $.cookie(cookieName)
var _paq = window._paq = window._paq || [];
(function ($) {
var cookieName = "hki_consent";
var cookie = $.cookie(cookieName);
var _paq = (window._paq = window._paq || []);

const $cookieConsentContainer = $("#cookie-consent"),
const $cookieConsentContainer = $("#cookie-consent"),
$readMoreBtn = $("#cookie-consent__readmore-btn"),
$cookieContents = $("#cookie-consent__content"),
$acceptBtn = $("#consent_accept"),
$rejectBtn = $("#consent_reject"),
$consentLink = $("#cookie-consent-link");

// if rejected
if(cookie && cookie === 0) {
_paq.push(['forgetCookieConsentGiven']);
_paq.push(['requireCookieConsent']);
}

// if accepted
if(cookie && cookie === 1) {
_paq.push(['setCookieConsentGiven']);
}
// always require on load
_paq.push(["requireCookieConsent"]);
_paq.push(["requireConsent"]);

if(!cookie && _paq) {
_paq.push(['requireCookieConsent']);
// if rejected
if (cookie && cookie === 0) {
_paq.push(["forgetCookieConsentGiven"]);
_paq.push(["forgetConsentGiven"]);
}

$cookieConsentContainer.fadeIn();
$cookieConsentContainer.animate({ "bottom": "0" }, "slow" );
// if accepted
if (cookie && cookie === 1) {
_paq.push(["rememberConsentGiven"]);
_paq.push(["rememberCookieConsentGiven"]);
}

$readMoreBtn.on('click', function (e) {
$readMoreBtn.hide();
$cookieContents.show();
});
}
if (!cookie) {
$cookieConsentContainer.fadeIn();
$cookieConsentContainer.animate({ bottom: "0" }, "slow");

$cookieConsentContainer.on('click', function (e) {
$readMoreBtn.hide();
$cookieContents.show();
$readMoreBtn.on("click", function (e) {
$readMoreBtn.hide();
$cookieContents.show();
});
}

$acceptBtn.on('click', function (e) {
if(_paq) {
_paq.push(['setCookieConsentGiven']);
$.cookie(cookieName, 1, { expires : 393 });
$cookieConsentContainer.fadeOut();
}
});

$rejectBtn.on('click', function (e) {
if(_paq) {
_paq.push(['forgetCookieConsentGiven']);
$.cookie(cookieName, 0, { expires : 393 });
$cookieConsentContainer.fadeOut();
}
});
$cookieConsentContainer.on("click", function (e) {
$readMoreBtn.hide();
$cookieContents.show();
});

// click cookie consent footer navi link to force open
$consentLink.on('click', function (e) {
e.preventDefault()
$cookieConsentContainer.fadeIn();
$cookieConsentContainer.animate({ "bottom": "0" }, "slow" );
$cookieContents.show();
$readMoreBtn.hide();
});
$acceptBtn.on("click", function (e) {
if (_paq) {
_paq.push(["rememberConsentGiven"]);
_paq.push(["rememberCookieConsentGiven"]);
$.cookie(cookieName, 1, { expires: 393 });
$cookieConsentContainer.fadeOut();
}
});

// click outside to collapse cookie consent
$(document).on('click', function (e) {
if ($(e.target).closest("#cookie-consent").length === 0) {
$cookieContents.hide();
$readMoreBtn.show();
}
});
})(jQuery);
$rejectBtn.on("click", function (e) {
if (_paq) {
_paq.push(["forgetConsentGiven"]);
_paq.push(["forgetCookieConsentGiven"]);
$.cookie(cookieName, 0, { expires: 393 });
$cookieConsentContainer.fadeOut();
}
});

// click cookie consent footer navi link to force open
$consentLink.on("click", function (e) {
e.preventDefault();
$cookieConsentContainer.fadeIn();
$cookieConsentContainer.animate({ bottom: "0" }, "slow");
$cookieContents.show();
$readMoreBtn.hide();
});

// click outside to collapse cookie consent
$(document).on("click", function (e) {
if ($(e.target).closest("#cookie-consent").length === 0) {
$cookieContents.hide();
$readMoreBtn.show();
}
});
})(jQuery);

0 comments on commit 53e4c60

Please sign in to comment.