var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; const TgphxLC = { UTM_KEYS: ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'], init(config) { TgphxLC.visitEpoch = Math.floor(Date.now() / 1000); TgphxLC.targetUrl = config.targetUrl; TgphxLC.tgChannelID = config.tgChannelID; TgphxLC.yaCounterID = config.yaCounterID; TgphxLC.buttonClassPattern = config.buttonClassPattern; TgphxLC.postButtonClassPattern = config.postButtonClassPattern; TgphxLC.redirectToChannel = config.redirectToChannel; window.addEventListener('DOMContentLoaded', () => { void TgphxLC.onDOMContentLoaded(); }); }, onDOMContentLoaded() { return __awaiter(this, void 0, void 0, function* () { const tgphxID = yield TgphxLC.getCookieID('tgphxID'); if (tgphxID !== undefined) TgphxLC.tgphxID = tgphxID; const yclid = yield TgphxLC.getParamSearch('yclid'); TgphxLC.yclid = !isNaN(Number(yclid)) ? yclid : undefined; TgphxLC.isad = yield TgphxLC.getCookieID('_ym_isad'); TgphxLC.sid = yield TgphxLC.getCookieID('yabs-sid'); TgphxLC.yaClientID = (yield TgphxLC.getCookieID('_ym_uid')) || TgphxLC.getsLocalStorage('_ym_uid'); if (!TgphxLC.yclid && !TgphxLC.yaClientID) { TgphxLC.hasYM = yield TgphxLC.checkYM(); if (TgphxLC.hasYM) yield TgphxLC.getYaClientID(); } TgphxLC.utmParams = yield TgphxLC.getLocationSearchParams(TgphxLC.UTM_KEYS); if (TgphxLC.redirectToChannel === 'bot') { yield TgphxLC.sendClientVisit('visit_bot'); } else { yield TgphxLC.sendClientVisit('visit'); } if (TgphxLC.redirectToChannel !== false) { if (TgphxLC.redirectToChannel === 'bot') { yield TgphxLC.doRedirectToBot(); } else { yield TgphxLC.doRedirectToChannel(); } } yield TgphxLC.setButtonStyle(); const buttons = document.querySelectorAll(`[class*=${TgphxLC.buttonClassPattern}]`); for (const button of buttons) { button.addEventListener('click', (event) => { void TgphxLC.onMarkedButtonClick(event); }); } }); }, getsLocalStorage(param) { try { if ('localStorage' in window && window['localStorage'] !== null) { return window.localStorage[param]; } } catch (e) { return null; } }, getCookieID(name) { return __awaiter(this, void 0, void 0, function* () { const cookies = document.cookie.split('; '); for (const cookie of cookies) { if (cookie.startsWith(`${name}=`)) return cookie.split('=')[1]; } return undefined; }); }, getParamSearch(p) { return __awaiter(this, void 0, void 0, function* () { var match = RegExp('[?&]' + p + '=([^&]*)').exec(window.location.search); return match && decodeURIComponent(match[1].replace(/\+/g, ' ')); }); }, checkYM() { return __awaiter(this, void 0, void 0, function* () { const counterObject = window[`yaCounter${TgphxLC.yaCounterID}`]; if (counterObject !== undefined) return true; else return false; }); }, getYaClientID() { return __awaiter(this, void 0, void 0, function* () { ym(TgphxLC.yaCounterID, 'getClientID', id => { TgphxLC.yaClientID = `${id}`; }); }); }, getLocationSearchParams(keys) { return __awaiter(this, void 0, void 0, function* () { const sp = new URLSearchParams(document.location.search); const ret = {}; sp.forEach((value, key) => { const lc = key.toLowerCase(); if (value !== '' && keys.includes(lc)) { ret[lc] = decodeURIComponent(value); } }); return ret; }); }, sendClientVisit(path) { return __awaiter(this, void 0, void 0, function* () { const sp = Object.assign({ visitEpoch: TgphxLC.visitEpoch.toString(), yaCounterID: TgphxLC.yaCounterID.toString() }, TgphxLC.utmParams); if (TgphxLC.tgphxID !== undefined) sp.tgphxID = TgphxLC.tgphxID; if (TgphxLC.tgChannelID !== undefined) sp.tgChannelID = TgphxLC.tgChannelID; if (TgphxLC.yaClientID !== undefined) sp.yaClientID = TgphxLC.yaClientID; if (TgphxLC.yclid !== undefined) sp.yclid = TgphxLC.yclid; if (TgphxLC.isad !== undefined) sp.isad = TgphxLC.isad; if (TgphxLC.sid !== undefined) sp.sid = TgphxLC.sid; const targetUrl = new URL(`${TgphxLC.targetUrl}/api/landing/${path}`); targetUrl.search = new URLSearchParams(sp).toString(); const request = yield fetch(targetUrl, { method: 'POST' }); const response = yield request.json(); const id = response.clientId; if (id) { TgphxLC.clientId = id; } if (response.tgphxID) { TgphxLC.tgphxID = response.tgphxID; document.cookie = `tgphxID=${response.tgphxID}`; } }); }, doRedirectToChannel() { return __awaiter(this, void 0, void 0, function* () { const sp = {}; if (TgphxLC.tgChannelID !== undefined) sp.tgChannelID = TgphxLC.tgChannelID; if (TgphxLC.clientId !== undefined) sp.clientId = TgphxLC.clientId; if (!isNaN(Number(String(TgphxLC.redirectToChannel)))) sp.redirectToPost = String(TgphxLC.redirectToChannel); const targetUrl = new URL(`${TgphxLC.targetUrl}/api/landing/redirect`); targetUrl.search = new URLSearchParams(sp).toString(); if (TgphxLC.hasYM) ym(TgphxLC.yaCounterID, 'reachGoal', 'toChannel'); window.location.replace(targetUrl.toString()); }); }, doRedirectToBot() { return __awaiter(this, void 0, void 0, function* () { const sp = { botName: 'tgraphyx_bot', }; if (TgphxLC.clientId !== undefined) sp.clientId = TgphxLC.clientId; const targetUrl = new URL(`${TgphxLC.targetUrl}/api/bot/redirect`); targetUrl.search = new URLSearchParams(sp).toString(); if (TgphxLC.hasYM) ym(TgphxLC.yaCounterID, 'reachGoal', 'toBot'); window.location.replace(targetUrl.toString()); }); }, setButtonStyle() { var _a; return __awaiter(this, void 0, void 0, function* () { const style = document.createElement('style'); style.innerHTML = `[class*="${TgphxLC.buttonClassPattern}"]:hover {cursor: pointer;}`; (_a = document.getElementsByTagName('head')[0]) === null || _a === void 0 ? void 0 : _a.appendChild(style); }); }, onMarkedButtonClick(event) { return __awaiter(this, void 0, void 0, function* () { if (!TgphxLC.tgChannelID) { const targetUrl = new URL(`${TgphxLC.targetUrl}/api/bot/redirect`); window.location.href = targetUrl.toString(); return; } const sp = { tgChannelID: TgphxLC.tgChannelID, }; if (TgphxLC.clientId !== undefined) sp.clientId = TgphxLC.clientId; const button = event.currentTarget; for (const className of button.classList) { const match = className.match(TgphxLC.postButtonClassPattern); if (match !== null) { sp.redirectToPost = match[1]; break; } } const targetUrl = new URL(`${TgphxLC.targetUrl}/api/landing/redirect`); targetUrl.search = new URLSearchParams(sp).toString(); window.location.href = targetUrl.toString(); }); }, }; TgphxLC.init(JSON.parse("{\"targetUrl\":\"https://app.telegraphyx.ru\",\"tgChannelID\":\"-1002133678033\",\"yaCounterID\":96378614,\"buttonClassPattern\":\"telegraphyx-to-channel\",\"postButtonClassPattern\":\"telegraphyx-to-channel-post-(\\\\d+)\",\"redirectToChannel\":false}"));