// abhsplit-tracker.js (function() { const ABHSPLIT_SERVER = 'https://advancedbiohealth.com/AbhSplit'; function generateUUID() { const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); console.log('Generated UUID:', uuid); return uuid; } function getOrCreateVisitorId(pageType) { let visitorId = localStorage.getItem('abhsplit_visitor_id'); console.log('Existing visitor ID from localStorage:', visitorId); // Only generate new ID if it's a landing page and no ID exists if (!visitorId && pageType === 'landing') { visitorId = generateUUID(); localStorage.setItem('abhsplit_visitor_id', visitorId); console.log('Created new visitor ID:', visitorId); } return visitorId; } function setCookie(name, value, days) { let expires = ''; if (days) { const date = new Date(); date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); expires = '; expires=' + date.toUTCString(); } document.cookie = name + '=' + (value || '') + expires + '; path=/; Secure; SameSite=Lax'; console.log(`Cookie set: ${name}=${value}`); } async function trackPageView(experimentId, pageType) { console.log('TrackPageView called with:', { experimentId, pageType }); const visitorId = getOrCreateVisitorId(pageType); // If this is an upsell page and no visitor ID exists, it's a direct visit - ignore it if (pageType === 'upsell' && !visitorId) { console.log('Direct upsell page visit detected - tracking aborted'); return; } console.log('Active visitor ID:', visitorId); // Check if we already have a version assigned const existingVersion = localStorage.getItem('abhsplit_version_' + experimentId); console.log('Existing version:', existingVersion); // Store experiment info only if we have a valid visitor ID if (visitorId) { localStorage.setItem('abhsplit_experiment_' + experimentId, visitorId); setCookie('abhsplit_exp_' + experimentId, visitorId, 365); } const currentUrl = window.location.href; console.log('Current URL:', currentUrl); try { console.log('Sending request to server...'); const response = await fetch(`${ABHSPLIT_SERVER}/track.php`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ visitor_id: visitorId, experiment_id: experimentId, page_type: pageType, url: currentUrl, existing_version: existingVersion || null }), credentials: 'include' }); const data = await response.json(); console.log('Server response:', data); if (data.success) { window.isRedirect = false; // Store the assigned version only if we have a valid visitor ID if (visitorId) { localStorage.setItem('abhsplit_version_' + experimentId, data.version); setCookie('abhsplit_ver_' + experimentId, data.version, 365); } // Only handle redirects for landing pages if (pageType === 'landing' && data.version === 'variant' && data.redirect_url && currentUrl !== data.redirect_url) { console.log('Redirecting to variant:', data.redirect_url); window.isRedirect = true; console.log('Setting isRedirect flag to true'); // Use replace to avoid breaking the back button window.location.replace(data.redirect_url); } else { console.log('No redirect needed. Version:', data.version); } } else { console.error('Server returned error:', data.error); } } catch (error) { console.error('AbhSplit tracking error:', error); } } // Expose globally window.AbhSplit = { track: trackPageView }; console.log('AbhSplit tracker initialized'); })();