UNPKG

@mezereon/tracking

Version:

Tracking for Mezereon Smart Search & Filter

78 lines (73 loc) 1.98 kB
const Tracking = { visitTtl: 30, // 30 minutes visitorTtl: 2 * 365 * 24 * 60, // 2 years set: function (name, value, ttl, domain) { let expires = ''; let cookieDomain = ''; if (ttl) { let date = new Date(); date.setTime(date.getTime() + ttl * 60 * 1000); expires = '; expires=' + date.toGMTString(); } if (domain) { cookieDomain = '; domain=' + domain; } document.cookie = name + '=' + escape(value) + expires + cookieDomain + '; path=/; secure; SameSite=None'; }, get: function (name) { let i, c; let nameEQ = name + '='; let ca = document.cookie.split(';'); for (i = 0; i < ca.length; i++) { c = ca[i]; while (c.charAt(0) === ' ') { c = c.substring(1, c.length); } if (c.indexOf(nameEQ) === 0) { return unescape(c.substring(nameEQ.length, c.length)); } } return null; }, uuidv4: function () { var crypto = window.crypto || window.msCrypto; return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) => ( c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4))) ).toString(16) ); }, getVisitId: function () { var visitId = this.get('mz_visitId'); if (visitId !== null) { return visitId; } visitId = this.uuidv4(); this.set('mz_visitId', visitId, this.visitTtl); return visitId; }, getVisitorId: function () { var visitorId = this.get('mz_visitorId'); if (visitorId !== null) { return visitorId; } visitorId = this.uuidv4(); this.set('mz_visitorId', visitorId, this.visitorTtl); return visitorId; }, getClientId: function () { var clientId = this.get('_shopify_y'); if (clientId !== null) { return clientId; } return this.uuidv4(); }, }; module.exports = Tracking;