1// * this handler only runs after the Osano cookie consent being granted, otherwise, ignored. 2function getCookie(name) { 3 const pattern = new RegExp( 4 '(?:^|; )' + 5 name.replace(/([.$?*|{}()[\]\\/+^])/g, '\\$1') + 6 '=([^;]*)' 7 ); 8 const match = document.cookie.match(pattern); 9 return match ? decodeURIComponent(match[1]) : null; 10} 11 12// polling fetching dy cookies 13function waitForDYCookie({ interval = 50, timeout = 5000 } = {}) { 14 return new Promise((resolve, reject) => { 15 const start = Date.now(); 16 (function check() { 17 const dyid = getCookie("_dyid"); 18 const dyjsession = getCookie("_dyjsession"); 19 if (dyid && dyjsession) return resolve({dyid, dyjsession}); 20 if (Date.now() - start > timeout) { 21 return reject(new Error("Timeout: DY cookies did not appear after consent.")); 22 } 23 setTimeout(check, interval); 24 })(); 25 }); 26} 27 28function fireImpression() { 29 if (sessionStorage.getItem('dyImpressionFired') === 'true') { 30 return; 31 } 32 waitForDYCookie().then(({ dyid, dyjsession }) => { 33 // Fire impression request 34 sessionStorage.setItem('dyImpressionFired', 'true'); 35 Object.values(window.DY_CAMPAIGN.data).forEach((campaignData) => { 36 fetch("/api/dy/engagement", { 37 method: "POST", 38 headers: { "Content-Type": "application/json" }, 39 body: JSON.stringify({ 40 user: { 41 "active_consent_accepted": true, 42 "dyid": dyid, 43 }, 44 session: { dy: dyjsession }, 45 engagements: [{ 46 type: "IMP", 47 variation: [campaignData.variation.id], 48 decisionId: campaignData.choice.decisionId 49 }] 50 }), 51 }).catch((err) => console.error("Impression error:", err)); 52 }) 53 }).catch((err) => console.error(err)); 54} 55 56// DY cookies will be set once the personalization consent is granted 57// For new visitors (or session) 58Osano?.cm?.on("osano-cm-consent-saved", (consent) => { 59 if (!consent?.PERSONALIZATION) return; 60 61 const hasDyCampaign = () => 62 window.DY_CAMPAIGN && typeof window.DY_CAMPAIGN === "object" && Object.keys(window.DY_CAMPAIGN).length > 0; 63 64 if (hasDyCampaign()) { 65 fireImpression(); 66 return; 67 } 68 69 // start polling if DY campaign data is not available 70 const dyCampaignDataTimeout = setTimeout(() => { 71 clearInterval(dyCampaignDataInterval); 72 if (hasDyCampaign()) { 73 console.error("Impression failed: Timeout waiting for window.DY_CAMPAIGN."); 74 } 75 }, 3000); // Wait for a maximum of 3 seconds 76 77 const dyCampaignDataInterval = setInterval(() => { 78 if (hasDyCampaign()) { 79 clearInterval(dyCampaignDataInterval); 80 clearTimeout(dyCampaignDataTimeout); 81 fireImpression(); 82 } 83 }, 100); 84});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.