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'); const botMetricsID = yield TgphxLC.getCookieID('botMetricsID'); if (tgphxID !== undefined) TgphxLC.tgphxID = tgphxID; if (botMetricsID !== undefined) TgphxLC.botMetricsID = botMetricsID; 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); }); } }); }, 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; }); }, 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.botMetricsID !== undefined) sp.botMetricsID = TgphxLC.botMetricsID; if (TgphxLC.tgChannelID !== undefined) sp.tgChannelID = TgphxLC.tgChannelID; if (TgphxLC.yaClientID !== undefined) sp.yaClientID = TgphxLC.yaClientID; 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.id; if (id && path === 'visit_bot') { TgphxLC.botMetricsID = id; document.cookie = `botMetricsID=${id}`; } else if (id && path === 'visit') { TgphxLC.tgphxID = id; document.cookie = `tgphxID=${id}`; } }); }, doRedirectToChannel() { var _a; return __awaiter(this, void 0, void 0, function* () { const sp = { tgChannelID: (_a = TgphxLC.tgChannelID) !== null && _a !== void 0 ? _a : '', }; if (TgphxLC.tgphxID !== undefined) sp.tgphxID = TgphxLC.tgphxID; 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 = { botMetricsID: (_a = TgphxLC.botMetricsID) !== null && _a !== void 0 ? _a : '', botName: 'tgraphyx_bot' }; 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.tgphxID !== undefined) sp.tgphxID = TgphxLC.tgphxID; 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\":\"-1002035201254\",\"yaCounterID\":96253814,\"buttonClassPattern\":\"telegraphyx-to-channel\",\"postButtonClassPattern\":\"telegraphyx-to-channel-post-(\\\\d+)\",\"redirectToChannel\":true}"));