UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

338 lines (337 loc) 8.91 kB
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 z, 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 ot } from "../triggers/triggers-provider-factory.js"; import ir from "../DUST/dust-provider-factory.js"; import { subscribeToDust as $t } from "../DUST/subscribe-to-dust.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 Jt, logger as E } from "../../shared-lib/index.js"; import _t from "../models/identifier.js"; const q = { Ph: "allowCrawlerActivity", Wh: "baseUrl", Vh: "noCookies", Kh: "devicePropertyAllowlist", Da: "disablePushTokenMaintenance", $h: "enableLogging", Yh: "enableSdkAuthentication", Ca: "manageServiceWorkerExternally", Xh: "minimumIntervalBetweenTriggerActionsInSeconds", Zh: "sessionTimeoutInSeconds", Qh: "appVersion", Ia: "appVersionNumber", _a: "serviceWorkerLocation", Ba: "safariWebsitePushId", Ea: "localization", er: "contentSecurityNonce", re: "allowUserSuppliedJavascript", Jo: "inAppMessageZIndex", To: "openInAppMessagesInNewTab", tn: "openCardsInNewTab", Oh: "requireExplicitInAppMessageDismissal", Na: "doNotLoadFontAwesome", Oa: "deviceId", Aa: "serviceWorkerScope", zi: "dustHost", Ta: "sdkFlavor", }; class Yt { constructor() { (this.Pn = ""), (this.Ra = ""), (this.Pa = void 0), (this.La = null), (this.Dn = null), (this.B = null), (this.au = null), (this.h = null), (this.C = null), (this.j = null), (this.Cs = null), (this.Ua = ""), (this.isInitialized = !1), (this.Wa = !1), (this.Va = new u()), (this.Fa = new u()), (this.options = {}), (this.Ka = []), (this.Ga = []), (this.Mr = []), (this.Ra = "6.4.0"); } $a(t) { this.Va.wt(t); } Lh(t) { this.Fa.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(q.$h); const r = ll(no.Ha()); 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.Pn = t; let o = this.ee(q.Wh); if (null == o || "" === o || "string" != typeof o) return E.error("Braze requires a valid baseUrl to be initialized."), !1; !1 === /^https?:/.test(o) && (o = `https://${o}`); const n = o; if ( ((o = document.createElement("a")), (o.href = n), "/" === o.pathname && (o = `${o}api/v3`), (this.Ua = o.toString()), ro.Ja && !this.ee(q.Ph)) ) return ( E.info("Ignoring activity from crawler bot " + navigator.userAgent), (this.Wa = !0), !1 ); const h = this.ee(q.Vh) || !1; if ( ((this.j = Gt.Ya(t, h)), h && this.j.Xa(t), new ee.ne(null, !0).jr(s.ie)) ) return ( E.info("Ignoring all activity due to previous opt out"), (this.Wa = !0), !1 ); for (const t of C(this.options)) -1 === Pt(Jt).indexOf(t) && E.warn(`Ignoring unknown initialization option '${t}'.`); const a = ["mparticle", "wordpress", "tealium"]; if (null != this.ee(q.Ta)) { const t = this.ee(q.Ta); -1 !== a.indexOf(t) ? (this.Pa = t) : E.error("Invalid sdk flavor passed: " + t); } let l = this.ee(Jt.Kh); 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 E.error( "devicePropertyAllowlist must be an array. Defaulting to all properties.", ), (l = null); const c = this.ee(q.Oa); if (c) { const t = new _t(c); this.j.uu(s.iu.Oa, t); } (this.Dn = 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(q.Zh))); const f = new u(); return ( (this.La = new Bt(this.j, this.ee(q.Yh), f)), this.q(f), (this.B = new Mt( this.Dn, this.j, this.La, this.Cs, this.C, this.h, this.Pn, this.Ua, this.Ra, this.Pa || "", this.ee(q.Qh), this.ee(q.Ia), )), (this.au = new Wt( this.Pn, this.Ua, this.C, this.Dn, this.Cs, this.h, this.j, (t) => { if (this.ao()) for (const i of this.gr()) i.N(t); }, this.La, this.B, )), this.au.initialize(), h || this.j.Za(), E.info( `Initialized for the Braze backend at "${this.ee( q.Wh, )}" with API key "${this.Pn}".`, ), ot.t(), $t(), this.h.$e(() => { var t; this.isInitialized && (null === (t = this.h) || void 0 === t ? void 0 : t.Te()) && import("../FeatureFlags/refresh-feature-flags.js").then((t) => { if (!this.isInitialized) return; (0, t.default)(); }); }), this.au.rn(() => { var t; this.isInitialized && (null === (t = this.h) || void 0 === t ? void 0 : t.Te()) && import("../FeatureFlags/refresh-feature-flags.js").then((t) => { if (!this.isInitialized) return; (0, t.default)(void 0, void 0, !0); }); }), this.Va.L(this.options), (this.isInitialized = !0), window.dispatchEvent(new CustomEvent("braze.initialized")), !0 ); } destroy(t) { if ((E.destroy(), this.ao())) { this.Fa.L(), this.Fa.removeAllSubscriptions(); for (const t of this.Ka) t.destroy(); this.Ka = []; for (const t of this.Ga) t.clearData(!1); this.B && this.B.fo(), (this.Ga = []), this.removeAllSubscriptions(), (this.Mr = []), null != this.au && this.au.destroy(), (this.au = null), (this.La = null), (this.Dn = null), (this.B = null), (this.h = null), (this.C = null), (this.Cs = null), (this.options = {}), (this.Pa = void 0), (this.isInitialized = !1), (this.Wa = !1), t && (this.j = null); } } rr() { return !this.Qa() && (!!this.ao() || (console.warn(w), !1)); } Ma() { return this.Pn; } Sr() { return this.La; } V() { return this.Ua; } de() { return this.Dn; } m() { return this.B; } ee(t) { return this.options[t]; } gr() { return this.Ga; } nn() { return this.au; } l() { return this.h; } u() { return this.C; } p() { return this.j; } br() { if (this.Cs && this.au) return new User(this.Cs, this.au); } ir() { return this.Cs; } nr() { return !0 === this.ee(q.re); } g(t) { let i = !1; for (const s of this.Ka) s === t && (i = !0); i || this.Ka.push(t); } v(i) { let s = !1; for (const t of this.Ga) t.constructor === i.constructor && (s = !0); i instanceof t && !s && this.Ga.push(i); } q(t) { t instanceof u && this.Mr.push(t); } removeAllSubscriptions() { if (this.rr()) for (const t of this.Mr) t.removeAllSubscriptions(); } removeSubscription(t) { if (this.rr()) for (const i of this.Mr) i.removeSubscription(t); } ce(t) { this.Wa = t; } ao() { return this.isInitialized; } Qa() { return this.Wa; } tr(t, i) { if (!this.rr()) return null; return ir.o().Ki(t, i); } Vs() { return this.Ra; } } const r = new Yt(); export { r as default, Yt as BrazeSdkInstance, q as OPTIONS };