initial commit for segment introduction (#998)

* initial commit for segment introduction

* conditionals to check for window.analytics
This commit is contained in:
jasonbryant84
2024-07-02 23:51:14 +02:00
committed by GitHub
parent 87d9b71fbd
commit c22a4ceb61
9 changed files with 306 additions and 30 deletions
@@ -1,3 +1,6 @@
import { getCookie, setCookie } from './helpers';
import { loadSegment, handleConsent } from './segment-helpers';
(function () {
window.cookit = function (options) {
// SETTINGS
@@ -56,32 +59,13 @@
// EVENT LISTENER (click)
button.addEventListener('click', () => {
if (!window.analytics) {
loadSegment();
handleConsent();
}
banner.remove();
setCookie('cookie-consent', 1, settings.lifetime);
});
// GET COOKIE
function getCookie(name) {
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
name = name + '=';
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1);
}
if (c.indexOf(name) === 0) {
return c.substring(name.length, c.length);
}
}
}
// SET COOKIE
function setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
const expires = 'expires=' + date.toUTCString();
document.cookie = name + '=' + value + ';' + expires + ';path=/;Secure';
}
};
})();