@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 N } from "../../shared-lib/index.js";
import _t from "../models/identifier.js";
const z = {
_o: "allowCrawlerActivity",
Yo: "baseUrl",
Xo: "noCookies",
Zo: "devicePropertyAllowlist",
Aa: "disablePushTokenMaintenance",
Wh: "enableLogging",
Vh: "enableSdkAuthentication",
qa: "manageServiceWorkerExternally",
Kh: "minimumIntervalBetweenTriggerActionsInSeconds",
$h: "sessionTimeoutInSeconds",
Yh: "appVersion",
Xh: "appVersionNumber",
Ma: "serviceWorkerLocation",
ka: "safariWebsitePushId",
Zn: "localization",
er: "contentSecurityNonce",
re: "allowUserSuppliedJavascript",
Ko: "inAppMessageZIndex",
Qo: "openInAppMessagesInNewTab",
tn: "openCardsInNewTab",
Lh: "requireExplicitInAppMessageDismissal",
Zh: "doNotLoadFontAwesome",
Qh: "deviceId",
_a: "serviceWorkerScope",
Ea: "sdkFlavor",
};
class Jt {
constructor() {
(this.on = ""),
(this.Ia = ""),
(this.Sa = void 0),
(this.Na = null),
(this.un = null),
(this.S = null),
(this.Cn = null),
(this.B = null),
(this.T = null),
(this.C = null),
(this.Cs = null),
(this.Oa = ""),
(this.isInitialized = !1),
(this.Ta = !1),
(this.Ra = new u()),
(this.Ca = new u()),
(this.options = {}),
(this.Pa = []),
(this.La = []),
(this.Mi = []),
(this.Ia = "6.0.0");
}
Da(t) {
this.Ra.Dt(t);
}
mh(t) {
this.Ca.Dt(t);
}
initialize(t, i) {
if (this.ao())
return N.info("Braze has already been initialized with an API key."), !0;
this.options = i || {};
let e = this.ee(z.Wh);
const r = ll(no.Ua());
if (
(r && "true" === r.brazeLogging && (e = !0),
N.init(e),
N.info(
`Initialization Options: ${JSON.stringify(this.options, null, 2)}`,
),
null == t || "" === t || "string" != typeof t)
)
return N.error("Braze requires a valid API key to be initialized."), !1;
this.on = t;
let n = this.ee(z.Yo);
if (null == n || "" === n || "string" != typeof n)
return N.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.Oa = n.toString()),
ro.Wa && !this.ee(z._o))
)
return (
N.info("Ignoring activity from crawler bot " + navigator.userAgent),
(this.Ta = !0),
!1
);
const h = this.ee(z.Xo) || !1;
if (
((this.C = Gt.Ba(t, h)), h && this.C.Va(t), new ee.se(null, !0).pr(s.me))
)
return (
N.info("Ignoring all activity due to previous opt out"),
(this.Ta = !0),
!1
);
for (const t of C(this.options))
-1 === Pt($t).indexOf(t) &&
N.warn(`Ignoring unknown initialization option '${t}'.`);
const a = ["mparticle", "wordpress", "tealium"];
if (null != this.ee(z.Ea)) {
const t = this.ee(z.Ea);
-1 !== a.indexOf(t)
? (this.Sa = t)
: N.error("Invalid sdk flavor passed: " + t);
}
let l = this.ee($t.Zo);
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
N.error(
"devicePropertyAllowlist must be an array. Defaulting to all properties.",
),
(l = null);
const c = this.ee(z.Qh);
if (c) {
const t = new _t(c);
this.C.uu(s.iu.Qh, t);
}
(this.un = new Ot(this.C, l)),
(this.B = new Vt(this.C)),
(this.Cs = new bt(this.B, this.C)),
(this.T = new Kt(this.C, this.Cs, this.B, this.ee(z.$h)));
const f = new u();
return (
(this.Na = new Bt(this.C, this.ee(z.Vh), f)),
this.F(f),
(this.S = new Mt(
this.un,
this.C,
this.Na,
this.Cs,
this.T,
this.B,
this.on,
this.Oa,
this.Ia,
this.Sa || "",
this.ee(z.Yh),
this.ee(z.Xh),
)),
(this.Cn = new Wt(
this.on,
this.Oa,
this.T,
this.un,
this.Cs,
this.B,
this.C,
(t) => {
if (this.ao()) for (const i of this.lr()) i.U(t);
},
this.Na,
this.S,
)),
this.Cn.initialize(),
h || this.C.Fa(),
N.info(
`Initialized for the Braze backend at "${this.ee(
z.Yo,
)}" with API key "${this.on}".`,
),
rt.p(),
this.B.Ke(() => {
var t;
this.isInitialized &&
(null === (t = this.B) || void 0 === t ? void 0 : t.ke()) &&
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.B) || void 0 === t ? void 0 : t.ke()) &&
import("../FeatureFlags/refresh-feature-flags.js").then((t) => {
if (!this.isInitialized) return;
(0, t.default)(void 0, void 0, !0);
});
}),
this.Ra.X(this.options),
(this.isInitialized = !0),
window.dispatchEvent(new CustomEvent("braze.initialized")),
!0
);
}
destroy(t) {
if ((N.destroy(), this.ao())) {
this.Ca.X(), this.Ca.removeAllSubscriptions();
for (const t of this.Pa) t.destroy();
this.Pa = [];
for (const t of this.La) t.clearData(!1);
this.S && this.S.fo(),
(this.La = []),
this.removeAllSubscriptions(),
(this.Mi = []),
null != this.Cn && this.Cn.destroy(),
(this.Cn = null),
(this.Na = null),
(this.un = null),
(this.S = null),
(this.B = null),
(this.T = null),
(this.Cs = null),
(this.options = {}),
(this.Sa = void 0),
(this.isInitialized = !1),
(this.Ta = !1),
t && (this.C = null);
}
}
rr() {
return !this.Ka() && (!!this.ao() || (console.warn(w), !1));
}
ba() {
return this.on;
}
Sr() {
return this.Na;
}
Z() {
return this.Oa;
}
ue() {
return this.un;
}
g() {
return this.S;
}
ee(t) {
return this.options[t];
}
lr() {
return this.La;
}
nn() {
return this.Cn;
}
v() {
return this.B;
}
P() {
return this.T;
}
j() {
return this.C;
}
gr() {
if (this.Cs && this.Cn) return new User(this.Cs, this.Cn);
}
ir() {
return this.Cs;
}
tr() {
return !0 === this.ee(z.re);
}
h(t) {
let i = !1;
for (const s of this.Pa) s === t && (i = !0);
i || this.Pa.push(t);
}
_(i) {
let s = !1;
for (const t of this.La) t.constructor === i.constructor && (s = !0);
i instanceof t && !s && this.La.push(i);
}
F(t) {
t instanceof u && this.Mi.push(t);
}
removeAllSubscriptions() {
if (this.rr()) for (const t of this.Mi) t.removeAllSubscriptions();
}
removeSubscription(t) {
if (this.rr()) for (const i of this.Mi) i.removeSubscription(t);
}
fe(t) {
this.Ta = t;
}
ao() {
return this.isInitialized;
}
Ka() {
return this.Ta;
}
Ws() {
return this.Ia;
}
}
const r = new Jt();
export { r as default, Jt as BrazeSdkInstance, z as OPTIONS };