UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

265 lines (264 loc) 7.39 kB
import a 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 l } from "../util/math.js"; import G from "../models/request-result.js"; import { logger as E, EventTypes as m } 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 ft, containsUnknownBrazeAction as gt, 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.B = t), (this.C = e), (this.j = s), (this.Cs = i), (this.B = t), (this.C = e), (this.j = s), (this.Cs = i), (this.Mr = new u()), r.q(this.Mr), (this.Br = 1e3), (this._r = 6e4), (this.qr = null), (this.Er = null), (this.Gr = null); } Nr() { return this.Mr; } Or(t) { return this.Mr.wt(t); } Xr() { return this.qr; } Hr(t) { this.qr = t; } rt(t, e, s, i) { const r = new G(); let n; if (e === m.Jr || t instanceof ControlMessage) { if (!t.Qt()) return ( E.info( "This in-app message has already received an impression. Ignoring analytics event.", ), r ); } else if (e === m.Kr || (t instanceof HtmlMessage && e === m.Lr)) { if (!t.$t(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.Qr(t)), null == n ? r : (t.messageExtras && (n.message_extras = t.messageExtras), null != s && (n.bid = s), c.rt(e, n)) ); } Ur(t, e) { const s = new G(); if (!t.$t()) return ( E.info( "This in-app message button has already received a click. Ignoring analytics event.", ), s ); const i = this.Qr(e); return null == i ? s : t.id === InAppMessageButton.Vr ? (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), c.rt(m.Lr, i)); } Wr(t) { const e = t.messageFields; return (null != e && e.is_push_primer) || !1; } Yr(t) { if (!(t instanceof InAppMessage)) return; const e = (t) => { if (!t) return; const e = eo(t); if (gt(e)) return dt(jt.Zr, "In-App Message"); if (ft(e)) { const t = vt.en(); if (!t.sn) return vt.on(t.reason, "In-App Message"); } }; if (this.Wr(t)) { const t = vt.en(); if (!t.sn) return vt.on(t.reason, "In-App Message"); } 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; } an(t, e) { e !== this.Gr && this.ln(), (this.Er = t), (this.Gr = e); } ln() { null != this.Er && (clearTimeout(this.Er), (this.Er = null), (this.Gr = null)); } un(t, e, s, i) { const r = this.B; if (!r) return; this.Gr && t.triggerId !== this.Gr && (this.ln(), h.fi(this.j, h.H.mn)); const n = r.cn(!1), o = r.$(n); (o.template = { trigger_id: t.triggerId, trigger_event_type: e }), null != s && (o.template.data = s.pn()); const u = r.A(o, h.H.mn); r.J( o, (r = -1) => { const n = this.B; if (!n) return; const m = new Date().valueOf(); h.K(this.j, h.H.mn, m), -1 !== r && u.push(["X-Braze-Req-Tokens-Remaining", r.toString()]); let c, p, f = !1; a.O({ url: `${n.V()}/template/`, data: o, headers: u, W: (e) => { if (!n.Y(o, e, u)) return void ("function" == typeof t.hn && t.hn()); if ((n.Z(), null == e || null == e.templated_message)) return; const s = e.templated_message; if (s.type !== pt.gn.fn) return; const i = ut(s.data); if (null == i) return; const r = this.Yr(i); if (r) return E.error(r), void ("function" == typeof t.hn && t.hn()); "function" == typeof t.dn && t.dn(i); }, error: (e) => { (f = !0), (c = e), (p = `getting user personalization for message ${t.triggerId}.`); }, tt: (r, o) => { if (new Date().valueOf() - t.jn < t.vn) { let r = 0; if (f) { const e = Math.min(t.vn, this._r), s = this.Br; null == i && (i = s), (r = Math.min(e, l(s, 3 * i))); } n.st( o, () => { this.un(t, e, s, r); }, h.H.mn, (e) => this.an(e, t.triggerId), () => this.ln(), r, ); } f && n._(c, p); }, }); }, h.H.mn, ); } Qr(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; } Ge(t) { return ( !!this.j && !( !(t && t instanceof InAppMessage && t.constructor !== InAppMessage) || t instanceof ControlMessage ) && this.j.ft(s.ct.yn, t.dt()) ); } Ar() { if (!this.j) return null; const t = this.j.lt(s.ct.yn); if (!t) return null; let e; switch (t.type) { case InAppMessage.Le.bn: e = FullScreenMessage.In(t); break; case InAppMessage.Le.Mn: case InAppMessage.Le.Ke: case InAppMessage.Le.Ao: e = HtmlMessage.In(t); break; case InAppMessage.Le.An: case InAppMessage.Le.Io: e = ModalMessage.In(t); break; case InAppMessage.Le.Tn: e = SlideUpMessage.In(t); } return e && this.kn(), e; } kn() { this.j && this.j.Nt(s.ct.yn); } }