First Party script voor verlagen unassigned shopify
<script type="text/javascript">
(function() {
'use strict';
// Generate UUID v4 (compatible method)
function mkTagging_generateUUID() {
// Fallback for older browsers without crypto API
if (typeof crypto !== 'undefined' && crypto.getRandomValues) {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, function(c) {
return (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16);
});
} else {
// Less secure fallback for very old browsers
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0;
var v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
}
// Get cookie value by name
function mkTagging_getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
// Set cookie with expiration
function mkTagging_setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
// Set both path and SameSite for better compatibility
document.cookie = name + "=" + value + expires + "; path=/; SameSite=Lax";
}
// Safe localStorage wrapper
function mkTagging_getLocalStorage(key) {
try {
if (typeof Storage !== 'undefined' && window.localStorage) {
return localStorage.getItem(key);
}
} catch (e) {
// localStorage might be blocked or unavailable
console.log('localStorage not available:', e);
}
return null;
}
function mkTagging_setLocalStorage(key, value) {
try {
if (typeof Storage !== 'undefined' && window.localStorage) {
localStorage.setItem(key, value);
return true;
}
} catch (e) {
// localStorage might be blocked or unavailable
console.log('localStorage not available:', e);
}
return false;
}
// Main initialization function
function mkTagging_initVisitorId() {
var cookieName = '_taggingmk';
var storageKey = '_taggingmk_backup';
var visitorId = null;
// 1. Check if cookie exists
visitorId = mkTagging_getCookie(cookieName);
// 2. If no cookie, check localStorage backup
if (!visitorId) {
visitorId = mkTagging_getLocalStorage(storageKey);
// If found in localStorage but not in cookie, restore the cookie
if (visitorId) {
mkTagging_setCookie(cookieName, visitorId, 365);
console.log('MK Tagging: Visitor ID restored from localStorage:', visitorId);
}
}
// 3. If still no ID, generate a new one
if (!visitorId) {
visitorId = mkTagging_generateUUID();
mkTagging_setCookie(cookieName, visitorId, 365);
console.log('MK Tagging: New visitor ID generated:', visitorId);
}
// 4. Always sync to localStorage as backup (if available)
if (mkTagging_setLocalStorage(storageKey, visitorId)) {
console.log('MK Tagging: Visitor ID backed up to localStorage');
}
// Return the visitor ID for potential use
return visitorId;
}
// Run on DOM ready or immediately if already loaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', mkTagging_initVisitorId);
} else {
// DOM is already loaded
mkTagging_initVisitorId();
}
// Create a namespaced object for any exposed functionality
window.mkTagging = window.mkTagging || {};
window.mkTagging.getVisitorId = function() {
return mkTagging_getCookie('_taggingmk') || mkTagging_getLocalStorage('_taggingmk_backup');
};
})();
</script>
Bijgewerkt op: 25/08/2025
Dankuwel!