import { logCoveoPageView } from "docUtils/utils"; import { LightningElement, api } from "lwc"; import { escapeRegExp } from "dxUtils/regexps"; const REDUNDANT_INSTANCE_ERROR_MESSAGE = "Multiple s detected, this should never be the case."; declare module globalThis { let singletonTrafficLabelerConnected: boolean; } export default class TrafficLabeler extends LightningElement { @api internalIps?: string; @api coveoOrganizationId!: string; @api coveoAnalyticsToken!: string; connectedCallback(): void { if (!globalThis.singletonTrafficLabelerConnected) { globalThis.singletonTrafficLabelerConnected = true; if (this.internalIps) { this.evaluateVPNAddress(); } } else { console.error(REDUNDANT_INSTANCE_ERROR_MESSAGE); } logCoveoPageView(this.coveoOrganizationId, this.coveoAnalyticsToken); } async evaluateVPNAddress() { try { const ipifyResult = await fetch( "https://api.ipify.org/?format=json" ); const result = await ipifyResult.json(); const internalIps = this.internalIps?.split(","); const isInternal = internalIps?.some((value) => { const regex = new RegExp(escapeRegExp(value)); return regex.test(result.ip); }); if (isInternal) { this.setTrafficType("internal"); } } catch (error) { console.error("fetch failed ", error); } } setTrafficType(trafficType: "internal" | "external") { // @ts-ignore) const dataLayer = (window.dataLayer = window.dataLayer || []); dataLayer.push({ traffic_type: trafficType }); } }