(function(global){'use strict';function readCookie(name){const match=document.cookie.match(new RegExp('(?:^|; )'+name.replace(/([.*+?^${}()|[\]\\])/g,'\\$1')+'=([^;]+)'));return match?decodeURIComponent(match[1]):'';}
function parseClientIdFromGaCookie(value){if(!value)return'';if(/^\d+\.\d+$/.test(value))return value;const m=value.match(/^GA\d+\.\d+\.(\d+\.\d+)$/);return m?m[1]:'';}
function getCachedClientId(){try{return sessionStorage.getItem('unipixel_google_client_id')||'';}catch{return'';}}
function cacheClientId(clientId){try{if(clientId)sessionStorage.setItem('unipixel_google_client_id',clientId);}catch{}}
function resolveGoogleClientId(options){const{measurementId,maxAttempts=3,attemptDelayMs=150}=options||{};return new Promise((resolve)=>{const cached=getCachedClientId();if(cached)return resolve(cached);let attempts=0;const tryOnce=()=>{attempts+=1;const canUseGtag=typeof window.gtag==='function'&&!!measurementId;if(canUseGtag){try{window.gtag('get',measurementId,'client_id',function(raw){const normalized=parseClientIdFromGaCookie(raw||'');if(normalized){cacheClientId(normalized);return resolve(normalized);}
const fromCookie=parseClientIdFromGaCookie(readCookie('_ga'));if(fromCookie){cacheClientId(fromCookie);return resolve(fromCookie);}
if(attempts<maxAttempts)return setTimeout(tryOnce,attemptDelayMs);return resolve('');});return;}catch(_){}}
const fromCookie=parseClientIdFromGaCookie(readCookie('_ga'));if(fromCookie){cacheClientId(fromCookie);return resolve(fromCookie);}
if(attempts<maxAttempts)return setTimeout(tryOnce,attemptDelayMs);return resolve('');};tryOnce();});}
function getGclid(){const m=document.cookie.match(/(?:^|; )unipixel_gclid=([^;]+)/);return m?decodeURIComponent(m[1]):'';}
function ensureDataLayer(){if(!Array.isArray(global.dataLayer)){global.dataLayer=[];}
return global.dataLayer;}
global.GoogleHelper={resolveGoogleClientId,parseClientIdFromGaCookie,getGclid,ensureDataLayer};})(window);