UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

264 lines (263 loc) 7.33 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 v from "../common/event-logger.js"; import { newInAppMessageFromJson as pt } from "./in-app-message-factory.js"; import { randomInclusive as a } from "../util/math.js"; import L from "../models/request-result.js"; import { logger as E, EventTypes as p } from "../../shared-lib/index.js"; import f from "../managers/subscription-manager.js"; import gt from "../triggers/models/trigger.js"; import { BRAZE_ACTION_URI_REGEX as to } from "../util/validation-utils.js"; import { containsPushPrimerBrazeAction as jt, containsUnknownBrazeAction as vt, getDecodedBrazeAction as eo, ineligibleBrazeActionURLErrorMessage as yt, INELIGIBLE_BRAZE_ACTION_URL_ERROR_TYPES as bt, } from "../util/braze-actions.js"; import It 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"; import { IamClickAction as $e, IamServerTypes as dt } from "./constants.js"; export default class ea { constructor(t, e, s, i) { (this.B = t), (this.j = e), (this.C = s), (this.Ss = i), (this.B = t), (this.j = e), (this.C = s), (this.Ss = i), (this.In = new f()), r.S(this.In), (this.An = 1e3), (this.Dn = 6e4), (this.Bn = null), (this._n = null), (this.qn = null); } Pn() { return this.In; } En(t) { return this.In.Kt(t); } Gn() { return this.Bn; } Nn(t) { this.Bn = t; } wt(t, e, s, i) { const r = new L(); let n; if (e === p.Fn || t instanceof ControlMessage) { if (!t.gs()) return ( E.info( "This in-app message has already received an impression. Ignoring analytics event.", ), r ); } else if (e === p.On || (t instanceof HtmlMessage && e === p.Xn)) { if (!t.ss(i)) return ( E.info( "This in-app message has already received a click. Ignoring analytics event.", ), r ); } return ( (n = t instanceof ControlMessage ? { trigger_ids: [t.triggerId] } : this.Hn(t)), null == n ? r : (t.messageExtras && (n.message_extras = t.messageExtras), null != s && (n.bid = s), v.wt(e, n)) ); } Jn(t, e) { const s = new L(); if (!t.ss()) return ( E.info( "This in-app message button has already received a click. Ignoring analytics event.", ), s ); const i = this.Hn(e); return null == i ? s : t.id === InAppMessageButton.Kn ? (E.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), v.wt(p.Xn, i)); } Ln(t) { const e = t.messageFields; return (null != e && e.is_push_primer) || !1; } Qn(t) { if (!(t instanceof InAppMessage)) return; const e = (t) => { if (!t) return; const e = eo(t); if (vt(e)) return yt(bt.Un, "In-App Message"); if (jt(e)) { const t = It.Vn(); if (!t.Wn) return It.Yn(t.reason, "In-App Message"); } }; if (this.Ln(t)) { const t = It.Vn(); if (!t.Wn) return It.Yn(t.reason, "In-App Message"); } const s = t.buttons || []; let i; for (const t of s) if ( t.clickAction === $e.URI && t.uri && to.test(t.uri) && ((i = e(t.uri)), i) ) return i; return t.clickAction === $e.URI && t.uri && to.test(t.uri) ? e(t.uri) : void 0; } Zn(t, e) { e !== this.qn && this._o(), (this._n = t), (this.qn = e); } _o() { null != this._n && (clearTimeout(this._n), (this._n = null), (this.qn = null)); } Xo(t, e, s, i) { const r = this.B; if (!r) return; this.qn && t.triggerId !== this.qn && (this._o(), h.Li(this.C, h.it.Yo)); const n = r.Zo(!1), o = r.Z(n); (o.template = { trigger_id: t.triggerId, trigger_event_type: e }), null != s && (o.template.data = s.ta()); const u = r.tt(o, h.it.Yo); r.et( o, (r = -1) => { const n = this.B; if (!n) return; const m = new Date().valueOf(); h.nt(this.C, h.it.Yo, m), -1 !== r && u.push(["X-Braze-Req-Tokens-Remaining", r.toString()]); let c, p, f = !1; l.ot({ url: `${n.ht()}/template/`, data: o, headers: u, lt: (e) => { if (!n.ut(o, e, u)) return void ("function" == typeof t.ia && t.ia()); if ((n.ct(), null == e || null == e.templated_message)) return; const s = e.templated_message; if (s.type !== gt.la.oa) return; const i = pt(s.data); if (null == i) return; const r = this.Qn(i); if (r) return E.error(r), void ("function" == typeof t.ia && t.ia()); "function" == typeof t.ua && t.ua(i); }, error: (e) => { (f = !0), (c = e), (p = `getting user personalization for message ${t.triggerId}.`); }, ft: (r, o) => { if (new Date().valueOf() - t.ma < t.ca) { let r = 0; if (f) { const e = Math.min(t.ca, this.Dn), s = this.An; null == i && (i = s), (r = Math.min(e, a(s, 3 * i))); } n.yt( o, () => { this.Xo(t, e, s, r); }, h.it.Yo, (e) => this.Zn(e, t.triggerId), () => this._o(), r, ); } f && n.dt(c, p); }, }); }, h.it.Yo, ); } Hn(t) { if (null == t.triggerId) return ( E.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; } jn(t) { return ( !!this.C && !( !(t && t instanceof InAppMessage && t.constructor !== InAppMessage) || t instanceof ControlMessage ) && this.C.It(s.Tt.pa, t.qt()) ); } sa() { if (!this.C) return null; const t = this.C.Rt(s.Tt.pa); if (!t) return null; let e; switch (t.type) { case dt.cE: e = FullScreenMessage.ha(t); break; case dt.iE: case dt.PE: case dt.nE: e = HtmlMessage.ha(t); break; case dt.aE: case dt.UE: e = ModalMessage.ha(t); break; case dt.sE: e = SlideUpMessage.ha(t); } return e && this.fa(), e; } fa() { this.C && this.C.Qt(s.Tt.pa); } }