UNPKG

@klevu/core

Version:

Typescript SDK that simplifies development on Klevu backend. Klevu provides advanced AI-powered search and discovery solutions for online retailers.

61 lines (60 loc) 1.74 kB
import { KlevuConfig } from "../index.js"; import { KlevuUserSession } from "../resolvers/usersession.js"; export class Klaviyo { constructor() { this.timer = null; } setExchangeId() { var _a; if (!globalThis._learnq) { if (!this.timer) { this.timer = setInterval(() => { this.setExchangeId(); }, 300); } return; } const _getIdentifiersResponse = (_a = globalThis._learnq) === null || _a === void 0 ? void 0 : _a.push([ "_getIdentifiers", "", {}, ]); this.exchangeId = _getIdentifiersResponse.$exchange_id; if (!this.exchangeId) { if (!this.timer) { this.timer = setInterval(() => { this.setExchangeId(); }, 300); } return; } if (this.timer) { clearInterval(this.timer); } KlevuUserSession.getDefault().generateSession(); } static init() { Klaviyo.default = new Klaviyo(); this.getDefault().setExchangeId(); } static getDefault() { if (!Klaviyo.default) { throw new Error("Klaviyo not initialized."); } return Klaviyo.default; } getExchangeId() { return this.exchangeId; } static generatePayload() { if (!KlevuConfig.getDefault().enableKlaviyoConnector || !this.default || !this.getDefault().exchangeId) { return undefined; } return { connectorType: "klaviyo", exchangeId: this.getDefault().exchangeId || "", }; } }