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) { const now = new Date(); const utcTimestamp = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds(), now.getUTCMilliseconds()); TgphxLC.visitEpoch = Math.floor(utcTimestamp / 1000); TgphxLC.targetUrl = config.targetUrl; if (config.tgChannelID) { TgphxLC.tgChannelID = config.tgChannelID; } if (config.start) { TgphxLC.start = config.start; } if (config.botID) { TgphxLC.botID = config.botID; } if (config.landing) { TgphxLC.landing = config.landing; } if (config.tgBotName) { TgphxLC.tgBotName = config.tgBotName; } if (config.vkPixelID) { TgphxLC.vkPixelID = config.vkPixelID; } TgphxLC.yaCounterID = config.yaCounterID; TgphxLC.buttonClassPattern = config.buttonClassPattern; TgphxLC.postButtonClassPattern = config.postButtonClassPattern; TgphxLC.redirectToChannel = config.redirectToChannel; TgphxLC.withoutRedirectToChannel = config.withoutRedirectToChannel; if (TgphxLC.hasYM) { ym(TgphxLC.yaClientID, 'init', { clickmap: false, trackLinks: true, accurateTrackBounce: true, webvisor: false, triggerEvent: true }); } window.addEventListener('DOMContentLoaded', () => { void TgphxLC.onDOMContentLoaded(); }); window.addEventListener('yacounter' + TgphxLC.yaCounterID + 'inited', function () { TgphxLC.inited = true; void TgphxLC.onYaCounterInited(); console.log('yacounter' + TgphxLC.yaCounterID + 'inited'); }); }, onYaCounterInited() { return __awaiter(this, void 0, void 0, function* () { try { if (!TgphxLC.yaClientID && TgphxLC.hasYM) { yield TgphxLC.getYaClientID(); if (!TgphxLC.yaClientID) { yield new Promise(resolve => setTimeout(resolve, 1000)); yield TgphxLC.getYaClientID(); } } } catch (e) { console.error(e.message); } finally { void this.startVisitAndRedirect(); } }); }, startVisitAndRedirect() { return __awaiter(this, void 0, void 0, function* () { if (!!TgphxLC.tgBotName) { 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(); } } }); }, 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.hasYM = typeof ym === 'function'; TgphxLC.utmParams = yield TgphxLC.getLocationSearchParams(TgphxLC.UTM_KEYS); 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.vkPixelID) { TgphxLC.getRbClick(); } if (!TgphxLC.inited) { void this.onYaCounterInited(); } yield TgphxLC.setButtonStyle(); const buttons = document.querySelectorAll(`[class*="${TgphxLC.buttonClassPattern}"]`); for (let i = 0; i < buttons.length; i++) { const button = buttons[i]; const clickTarget = button.children[0] || button; clickTarget.addEventListener('click', (event) => { 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, ' ')); }); }, getYaClientID() { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { try { ym(TgphxLC.yaCounterID, 'getClientID', (id) => __awaiter(this, void 0, void 0, function* () { const yaClientID = id ? id.toString() : null; if (yaClientID) { if (yaClientID.startsWith('"')) { TgphxLC.yaClientID = yaClientID.substring(1, TgphxLC.yaClientID.length - 1); } else { TgphxLC.yaClientID = yaClientID; } resolve(); } else { reject(); } })); } catch (e) { reject(e); } }); }); }, 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; }); }, getRbClick() { return __awaiter(this, void 0, void 0, function* () { const searchParams = new URLSearchParams(window.location.search); const rbClickId = searchParams.get('rb_clickid'); if (rbClickId) TgphxLC.rb_clickid = rbClickId; }); }, 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) sp.tgphxID = TgphxLC.tgphxID; if (!!TgphxLC.tgChannelID) sp.tgChannelID = TgphxLC.tgChannelID; if (!!TgphxLC.tgBotName) sp.tgBotName = TgphxLC.tgBotName; if (!!TgphxLC.vkPixelID) sp.vkPixelID = TgphxLC.vkPixelID; if (!!TgphxLC.rb_clickid) sp.rb_clickid = TgphxLC.rb_clickid; if (!!TgphxLC.yaClientID) sp.yaClientID = TgphxLC.yaClientID; if (!!TgphxLC.yclid) sp.yclid = TgphxLC.yclid; if (!!TgphxLC.isad) sp.isad = TgphxLC.isad; if (!!TgphxLC.sid) 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() { var _a; return __awaiter(this, void 0, void 0, function* () { const sp = { tgBotName: (_a = TgphxLC.tgBotName) !== null && _a !== void 0 ? _a : 'tgraphyx_bot', }; if (TgphxLC.tgChannelID !== undefined) sp.tgChannelID = TgphxLC.tgChannelID; if (TgphxLC.botID !== undefined) sp.botID = TgphxLC.botID; if (TgphxLC.start !== undefined) sp.start = TgphxLC.start; if (TgphxLC.landing !== undefined) sp.landing = TgphxLC.landing; 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) { event.preventDefault(); event.stopPropagation(); if (TgphxLC.tgBotName) { const sp = { tgBotName: TgphxLC.tgBotName, }; if (TgphxLC.tgChannelID !== undefined) sp.tgChannelID = TgphxLC.tgChannelID; if (TgphxLC.botID !== undefined) sp.botID = TgphxLC.botID; if (TgphxLC.start !== undefined) sp.start = TgphxLC.start; if (TgphxLC.landing !== undefined) sp.landing = TgphxLC.landing; 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.href = targetUrl.toString(); return; } const sp = { tgChannelID: TgphxLC.tgChannelID, }; if (TgphxLC.clientId !== undefined) sp.clientId = TgphxLC.clientId; if (!isNaN(Number(String(TgphxLC.withoutRedirectToChannel)))) sp.redirectToPost = String(TgphxLC.withoutRedirectToChannel); 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\":\"-1001702624228\",\"tgBotName\":\"\",\"botID\":\"\",\"vkPixelID\":\"\",\"start\":\"\",\"landing\":\"\",\"yaCounterID\":96814013,\"buttonClassPattern\":\"header__button\",\"postButtonClassPattern\":\"telegraphyx-to-channel-post-(\\\\d+)\",\"redirectToChannel\":\"51\",\"withoutRedirectToChannel\":false}"));