UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

353 lines (352 loc) 9.29 kB
import Gt from "./auth-manager.js"; import t from "../common/base-provider.js"; import ro from "../util/browser-detector.js"; import Pt from "./device-manager.js"; import DeviceProperties from "../Core/device-properties.js"; import { isArray as g, keys as D, validateValueIsFromEnum as ta, values as Wt, } from "../util/code-utils.js"; import Vt from "./network-manager.js"; import Kt from "../request-controller.js"; import Yt from "./server-config-manager.js"; import Xt from "./session-manager.js"; import ne, { STORAGE_KEYS as s } from "./storage-manager.js"; import Zt from "./storage-manager-factory.js"; import f from "./subscription-manager.js"; import { TriggersProviderFactory as rt } from "../triggers/triggers-provider-factory.js"; import sr from "../DUST/dust-provider-factory.js"; import { subscribeToDust as Qt } from "../DUST/subscribe-to-dust.js"; import Ut from "../User/user-manager.js"; import { User } from "../User/index.js"; import { parseQueryStringKeyValues as lt } from "../util/url-utils.js"; import { WindowUtils as no } from "../util/window-utils.js"; import { CoreStrings as R } from "../common/constants.js"; import { SupportedOptions as hi, logger as E } from "../../shared-lib/index.js"; import _t from "../models/identifier.js"; const U = { _h: "allowCrawlerActivity", Nh: "baseUrl", se: "cookieExpiryInDays", Oh: "noCookies", Th: "devicePropertyAllowlist", ka: "disablePushTokenMaintenance", Rh: "enableLogging", Ph: "enableSdkAuthentication", _a: "manageServiceWorkerExternally", Dh: "minimumIntervalBetweenTriggerActionsInSeconds", Lh: "sessionTimeoutInSeconds", yh: "appVersion", Mh: "appVersionNumber", xa: "serviceWorkerLocation", Ma: "safariWebsitePushId", qa: "localization", sr: "contentSecurityNonce", nr: "allowUserSuppliedJavascript", ja: "inAppMessageZIndex", ba: "openInAppMessagesInNewTab", tn: "openCardsInNewTab", oh: "requireExplicitInAppMessageDismissal", Uh: "doNotLoadFontAwesome", Wh: "deviceId", ya: "serviceWorkerScope", Qe: "dustHost", Bh: "sdkFlavor", }; class li { constructor() { (this.tu = ""), (this.Vh = ""), (this.Kh = void 0), (this.Gh = null), (this.eu = null), (this.B = null), (this.Ru = null), (this.h = null), (this.j = null), (this.C = null), (this.Ss = null), (this.Yh = ""), (this.isInitialized = !1), (this.$h = !1), (this.qh = new f()), (this.Hh = new f()), (this.options = {}), (this.Jh = []), (this.Xh = []), (this.In = []), (this.Vh = "6.7.1"); } Zh(t) { this.qh.Kt(t); } uh(t) { this.Hh.Kt(t); } initialize(t, i) { var e, r, o; if (this.ao()) return E.info("Braze has already been initialized with an API key."), !0; this.options = i || {}; let n = this.er(U.Rh); const h = lt(no.Qh()); if ( (h && "true" === h.brazeLogging && (n = !0), E.init(n), 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.tu = t; let l = this.er(U.Nh); if (null == l || "" === l || "string" != typeof l) return E.error("Braze requires a valid baseUrl to be initialized."), !1; !1 === /^https?:/.test(l) && (l = `https://${l}`); const a = l; if ( ((l = document.createElement("a")), (l.href = a), "/" === l.pathname && (l = `${l}api/v3`), (this.Yh = l.toString()), ro.il && !this.er(U._h)) ) return ( E.info("Ignoring activity from crawler bot " + navigator.userAgent), (this.$h = !0), !1 ); const u = this.er(U.Oh) || !1, c = this.er(U.se), m = Zt.sl(); if ( ((this.C = Zt.rl(t, u, c)), u && this.C.hl(t), new ne.le(null, !0).wr(s.ce)) ) return ( E.info("Ignoring all activity due to previous opt out"), (this.$h = !0), !1 ); for (const t of D(this.options)) -1 === Wt(hi).indexOf(t) && E.warn(`Ignoring unknown initialization option '${t}'.`); const p = ["mparticle", "wordpress", "tealium"]; if (null != this.er(U.Bh)) { const t = this.er(U.Bh); -1 !== p.indexOf(t) ? (this.Kh = t) : E.error("Invalid sdk flavor passed: " + t); } let d = this.er(hi.Th); if (null != d) if (g(d)) { const t = []; for (let i = 0; i < d.length; i++) ta( DeviceProperties, d[i], "devicePropertyAllowlist contained an invalid value.", "DeviceProperties", ) && t.push(d[i]); d = t; } else E.error( "devicePropertyAllowlist must be an array. Defaulting to all properties.", ), (d = null); const I = this.er(U.Wh); if (I) { const t = new _t(I); this.C.Iu(s.Ou.Wh, t); } (this.eu = new Pt(this.C, d)), (this.h = new Yt(this.C)), (this.Ss = new Ut(this.h, this.C)), (this.j = new Xt(this.C, this.Ss, this.h, this.er(U.Lh))); const _ = new f(); (this.Gh = new Gt(this.C, this.er(U.Ph), _)), this.S(_), (this.B = new Vt( this.eu, this.C, this.Gh, this.Ss, this.j, this.h, this.tu, this.Yh, this.Vh, this.Kh || "", this.er(U.yh), this.er(U.Mh), )), (this.Ru = new Kt( this.tu, this.Yh, this.j, this.eu, this.Ss, this.h, this.C, (t) => { if (this.ao()) for (const i of this.vr()) i.I(t); }, this.Gh, this.B, )), this.Ru.initialize(), u || this.C.ll(), E.info( `Initialized for the Braze backend at "${this.er( U.Nh, )}" with API key "${this.tu}".`, ), rt.t(), Qt(), this.h.bo(() => { var t; this.isInitialized && (null === (t = this.h) || void 0 === t ? void 0 : t.lo()) && import("../FeatureFlags/refresh-feature-flags.js").then((t) => { if (!this.isInitialized) return; (0, t.default)(); }); }), this.Ru.rn(() => { var t; this.isInitialized && (null === (t = this.h) || void 0 === t ? void 0 : t.lo()) && import("../FeatureFlags/refresh-feature-flags.js").then((t) => { if (!this.isInitialized) return; (0, t.default)(void 0, void 0, !0); }); }), this.qh.A(this.options), (this.isInitialized = !0), window.dispatchEvent(new CustomEvent("braze.initialized")); const S = null === (e = this.j) || void 0 === e ? void 0 : e.al(); return ( m || null == S || (null === (r = this.j) || void 0 === r ? void 0 : r.ul(new Date().valueOf(), S)) || null === (o = this.Ru) || void 0 === o || o.Sr(), !0 ); } destroy(t) { if ((E.destroy(), this.ao())) { this.Hh.A(), this.Hh.removeAllSubscriptions(); for (const t of this.Jh) t.destroy(); this.Jh = []; for (const t of this.Xh) t.clearData(!1); this.B && this.B.fo(), (this.Xh = []), this.removeAllSubscriptions(), (this.In = []), null != this.Ru && this.Ru.destroy(), (this.Ru = null), (this.Gh = null), (this.eu = null), (this.B = null), (this.h = null), (this.j = null), (this.Ss = null), (this.options = {}), (this.Kh = void 0), (this.isInitialized = !1), (this.$h = !1), t && (this.C = null); } } rr() { return !this.cl() && (!!this.ao() || (console.warn(R.ee), !1)); } za() { return this.tu; } Br() { return this.Gh; } ht() { return this.Yh; } ue() { return this.eu; } m() { return this.B; } er(t) { return this.options[t]; } vr() { return this.Xh; } nn() { return this.Ru; } l() { return this.h; } u() { return this.j; } p() { return this.C; } zr() { if (this.Ss && this.Ru) return new User(this.Ss, this.Ru); } ir() { return this.Ss; } ar() { return !0 === this.er(U.nr); } g(t) { let i = !1; for (const s of this.Jh) s === t && (i = !0); i || this.Jh.push(t); } v(i) { let s = !1; for (const t of this.Xh) t.constructor === i.constructor && (s = !0); i instanceof t && !s && this.Xh.push(i); } S(t) { t instanceof f && this.In.push(t); } removeAllSubscriptions() { if (this.rr()) for (const t of this.In) t.removeAllSubscriptions(); } removeSubscription(t) { if (this.rr()) for (const i of this.In) i.removeSubscription(t); } fe(t) { this.$h = t; } ao() { return this.isInitialized; } cl() { return this.$h; } dr(t, i) { if (!this.rr()) return null; return sr.o().Ze(t, i); } zi() { return this.Vh; } } const r = new li(); export { r as default, li as BrazeSdkInstance, U as OPTIONS };