(function (){
if(typeof window.EMHDLT==='undefined'){
return;
}
var cfg=window.EMHDLT;
var started=false;
var heartbeatTimer=null;
var pendingKey=String(cfg.storageKey||'emh_dlt_pending_code');
var consentStateOverride=null;
function safeStorage(){
try {
return window.sessionStorage||window.localStorage;
} catch (e){
return null;
}}
function storePendingCode(code){
var storage=safeStorage();
if(!storage||!code) return;
try {
storage.setItem(pendingKey, code);
} catch (e){}}
function getPendingCode(){
var storage=safeStorage();
if(!storage) return '';
try {
return storage.getItem(pendingKey)||'';
} catch (e){
return '';
}}
function clearPendingCode(){
var storage=safeStorage();
if(!storage) return;
try {
storage.removeItem(pendingKey);
} catch (e){}}
function parseConsentCookie(){
var cookieName=String(cfg.cookieYesCookieName||'cookieyes-consent').replace(/[.*+?^${}()|[\\]\]/g, '\\$&');
var match=document.cookie.match(new RegExp('(?:^|;)' + cookieName + '=([^;]+)'));
if(!match){
return {};}
var raw=decodeURIComponent(match[1]||'');
var out={};
raw.split(',').forEach(function (segment){
var idx=segment.indexOf(':');
if(idx===-1) return;
var key=segment.slice(0, idx).trim();
var val=segment.slice(idx + 1).trim().toLowerCase();
out[key]=val;
});
return out;
}
function cookieYesPresent(){
return !!(
window.cookieyes ||
window.CookieYes ||
document.getElementById('cookie-law-info-bar') ||
document.querySelector('[data-cky-tag]') ||
document.querySelector('script[src*="cookieyes" i], script[id*="cookieyes" i]') ||
Number(cfg.cookieYesDetectedServer||0)===1
);
}
function hasAnalyticsConsent(){
if(consentStateOverride!==null){
return !!consentStateOverride;
}
var consent=parseConsentCookie();
if(consent.consent==='yes'&&(consent.analytics==='yes'||consent.performance==='yes')){
return true;
}
if(cookieYesPresent()){
return false;
}
return true;
}
function post(url, payload, keepalive){
return fetch(url, {
method: 'POST',
credentials: 'same-origin',
keepalive: !!keepalive,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
}).then(function (res){
return res.json().catch(function (){ return {};}).then(function (data){
if(!res.ok){
var err=new Error('Request failed');
err.response=data;
throw err;
}
return data;
});
});
}
function sendHeartbeat(type, seconds){
if(!started||!cfg.sourceId||!cfg.session||!cfg.visit||!hasAnalyticsConsent()){
return;
}
post(cfg.endpoint, {
sourceId: cfg.sourceId,
session: cfg.session,
visit: cfg.visit,
page: window.location.href,
seconds: seconds||15,
type: type||'heartbeat'
}, type==='unload').catch(function (){});
}
function beginHeartbeats(){
if(heartbeatTimer){
clearInterval(heartbeatTimer);
}
heartbeatTimer=window.setInterval(function (){
sendHeartbeat('heartbeat', Math.max(1, Math.round((cfg.intervalMs||15000) / 1000)));
}, cfg.intervalMs||15000);
}
function resumeExistingTracking(){
if(started||!cfg.sourceId||!cfg.session||!cfg.visit||!hasAnalyticsConsent()){
return;
}
started=true;
beginHeartbeats();
sendHeartbeat('heartbeat', 5);
}
function startTrackingFromConsent(){
var code=cfg.trackingCode||getPendingCode();
if(started||!code||!hasAnalyticsConsent()){
return;
}
post(cfg.startEndpoint, {
code: code,
page: window.location.href
}).then(function (data){
if(!data||!data.ok){
return;
}
cfg.sourceId=data.sourceId||cfg.sourceId;
cfg.session=data.session||cfg.session;
cfg.visit=data.visit||cfg.visit;
cfg.trackingCode=code;
started=true;
clearPendingCode();
beginHeartbeats();
sendHeartbeat('heartbeat', 5);
}).catch(function (){});
}
function stopTracking(){
started=false;
if(heartbeatTimer){
clearInterval(heartbeatTimer);
heartbeatTimer=null;
}}
function clearTrackerCookies(){
['emh_dlt_source', 'emh_dlt_session', 'emh_dlt_visit'].forEach(function (name){
document.cookie=name + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/';
});
}
function applyConsentDetail(detail){
if(!detail||typeof detail!=='object'){
return;
}
if(Array.isArray(detail.accepted)){
consentStateOverride=detail.accepted.indexOf('analytics')!==-1||detail.accepted.indexOf('performance')!==-1;
return;
}
if(detail.analytics==='yes'||detail.performance==='yes'){
consentStateOverride=true;
return;
}
if(detail.analytics==='no'&&detail.performance==='no'){
consentStateOverride=false;
}}
function refreshTrackingState(){
if(cfg.trackingCode){
storePendingCode(cfg.trackingCode);
}
if(hasAnalyticsConsent()){
if(cfg.sourceId&&cfg.session&&cfg.visit){
resumeExistingTracking();
}else{
startTrackingFromConsent();
}}else{
stopTracking();
clearTrackerCookies();
}}
document.addEventListener('cookieyes_banner_load', function (){
window.setTimeout(refreshTrackingState, 250);
});
document.addEventListener('cookieyes_consent_update', function (eventData){
applyConsentDetail(eventData&&eventData.detail ? eventData.detail:null);
window.setTimeout(refreshTrackingState, 250);
window.setTimeout(refreshTrackingState, 1000);
});
document.addEventListener('DOMContentLoaded', refreshTrackingState);
window.setTimeout(refreshTrackingState, 300);
window.setTimeout(refreshTrackingState, 1200);
window.setTimeout(refreshTrackingState, 2500);
window.addEventListener('pageshow', refreshTrackingState);
document.addEventListener('visibilitychange', function (){
if(document.visibilityState==='visible'){
refreshTrackingState();
}});
window.addEventListener('beforeunload', function (){
if(!started||!cfg.sourceId||!cfg.session||!cfg.visit||!hasAnalyticsConsent()){
return;
}
sendHeartbeat('unload', 5);
});
})();