UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

356 lines (355 loc) 10 kB
import { EventTypes as p, Guid as V, IndexedDBAdapter as et, logger as E, } from "../shared-lib/index.js"; import v from "./common/event-logger.js"; import { removeAllVisibleInAppMessages as fi } from "./InAppMessage/ui/remove-all-visible-in-app-messages.js"; import { STORAGE_KEYS as s } from "./managers/storage-manager.js"; import f from "./managers/subscription-manager.js"; import Ie from "./models/braze-event.js"; import L from "./models/request-result.js"; import It from "./Push/utils/push-utils.js"; import { randomInclusive as a } from "./util/math.js"; import l from "./util/net.js"; import h from "./util/request-header-utils.js"; export default class Kt { constructor(t, i, s, e, h, n, o, r, l, u) { (this.tu = t), (this.baseUrl = i), (this.j = s), (this.eu = e), (this.Ss = h), (this.h = n), (this.C = o), (this.bc = r), (this.Gh = l), (this.B = u), (this.tu = t), (this.baseUrl = i), (this.kc = 0), (this.dS = o.RS() || 0), (this.yc = null), (this.j = s), (this.eu = e), (this.Ss = h), (this.h = n), (this.C = o), (this.Gh = l), (this.B = u), (this.bc = r), (this.jc = new f()), (this.Ac = null), (this.Dc = 50), (this.$c = !1), (this.qc = !1); } Cc(t, i) { return !t && !i && this.Gh.Ih() >= this.Dc; } Mc(t) { let i = this.j.am(); if (t.length > 0) { const s = this.Ss.getUserId(); for (const e of t) { const t = (!e.userId && !s) || e.userId === s; e.type === p.wm && t && (i = !0); } } return i; } Tc(t = !1, i = !0, e, n, o, r = !1, u = !1) { i && this.Lc(); const c = this.C.TS(), d = this.C.gS(); let m = !1; const f = (t, r, u = -1) => { const c = new Date().valueOf(); h.nt(this.C, h.it.pn, c), -1 !== u && r.push(["X-Braze-Req-Tokens-Remaining", u.toString()]); let d = !1; l.ot({ url: this.baseUrl + "/data/", data: t, headers: r, lt: (i) => { null != t.respond_with && t.respond_with.triggers && (this.kc = Math.max(this.kc - 1, 0)), this.B.ut(t, i, r) ? (this.Gh.ct(), this.h.sc(i), (null != t.respond_with && t.respond_with.user_id != this.Ss.getUserId()) || (null != t.device && this.C.It(s.Tt.Ba, t.device), null != t.sdk_metadata && (this.C.It(s.Tt.Ka, t.sdk_metadata), this.C.It(s.Tt.Pa, this.j.kt())), this.bc(i), h.Dl(this.C, h.it.pn, 1), "function" == typeof e && e())) : i.auth_error && (d = !0); }, error: () => { (d = !0), null != t.respond_with && t.respond_with.triggers && (this.kc = Math.max(this.kc - 1, 0)), this.B.Ua(t.events, t.attributes), "function" == typeof n && n(); }, ft: (t, s) => { "function" == typeof o && o(!d); const e = this.B.Bl(s); let n = 0; if (e) switch (e.type) { case "date": n = Math.max(e.value - new Date().valueOf(), 0); break; case "timestamp": n = e.value; } if (i && !m) { if (d) { h.zl(this.C, h.it.pn); const t = this.h.vt(), i = this.h.gt(), s = this.h.bt(); let e = this.yc; (null == e || e < t) && (e = t); const o = Math.min(s, a(t, e * i)) + n; this.Fc(o); } else this.Fc(Math.max(1e3 * this.dS, n)); m = !0; } }, }); }, g = this.Mc(c), p = t || g; if (this.Cc(r, g)) return void E.info( "Declining to flush data due to 50 consecutive authentication failures", ); if (i && !this.B.fl(c, d, p)) return this.Fc(), void ("function" == typeof o && o(!0)); const v = this.B.ml(p, c, d, u); p && this.kc++; let b = !1; if (v) for (const t of v) this.B.et( t.requestData, (i) => f(t.requestData, t.headers, i), h.it.pn, n, ), (b = !0); this.Gh.Fh() && i && !b ? this.Fc() : g && (E.info("Invoking new session subscriptions"), this.jc.A(), (this.qc = !0)); } Ic() { return this.kc > 0; } Fc(t = 1e3 * this.dS) { this.$c || (this.Lc(), (this.Ac = window.setTimeout(() => { if (document.hidden) { const t = "visibilitychange", i = () => { document.hidden || (document.removeEventListener(t, i, !1), this.Tc()); }; document.addEventListener(t, i, !1); } else this.Tc(); }, t)), (this.yc = t)); } Lc() { null != this.Ac && (clearTimeout(this.Ac), (this.Ac = null)); } initialize() { (this.$c = !1), this.Fc(); } destroy() { this.jc.removeAllSubscriptions(), this.Gh.xh(), this.Lc(), (this.$c = !0), this.Tc(void 0, !1, void 0, void 0, void 0, void 0, !0), (this.Ac = null), (this.qc = !1); } rn(t) { return this.qc ? (t(), null) : this.jc.Kt(t); } openSession() { const t = this.j.kt() !== this.j.el(); t && (this.C.uS(s.Ou.Wh), this.C.uS(s.Ou.Cu)), this.Tc(!1, void 0, () => { t && (this.C.Qt(s.Tt.jo), this.C.Qt(s.Tt.Wt)); }), this.Ea(), t && import("./Push/push-manager-factory.js").then((t) => { if (this.$c) return; const i = t.default.ra(); if ( null != i && (It.isPushPermissionGranted() || It.isPushBlocked()) ) { const t = () => { i.du() ? E.info( "Push token maintenance is disabled, not refreshing token for backend.", ) : i.subscribe(); }, e = (i, s) => { s && t(); }, h = () => { const i = this.C.Rt(s.Tt.Uu); (null == i || i) && t(); }, n = et.Ps.$s; new et(n, E).kr(n.Os.Fu, e, h); } }); } Pc() { this.C.Qt(s.Tt.Fo), this.C.Qt(s.Tt.Ci), this.C.Qt(s.Tt.pa); } xc() { this.C.Qt(s.Tt.Cl), this.C.Qt(s.Tt.iS), this.C.Qt(s.Tt.Ml), this.C.Qt(s.Tt.Ll); } changeUser(t, i, e) { const h = this.Ss.getUserId(); if (h !== t) { this.j.Sm(), this.Pc(), fi(), null != h && this.Tc(void 0, !1, void 0, void 0, void 0), this.Ss.Ju(t), e ? this.Gh.setSdkAuthenticationSignature(e) : this.Gh.jh(); for (let t = 0; t < i.length; t++) i[t].changeUser(null == h); this.B.fo(), null != h && this.C.Qt(s.Tt.tS), this.C.Qt(s.Tt.Ba), this.C.Qt(s.Tt.SS), this.xc(), this.openSession(), E.info('Changed user to "' + t + '".'); } else { let i = "Doing nothing."; e && this.Gh.kh() !== e && (this.Gh.setSdkAuthenticationSignature(e), (i = "Updated SDK authentication signature")), E.info(`Current user is already ${t}. ${i}`); } } requestImmediateDataFlush(t) { this.Lc(), this.j.el(); this.Tc( void 0, void 0, void 0, () => { E.error("Failed to flush data, request will be retried automatically."); }, t, !0, ); } Sr(t, i) { this.j.el(), E.info("Requesting explicit trigger refresh."), this.Tc(!0, void 0, t, i); } Gu(t, i) { const e = p.Nc, h = { a: t, l: i }, n = v.wt(e, h); return ( n && (E.info(`Logged alias ${t} with label ${i}`), this.C.It(s.Tt.SS, h)), n ); } Mu(t, i, s) { if (this.h.qu(i)) return ( E.info(`Custom Attribute "${i}" is blocklisted, ignoring.`), new L() ); const e = { key: i, value: s }, h = v.wt(t, e); if (h) { const t = "object" == typeof s ? JSON.stringify(s, null, 2) : s; E.info(`Logged custom attribute: ${i} with value: ${t}`); } return h; } setLastKnownLocation(t, i, s, e, h, n) { const o = { latitude: i, longitude: s }; null != e && (o.altitude = e), null != h && (o.ll_accuracy = h), null != n && (o.alt_accuracy = n); const r = v.wt(p.Oc, o, t || void 0); return ( r && E.info(`Set user last known location as ${JSON.stringify(o, null, 2)}`), r ); } yr(t, i) { const s = this.j.el(); return new Ie(this.Ss.getUserId(), p.Uc, t, s, { cid: i }); } Bc(t, i) { return new et(t, i); } Ea() { const t = et.Ps.$s; this.Bc(t, E).setItem(t.Os.pn, 1, { baseUrl: this.baseUrl, data: { api_key: this.tu, device_id: this.eu.ve().id }, userId: this.Ss.getUserId(), sdkAuthEnabled: this.Gh.Fh(), }); } Ar(t) { for (const i of t) if (i.api_key === this.tu) this.B.Ua(i.events, i.attributes); else { const t = et.Ps.$s; new et(t, E).setItem(t.Os.qr, V.de(), i); } } Na(t, i, s) { if (this.h.qu(t)) return ( E.info(`Custom Attribute "${t}" is blocklisted, ignoring.`), new L() ); let e, h; return ( null === i && null === s ? ((e = p.Jc), (h = { key: t })) : ((e = p.Kc), (h = { key: t, latitude: i, longitude: s })), v.wt(e, h) ); } va(t, i) { const s = { group_id: t, status: i }; return v.wt(p.Wc, s); } }