(function () { const ENDPOINT = "PASTE_YOUR_APPS_SCRIPT_WEB_APP_URL"; const SITE_KEY = "REPLACE_WITH_THE_SAME_SITE_KEY"; const STORAGE_ID = "bb_attribution_visitor"; const TOUCH_ID = "bb_attribution_touch"; function readJSON(key) { try { return JSON.parse(localStorage.getItem(key) || "null"); } catch (error) { return null; } } function saveJSON(key, value) { try { localStorage.setItem(key, JSON.stringify(value)); } catch (error) {} } function getCookie(name) { const match = document.cookie.match( new RegExp("(?:^|; )" + name + "=([^;]*)") ); return match ? decodeURIComponent(match[1]) : ""; } function getClientId() { const ga = getCookie("_ga"); const match = ga.match(/^GA\d+\.\d+\.(\d+\.\d+)$/); return match ? match[1] : ""; } function getReferrer() { return document.referrer || ""; } function getTrafficSource(referrer) { const params = new URLSearchParams(location.search); const utmSource = params.get("utm_source"); const utmMedium = params.get("utm_medium"); const utmCampaign = params.get("utm_campaign"); if (utmSource) { return { source: utmSource, medium: utmMedium || "unknown", campaign: utmCampaign || "" }; } if (params.has("gclid")) { return { source: "google", medium: "cpc", campaign: "" }; } if (params.has("fbclid")) { return { source: "facebook", medium: "paid_social", campaign: "" }; } if (referrer) { try { const host = new URL(referrer).hostname .replace(/^www\./, ""); if (host && !host.endsWith(location.hostname)) { return { source: host, medium: "referral", campaign: "" }; } } catch (error) {} } return null; } function getDevice() { const ua = navigator.userAgent; if (/tablet|ipad/i.test(ua)) return "tablet"; if (/mobile|iphone|android/i.test(ua)) return "mobile"; return "desktop"; } function send(payload) { payload.key = SITE_KEY; try { const body = new Blob( [JSON.stringify(payload)], { type: "text/plain" } ); navigator.sendBeacon(ENDPOINT, body); } catch (error) {} } let visitor = readJSON(STORAGE_ID); if (!visitor || !visitor.visitor_id) { visitor = { visitor_id: crypto.randomUUID() }; saveJSON(STORAGE_ID, visitor); } const referrer = getReferrer(); const newSource = getTrafficSource(referrer); let touch = readJSON(TOUCH_ID); if (!touch) { touch = { source: "direct", medium: "none", campaign: "", landing_page: location.href, referrer: referrer }; } if (newSource) { touch = { source: newSource.source, medium: newSource.medium, campaign: newSource.campaign, landing_page: location.href, referrer: referrer }; } saveJSON(TOUCH_ID, touch); send({ action: "visitor", visitor_id: visitor.visitor_id, source: touch.source, medium: touch.medium, campaign: touch.campaign, landing_page: touch.landing_page, referrer: referrer, device: getDevice(), client_id: getClientId() }); document.addEventListener("click", function (event) { const link = event.target.closest("a"); if (!link) return; const href = link.getAttribute("href") || ""; if (href.toLowerCase().startsWith("tel:")) { send({ action: "phone_click", visitor_id: visitor.visitor_id, phone_number: href.replace(/^tel:/i, ""), source: touch.source, medium: touch.medium, campaign: touch.campaign, landing_page: location.href }); } }); })();
Skip to contentBrandon Boushy SEO & Marketing LLC