@braze/web-sdk
Version:
Braze SDK for web sites and other JS platforms.
330 lines (329 loc) • 8.68 kB
JavaScript
import Bt from "./auth-manager.js";
import t from "../common/base-provider.js";
import ro from "../util/browser-detector.js";
import Ot from "./device-manager.js";
import DeviceProperties from "../Core/device-properties.js";
import {
isArray as D,
keys as C,
validateValueIsFromEnum as ta,
values as Pt,
} from "../util/code-utils.js";
import Mt from "./network-manager.js";
import Wt from "../request-controller.js";
import Vt from "./server-config-manager.js";
import Kt from "./session-manager.js";
import ee, { STORAGE_KEYS as s } from "./storage-manager.js";
import Gt from "./storage-manager-factory.js";
import u from "./subscription-manager.js";
import { TriggersProviderFactory as rt } from "../triggers/triggers-provider-factory.js";
import bt from "../User/user-manager.js";
import { User } from "../User/index.js";
import { parseQueryStringKeyValues as ll } from "../util/url-utils.js";
import { WindowUtils as no } from "../util/window-utils.js";
import { BRAZE_MUST_BE_INITIALIZED_ERROR as w } from "../common/constants.js";
import { SupportedOptions as $t, logger as E } from "../../shared-lib/index.js";
import _t from "../models/identifier.js";
const z = {
_o: "allowCrawlerActivity",
Ro: "baseUrl",
Wo: "noCookies",
Vo: "devicePropertyAllowlist",
Aa: "disablePushTokenMaintenance",
Yo: "enableLogging",
Xo: "enableSdkAuthentication",
qa: "manageServiceWorkerExternally",
Zo: "minimumIntervalBetweenTriggerActionsInSeconds",
Wh: "sessionTimeoutInSeconds",
Vh: "appVersion",
Kh: "appVersionNumber",
Ma: "serviceWorkerLocation",
ka: "safariWebsitePushId",
Xn: "localization",
er: "contentSecurityNonce",
re: "allowUserSuppliedJavascript",
Jo: "inAppMessageZIndex",
Do: "openInAppMessagesInNewTab",
tn: "openCardsInNewTab",
Lh: "requireExplicitInAppMessageDismissal",
$h: "doNotLoadFontAwesome",
Jh: "deviceId",
_a: "serviceWorkerScope",
Yh: "sdkFlavor",
};
class Jt {
constructor() {
(this.sn = ""),
(this.Xh = ""),
(this.Zh = void 0),
(this.Qh = null),
(this.on = null),
(this.B = null),
(this.Cn = null),
(this.h = null),
(this.C = null),
(this.j = null),
(this.Cs = null),
(this.Ea = ""),
(this.isInitialized = !1),
(this.Ia = !1),
(this.Sa = new u()),
(this.Na = new u()),
(this.options = {}),
(this.Oa = []),
(this.Ta = []),
(this.yi = []),
(this.Xh = "6.1.0");
}
Ra(t) {
this.Sa.wt(t);
}
mh(t) {
this.Na.wt(t);
}
initialize(t, i) {
if (this.ao())
return E.info("Braze has already been initialized with an API key."), !0;
this.options = i || {};
let e = this.ee(z.Yo);
const r = ll(no.Ca());
if (
(r && "true" === r.brazeLogging && (e = !0),
E.init(e),
E.info(
`Initialization Options: ${JSON.stringify(this.options, null, 2)}`,
),
null == t || "" === t || "string" != typeof t)
)
return E.error("Braze requires a valid API key to be initialized."), !1;
this.sn = t;
let n = this.ee(z.Ro);
if (null == n || "" === n || "string" != typeof n)
return E.error("Braze requires a valid baseUrl to be initialized."), !1;
!1 === /^https?:/.test(n) && (n = `https://${n}`);
const o = n;
if (
((n = document.createElement("a")),
(n.href = o),
"/" === n.pathname && (n = `${n}api/v3`),
(this.Ea = n.toString()),
ro.Pa && !this.ee(z._o))
)
return (
E.info("Ignoring activity from crawler bot " + navigator.userAgent),
(this.Ia = !0),
!1
);
const h = this.ee(z.Wo) || !1;
if (
((this.j = Gt.La(t, h)), h && this.j.Da(t), new ee.ne(null, !0).br(s.ie))
)
return (
E.info("Ignoring all activity due to previous opt out"),
(this.Ia = !0),
!1
);
for (const t of C(this.options))
-1 === Pt($t).indexOf(t) &&
E.warn(`Ignoring unknown initialization option '${t}'.`);
const a = ["mparticle", "wordpress", "tealium"];
if (null != this.ee(z.Yh)) {
const t = this.ee(z.Yh);
-1 !== a.indexOf(t)
? (this.Zh = t)
: E.error("Invalid sdk flavor passed: " + t);
}
let l = this.ee($t.Vo);
if (null != l)
if (D(l)) {
const t = [];
for (let i = 0; i < l.length; i++)
ta(
DeviceProperties,
l[i],
"devicePropertyAllowlist contained an invalid value.",
"DeviceProperties",
) && t.push(l[i]);
l = t;
} else
E.error(
"devicePropertyAllowlist must be an array. Defaulting to all properties.",
),
(l = null);
const c = this.ee(z.Jh);
if (c) {
const t = new _t(c);
this.j.uu(s.iu.Jh, t);
}
(this.on = new Ot(this.j, l)),
(this.h = new Vt(this.j)),
(this.Cs = new bt(this.h, this.j)),
(this.C = new Kt(this.j, this.Cs, this.h, this.ee(z.Wh)));
const f = new u();
return (
(this.Qh = new Bt(this.j, this.ee(z.Xo), f)),
this.q(f),
(this.B = new Mt(
this.on,
this.j,
this.Qh,
this.Cs,
this.C,
this.h,
this.sn,
this.Ea,
this.Xh,
this.Zh || "",
this.ee(z.Vh),
this.ee(z.Kh),
)),
(this.Cn = new Wt(
this.sn,
this.Ea,
this.C,
this.on,
this.Cs,
this.h,
this.j,
(t) => {
if (this.ao()) for (const i of this.lr()) i.N(t);
},
this.Qh,
this.B,
)),
this.Cn.initialize(),
h || this.j.Ua(),
E.info(
`Initialized for the Braze backend at "${this.ee(
z.Ro,
)}" with API key "${this.sn}".`,
),
rt.t(),
this.h.Ue(() => {
var t;
this.isInitialized &&
(null === (t = this.h) || void 0 === t ? void 0 : t.Re()) &&
import("../FeatureFlags/refresh-feature-flags.js").then((t) => {
if (!this.isInitialized) return;
(0, t.default)();
});
}),
this.Cn.rn(() => {
var t;
this.isInitialized &&
(null === (t = this.h) || void 0 === t ? void 0 : t.Re()) &&
import("../FeatureFlags/refresh-feature-flags.js").then((t) => {
if (!this.isInitialized) return;
(0, t.default)(void 0, void 0, !0);
});
}),
this.Sa.L(this.options),
(this.isInitialized = !0),
window.dispatchEvent(new CustomEvent("braze.initialized")),
!0
);
}
destroy(t) {
if ((E.destroy(), this.ao())) {
this.Na.L(), this.Na.removeAllSubscriptions();
for (const t of this.Oa) t.destroy();
this.Oa = [];
for (const t of this.Ta) t.clearData(!1);
this.B && this.B.fo(),
(this.Ta = []),
this.removeAllSubscriptions(),
(this.yi = []),
null != this.Cn && this.Cn.destroy(),
(this.Cn = null),
(this.Qh = null),
(this.on = null),
(this.B = null),
(this.h = null),
(this.C = null),
(this.Cs = null),
(this.options = {}),
(this.Zh = void 0),
(this.isInitialized = !1),
(this.Ia = !1),
t && (this.j = null);
}
}
rr() {
return !this.Wa() && (!!this.ao() || (console.warn(w), !1));
}
ba() {
return this.sn;
}
Sr() {
return this.Qh;
}
V() {
return this.Ea;
}
de() {
return this.on;
}
m() {
return this.B;
}
ee(t) {
return this.options[t];
}
lr() {
return this.Ta;
}
nn() {
return this.Cn;
}
l() {
return this.h;
}
u() {
return this.C;
}
p() {
return this.j;
}
gr() {
if (this.Cs && this.Cn) return new User(this.Cs, this.Cn);
}
ir() {
return this.Cs;
}
tr() {
return !0 === this.ee(z.re);
}
g(t) {
let i = !1;
for (const s of this.Oa) s === t && (i = !0);
i || this.Oa.push(t);
}
v(i) {
let s = !1;
for (const t of this.Ta) t.constructor === i.constructor && (s = !0);
i instanceof t && !s && this.Ta.push(i);
}
q(t) {
t instanceof u && this.yi.push(t);
}
removeAllSubscriptions() {
if (this.rr()) for (const t of this.yi) t.removeAllSubscriptions();
}
removeSubscription(t) {
if (this.rr()) for (const i of this.yi) i.removeSubscription(t);
}
ce(t) {
this.Ia = t;
}
ao() {
return this.isInitialized;
}
Wa() {
return this.Ia;
}
Vs() {
return this.Xh;
}
}
const r = new Jt();
export { r as default, Jt as BrazeSdkInstance, z as OPTIONS };