UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

263 lines (262 loc) 7.4 kB
import l from "../util/net.js"; import r from "../managers/braze-instance.js"; import ControlMessage from "./models/control-message.js"; import HtmlMessage from "./models/html-message.js"; import InAppMessage from "./models/in-app-message.js"; import InAppMessageButton from "./models/in-app-message-button.js"; import c from "../common/event-logger.js"; import { newInAppMessageFromJson as ut } from "./in-app-message-factory.js"; import { randomInclusive as a } from "../util/math.js"; import F from "../models/request-result.js"; import { logger as N, EventTypes as d } from "../../shared-lib/index.js"; import u from "../managers/subscription-manager.js"; import pt from "../triggers/models/trigger.js"; import { BRAZE_ACTION_URI_REGEX as to } from "../util/validation-utils.js"; import { containsPushPrimerBrazeAction as gt, containsUnknownBrazeAction as ft, getDecodedBrazeAction as eo, ineligibleBrazeActionURLErrorMessage as dt, INELIGIBLE_BRAZE_ACTION_URL_ERROR_TYPES as jt, } from "../util/braze-actions.js"; import vt from "../Push/utils/push-utils.js"; import h from "../util/request-header-utils.js"; import { STORAGE_KEYS as s } from "../managers/storage-manager.js"; import { FullScreenMessage, ModalMessage, SlideUpMessage } from "./index.js"; export default class ea { constructor(t, e, s, i) { (this.S = t), (this.T = e), (this.C = s), (this.Cs = i), (this.S = t), (this.T = e), (this.C = s), (this.Cs = i), (this.Mi = new u()), r.F(this.Mi), (this.Bi = 1e3), (this._i = 6e4), (this.Pi = null), (this.Gi = null), (this.$i = null); } Oi() { return this.Mi; } Xi(t) { return this.Mi.Dt(t); } Hi() { return this.Pi; } Ji(t) { this.Pi = t; } ut(t, e, s, i) { const r = new F(); let n; if (e === d.Ki || t instanceof ControlMessage) { if (!t._t()) return ( N.info( "This in-app message has already received an impression. Ignoring analytics event.", ), r ); } else if (e === d.Li || (t instanceof HtmlMessage && e === d.Qi)) { if (!t.$t(i)) return ( N.info( "This in-app message has already received a click. Ignoring analytics event.", ), r ); } return ( (n = t instanceof ControlMessage ? { trigger_ids: [t.triggerId] } : this.Vi(t)), null == n ? r : (t.messageExtras && (n.message_extras = t.messageExtras), null != s && (n.bid = s), c.ut(e, n)) ); } Wi(t, e) { const s = new F(); if (!t.$t()) return ( N.info( "This in-app message button has already received a click. Ignoring analytics event.", ), s ); const i = this.Vi(e); return null == i ? s : t.id === InAppMessageButton.Yi ? (N.info( "This in-app message button does not have a tracking id. Not logging event to Braze servers.", ), s) : (null != t.id && (i.bid = t.id), c.ut(d.Qi, i)); } Zi(t) { const e = t.messageFields; return (null != e && e.is_push_primer) || !1; } Ir(t) { if (!(t instanceof InAppMessage)) return; const e = (t) => { if (!t) return; const e = eo(t); return ft(e) ? dt(jt.Mr, "In-App Message") : gt(e) && !vt.Tr() ? dt(jt.wr, "In-App Message") : void 0; }; if (this.Zi(t) && !vt.Tr()) return "In-App Message contains a push prompt, but is not eligible for a push prompt. Ignoring."; const s = t.buttons || []; let i; for (const t of s) if ( t.clickAction === InAppMessage.ClickAction.URI && t.uri && to.test(t.uri) && ((i = e(t.uri)), i) ) return i; return t.clickAction === InAppMessage.ClickAction.URI && t.uri && to.test(t.uri) ? e(t.uri) : void 0; } Br(t, e) { e !== this.$i && this._r(), (this.Gi = t), (this.$i = e); } _r() { null != this.Gi && (clearTimeout(this.Gi), (this.Gi = null), (this.$i = null)); } qr(t, e, s, i) { const r = this.S; if (!r) return; this.$i && t.triggerId !== this.$i && (this._r(), h.vi(this.C, h.O.Er)); const n = r.Gr(!1, !1), o = r.H(n); (o.template = { trigger_id: t.triggerId, trigger_event_type: e }), null != s && (o.template.data = s.Nr()); const u = r.J(o, h.O.Er); r.V( o, (r = -1) => { const n = this.S; if (!n) return; const p = new Date().valueOf(); h.W(this.C, h.O.Er, p), -1 !== r && u.push(["X-Braze-Req-Tokens-Remaining", r.toString()]); let m, c, g = !1; l.Y({ url: `${n.Z()}/template/`, data: o, headers: u, tt: (e) => { if (!n.st(o, e, u)) return void ("function" == typeof t.$r && t.$r()); if ((n.it(), null == e || null == e.templated_message)) return; const s = e.templated_message; if (s.type !== pt.Or.Cr) return; const i = ut(s.data); if (null == i) return; const r = this.Ir(i); if (r) return N.error(r), void ("function" == typeof t.$r && t.$r()); "function" == typeof t.Xr && t.Xr(i); }, error: (e) => { (g = !0), (m = e), (c = `getting user personalization for message ${t.triggerId}.`); }, nt: (r, o) => { if (new Date().valueOf() - t.Hr < t.Jr) { let r = 0; if (g) { const e = Math.min(t.Jr, this._i), s = this.Bi; null == i && (i = s), (r = Math.min(e, a(s, 3 * i))); } n.rt( o, () => { this.qr(t, e, s, r); }, h.O.Er, (e) => this.Br(e, t.triggerId), () => this._r(), r, ); } g && n.et(m, c); }, }); }, h.O.Er, ); } Vi(t) { if (null == t.triggerId) return ( N.info( "The in-app message has no analytics id. Not logging event to Braze servers.", ), null ); const e = {}; return null != t.triggerId && (e.trigger_ids = [t.triggerId]), e; } sn(t) { return ( !!this.C && !( !(t && t instanceof InAppMessage && t.constructor !== InAppMessage) || t instanceof ControlMessage ) && this.C.Bt(s.gt.Kr, t.bt()) ); } Ar() { if (!this.C) return null; const t = this.C.ft(s.gt.Kr); if (!t) return null; let e; switch (t.type) { case InAppMessage.bo.Lr: e = FullScreenMessage.Qr(t); break; case InAppMessage.bo.Ur: case InAppMessage.bo.po: case InAppMessage.bo.Jo: e = HtmlMessage.Qr(t); break; case InAppMessage.bo.Vr: case InAppMessage.bo.Fo: e = ModalMessage.Qr(t); break; case InAppMessage.bo.Wr: e = SlideUpMessage.Qr(t); } return e && this.Yr(), e; } Yr() { this.C && this.C.Ut(s.gt.Kr); } }