import type { AdSpotType } from '../runtime'; /** Хелпер для работы с попандерной рекламой и UTM-метками */ export class PopunderHelper { private static utmParamsKeys = [ 'utm_campaign', 'utm_medium', 'utm_source', ]; /** Открыта ли страница уже после срабатывания попандера (по метке `asgtbndr` в URL) */ private static isPageOpenAfterPopunder() { return !!(new URLSearchParams(location.search).get('asgtbndr')); } /** Сохраняет UTM-параметры из URL в sessionStorage */ private static saveUtmParams() { const query = new URLSearchParams(location.search); query.forEach((value, key) => { if (PopunderHelper.utmParamsKeys.includes(key)) { sessionStorage.setItem(key, value); } }); }; /** Удаляет старые скрипты попандера и связанные с ним глобальные переменные window */ private static cleanOldPopunders(scriptUrl: string) { document.querySelectorAll('script').forEach(script => { if (script.src.includes(scriptUrl)) { script.remove(); } }); const puWindowKeys = [ 'AsgBanner', 'NaConf', 'asgAdgptLoaded', 'asgPopScript', 'asgfp' ]; puWindowKeys.forEach((key) => { if (window[key]) { delete window[key]; } }); } /** Создаёт и вставляет в `
` скрипт попандера для указанных спотов (только на клиенте) */ private static attachPopunderScript(scriptUrl: string, spots: string) { const ridts = `?_ridts_=${Date.now()}`; const script = document.createElement('script'); const scriptAttrs = { type: 'text/javascript', async: 'async', src: scriptUrl + ridts, defer: 'defer', }; script.dataset.spots = spots; script.dataset.subid1 = '%subid1%'; script.dataset.tag = 'asg'; Object.entries(scriptAttrs).forEach(([key, value]) => { script.setAttribute(key, value); }); if (import.meta.client) { document.head.appendChild(script); } } /** Возвращает сохранённые в sessionStorage UTM-параметры */ public static getSessionUtmParams(): Record