@braze/web-sdk
Version:
Braze SDK for web sites and other JS platforms.
263 lines (262 loc) • 7.4 kB
JavaScript
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 E 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.N(this.Mi),
(this._i = 1e3),
(this.Pi = 6e4),
(this.Gi = null),
(this.$i = null),
(this.Oi = null);
}
Xi() {
return this.Mi;
}
Hi(t) {
return this.Mi.Ft(t);
}
Ji() {
return this.Gi;
}
Ki(t) {
this.Gi = t;
}
ut(t, e, s, i) {
const r = new E();
let n;
if (e === d.Li || t instanceof ControlMessage) {
if (!t.ts())
return (
N.info(
"This in-app message has already received an impression. Ignoring analytics event.",
),
r
);
} else if (e === d.Qi || (t instanceof HtmlMessage && e === d.Vi)) {
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.Wi(t)),
null == n
? r
: (t.messageExtras && (n.message_extras = t.messageExtras),
null != s && (n.bid = s),
c.ut(e, n))
);
}
Yi(t, e) {
const s = new E();
if (!t.$t())
return (
N.info(
"This in-app message button has already received a click. Ignoring analytics event.",
),
s
);
const i = this.Wi(e);
return null == i
? s
: t.id === InAppMessageButton.Zi
? (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.Vi, i));
}
Ir(t) {
const e = t.messageFields;
return (null != e && e.is_push_primer) || !1;
}
Mr(t) {
if (!(t instanceof InAppMessage)) return;
const e = (t) => {
if (!t) return;
const e = eo(t);
return ft(e)
? dt(jt.Tr, "In-App Message")
: gt(e) && !vt.wr()
? dt(jt.Br, "In-App Message")
: void 0;
};
if (this.Ir(t) && !vt.wr())
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;
}
_r(t, e) {
e !== this.Oi && this.qr(), (this.$i = t), (this.Oi = e);
}
qr() {
null != this.$i &&
(clearTimeout(this.$i), (this.$i = null), (this.Oi = null));
}
Er(t, e, s, i) {
const r = this.S;
if (!r) return;
this.Oi && t.triggerId !== this.Oi && (this.qr(), h.wi(this.C, h.O.Gr));
const n = r.Nr(!1, !1),
o = r.H(n);
(o.template = { trigger_id: t.triggerId, trigger_event_type: e }),
null != s && (o.template.data = s.$r());
const u = r.J(o, h.O.Gr);
r.V(
o,
(r = -1) => {
const n = this.S;
if (!n) return;
const p = new Date().valueOf();
h.W(this.C, h.O.Gr, 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.Cr && t.Cr());
if ((n.it(), null == e || null == e.templated_message)) return;
const s = e.templated_message;
if (s.type !== pt.Xr.Or) return;
const i = ut(s.data);
if (null == i) return;
const r = this.Mr(i);
if (r)
return N.error(r), void ("function" == typeof t.Cr && t.Cr());
"function" == typeof t.Hr && t.Hr(i);
},
error: (e) => {
(g = !0),
(m = e),
(c = `getting user personalization for message ${t.triggerId}.`);
},
nt: (r, o) => {
if (new Date().valueOf() - t.Jr < t.Kr) {
let r = 0;
if (g) {
const e = Math.min(t.Kr, this.Pi),
s = this._i;
null == i && (i = s), (r = Math.min(e, a(s, 3 * i)));
}
n.rt(
o,
() => {
this.Er(t, e, s, r);
},
h.O.Gr,
(e) => this._r(e, t.triggerId),
() => this.qr(),
r,
);
}
g && n.et(m, c);
},
});
},
h.O.Gr,
);
}
Wi(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.Lr, t.bt())
);
}
Ar() {
if (!this.C) return null;
const t = this.C.ft(s.gt.Lr);
if (!t) return null;
let e;
switch (t.type) {
case InAppMessage.po.Qr:
e = FullScreenMessage.Ur(t);
break;
case InAppMessage.po.Vr:
case InAppMessage.po.fo:
case InAppMessage.po.Fo:
e = HtmlMessage.Ur(t);
break;
case InAppMessage.po.Wr:
case InAppMessage.po.Ao:
e = ModalMessage.Ur(t);
break;
case InAppMessage.po.Yr:
e = SlideUpMessage.Ur(t);
}
return e && this.Zr(), e;
}
Zr() {
this.C && this.C.Ut(s.gt.Lr);
}
}