UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

331 lines (330 loc) 8.72 kB
import qt 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 z, keys as C, validateValueIsFromEnum as ta, values as Rt, } from "../util/code-utils.js"; import Pt from "./network-manager.js"; import Lt from "../request-controller.js"; import Mt from "./server-config-manager.js"; import Wt from "./session-manager.js"; import ne, { STORAGE_KEYS as s } from "./storage-manager.js"; import Bt from "./storage-manager-factory.js"; import u from "./subscription-manager.js"; import { TriggersProviderFactory as ot } from "../triggers/triggers-provider-factory.js"; import kt 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 p } from "../common/constants.js"; import { SupportedOptions as zt, logger as N } from "../../shared-lib/index.js"; import _t from "../models/identifier.js"; const w = { Yo: "allowCrawlerActivity", Zo: "baseUrl", Wh: "noCookies", Vh: "devicePropertyAllowlist", Da: "disablePushTokenMaintenance", $h: "enableLogging", Yh: "enableSdkAuthentication", Ca: "manageServiceWorkerExternally", Xh: "minimumIntervalBetweenTriggerActionsInSeconds", Zh: "sessionTimeoutInSeconds", Qh: "appVersion", Ea: "appVersionNumber", _a: "serviceWorkerLocation", Ba: "safariWebsitePushId", Zn: "localization", er: "contentSecurityNonce", re: "allowUserSuppliedJavascript", Go: "inAppMessageZIndex", Ko: "openInAppMessagesInNewTab", tn: "openCardsInNewTab", en: "openNewsFeedCardsInNewTab", mh: "requireExplicitInAppMessageDismissal", Ia: "doNotLoadFontAwesome", Na: "deviceId", Aa: "serviceWorkerScope", Oa: "sdkFlavor", }; class Vt { constructor() { (this.an = ""), (this.Ta = ""), (this.Ra = void 0), (this.Pa = null), (this.hn = null), (this.S = null), (this.Fi = null), (this.B = null), (this.T = null), (this.C = null), (this.Cs = null), (this.La = ""), (this.isInitialized = !1), (this.Ua = !1), (this.Wa = new u()), (this.Va = new u()), (this.options = {}), (this.Fa = []), (this.Ka = []), (this.Mi = []), (this.Ta = "5.9.0"); } Ga(t) { this.Wa.Dt(t); } Nh(t) { this.Va.Dt(t); } initialize(t, i) { if (this.nn()) return N.info("Braze has already been initialized with an API key."), !0; this.options = i || {}; let e = this.ee(w.$h); const r = ll(no.$a()); 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.an = t; let n = this.ee(w.Zo); 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.La = n.toString()), ro.Ja && !this.ee(w.Yo)) ) return ( N.info("Ignoring activity from crawler bot " + navigator.userAgent), (this.Ua = !0), !1 ); const h = this.ee(w.Wh) || !1; if ( ((this.C = Bt.Ya(t, h)), h && this.C.Ha(t), new ne.se(null, !0).gr(s.me)) ) return ( N.info("Ignoring all activity due to previous opt out"), (this.Ua = !0), !1 ); for (const t of C(this.options)) -1 === Rt(zt).indexOf(t) && N.warn(`Ignoring unknown initialization option '${t}'.`); const a = ["mparticle", "wordpress", "tealium"]; if (null != this.ee(w.Oa)) { const t = this.ee(w.Oa); -1 !== a.indexOf(t) ? (this.Ra = t) : N.error("Invalid sdk flavor passed: " + t); } let l = this.ee(zt.Vh); if (null != l) if (z(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(w.Na); if (c) { const t = new _t(c); this.C.uu(s.iu.Na, t); } (this.hn = new Ot(this.C, l)), (this.B = new Mt(this.C)), (this.Cs = new kt(this.B, this.C)), (this.T = new Wt(this.C, this.Cs, this.B, this.ee(w.Zh))); const f = new u(); return ( (this.Pa = new qt(this.C, this.ee(w.Yh), f)), this.F(f), (this.S = new Pt( this.hn, this.C, this.Pa, this.Cs, this.T, this.B, this.an, this.La, this.Ta, this.Ra || "", this.ee(w.Qh), this.ee(w.Ea), )), (this.Fi = new Lt( this.an, this.La, this.T, this.hn, this.Cs, this.B, this.C, (t) => { if (this.nn()) for (const i of this.ar()) i.U(t); }, this.Pa, this.S, )), this.Fi.initialize(), h || this.C.Xa(), N.info( `Initialized for the Braze backend at "${this.ee( w.Zo, )}" with API key "${this.an}".`, ), ot.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.Fi.on(() => { 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.Wa.X(this.options), (this.isInitialized = !0), window.dispatchEvent(new CustomEvent("braze.initialized")), !0 ); } destroy(t) { if ((N.destroy(), this.nn())) { this.Va.X(), this.Va.removeAllSubscriptions(); for (const t of this.Fa) t.destroy(); this.Fa = []; for (const t of this.Ka) t.clearData(!1); this.S && this.S.fo(), (this.Ka = []), this.removeAllSubscriptions(), (this.Mi = []), null != this.Fi && this.Fi.destroy(), (this.Fi = null), (this.Pa = null), (this.hn = null), (this.S = null), (this.B = null), (this.T = null), (this.Cs = null), (this.options = {}), (this.Ra = void 0), (this.isInitialized = !1), (this.Ua = !1), t && (this.C = null); } } rr() { return !this.Za() && (!!this.nn() || (console.warn(p), !1)); } Ma() { return this.an; } Sr() { return this.Pa; } Z() { return this.La; } ue() { return this.hn; } g() { return this.S; } ee(t) { return this.options[t]; } ar() { return this.Ka; } rn() { return this.Fi; } v() { return this.B; } P() { return this.T; } j() { return this.C; } mr() { if (this.Cs && this.Fi) return new User(this.Cs, this.Fi); } ir() { return this.Cs; } tr() { return !0 === this.ee(w.re); } h(t) { let i = !1; for (const s of this.Fa) s === t && (i = !0); i || this.Fa.push(t); } _(i) { let s = !1; for (const t of this.Ka) t.constructor === i.constructor && (s = !0); i instanceof t && !s && this.Ka.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.Ua = t; } nn() { return this.isInitialized; } Za() { return this.Ua; } fi() { return this.Ta; } } const r = new Vt(); export { r as default, Vt as BrazeSdkInstance, w as OPTIONS };