UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

330 lines (329 loc) 8.69 kB
import kt 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 y, 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 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 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", Kh: "enableLogging", $h: "enableSdkAuthentication", Ca: "manageServiceWorkerExternally", Yh: "minimumIntervalBetweenTriggerActionsInSeconds", Xh: "sessionTimeoutInSeconds", Zh: "appVersion", Qh: "appVersionNumber", _a: "serviceWorkerLocation", Ba: "safariWebsitePushId", Xn: "localization", er: "contentSecurityNonce", re: "allowUserSuppliedJavascript", No: "inAppMessageZIndex", Ko: "openInAppMessagesInNewTab", tn: "openCardsInNewTab", en: "openNewsFeedCardsInNewTab", mh: "requireExplicitInAppMessageDismissal", Ea: "doNotLoadFontAwesome", Ia: "deviceId", Aa: "serviceWorkerScope", Na: "sdkFlavor", }; class Vt { constructor() { (this.un = ""), (this.Oa = ""), (this.Ta = void 0), (this.Ra = null), (this.an = null), (this.S = null), (this.Fi = null), (this.B = null), (this.T = null), (this.C = null), (this.Cs = null), (this.Pa = ""), (this.isInitialized = !1), (this.La = !1), (this.Ua = new u()), (this.Wa = new u()), (this.options = {}), (this.Va = []), (this.Fa = []), (this.Mi = []), (this.Oa = "5.8.1"); } Ka(t) { this.Ua.Ft(t); } Nh(t) { this.Wa.Ft(t); } initialize(t, i) { if (this._t()) return N.info("Braze has already been initialized with an API key."), !0; this.options = i || {}; let e = this.ee(w.Kh); const r = ll(no.Ga()); 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.un = 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.Pa = n.toString()), ro.$a && !this.ee(w.Yo)) ) return ( N.info("Ignoring activity from crawler bot " + navigator.userAgent), (this.La = !0), !1 ); const h = this.ee(w.Wh) || !1; if ( ((this.C = Bt.Ja(t, h)), h && this.C.Ya(t), new ne.se(null, !0).gr(s.me)) ) return ( N.info("Ignoring all activity due to previous opt out"), (this.La = !0), !1 ); for (const t of y(this.options)) -1 === Rt(zt).indexOf(t) && N.warn(`Ignoring unknown initialization option '${t}'.`); const a = ["mparticle", "wordpress", "tealium"]; if (null != this.ee(w.Na)) { const t = this.ee(w.Na); -1 !== a.indexOf(t) ? (this.Ta = 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.Ia); if (c) { const t = new _t(c); this.C.uu(s.iu.Ia, t); } (this.an = new Ot(this.C, l)), (this.B = new Mt(this.C)), (this.Cs = new bt(this.B, this.C)), (this.T = new Wt(this.C, this.Cs, this.B, this.ee(w.Xh))); const f = new u(); return ( (this.Ra = new kt(this.C, this.ee(w.$h), f)), this.N(f), (this.S = new Pt( this.an, this.C, this.Ra, this.Cs, this.T, this.B, this.un, this.Pa, this.Oa, this.Ta || "", this.ee(w.Zh), this.ee(w.Qh), )), (this.Fi = new Lt( this.un, this.Pa, this.T, this.an, this.Cs, this.B, this.C, (t) => { if (this._t()) for (const i of this.ar()) i.U(t); }, this.Ra, this.S, )), this.Fi.initialize(), h || this.C.Ha(), N.info( `Initialized for the Braze backend at "${this.ee( w.Zo, )}" with API key "${this.un}".`, ), ot.p(), this.B.Ke(() => { var t; this.isInitialized && (null === (t = this.B) || void 0 === t ? void 0 : t.Be()) && import("../FeatureFlags/refresh-feature-flags.js").then((t) => { if (!this.isInitialized) return; (0, t.default)(); }); }), this.Fi.rn(() => { var t; this.isInitialized && (null === (t = this.B) || void 0 === t ? void 0 : t.Be()) && import("../FeatureFlags/refresh-feature-flags.js").then((t) => { if (!this.isInitialized) return; (0, t.default)(void 0, void 0, !0); }); }), this.Ua.X(this.options), (this.isInitialized = !0), window.dispatchEvent(new CustomEvent("braze.initialized")), !0 ); } destroy(t) { if ((N.destroy(), this._t())) { this.Wa.X(), this.Wa.removeAllSubscriptions(); for (const t of this.Va) t.destroy(); this.Va = []; for (const t of this.Fa) t.clearData(!1); (this.Fa = []), this.removeAllSubscriptions(), (this.Mi = []), null != this.Fi && this.Fi.destroy(), (this.Fi = null), (this.Ra = null), (this.an = null), (this.S = null), (this.B = null), (this.T = null), (this.Cs = null), (this.options = {}), (this.Ta = void 0), (this.isInitialized = !1), (this.La = !1), t && (this.C = null); } } rr() { return !this.Xa() && (!!this._t() || (console.warn(p), !1)); } Ma() { return this.un; } Sr() { return this.Ra; } Z() { return this.Pa; } ue() { return this.an; } g() { return this.S; } ee(t) { return this.options[t]; } ar() { return this.Fa; } nn() { 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.Va) s === t && (i = !0); i || this.Va.push(t); } _(i) { let s = !1; for (const t of this.Fa) t.constructor === i.constructor && (s = !0); i instanceof t && !s && this.Fa.push(i); } N(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.La = t; } _t() { return this.isInitialized; } Xa() { return this.La; } di() { return this.Oa; } } const r = new Vt(); export { r as default, Vt as BrazeSdkInstance, w as OPTIONS };