UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

355 lines (354 loc) 9.97 kB
import l from "./util/net.js"; import ue from "./models/braze-event.js"; import c from "./common/event-logger.js"; import { randomInclusive as a } from "./util/math.js"; import G from "./models/request-result.js"; import { logger as N, IndexedDBAdapter as et, Guid as O, EventTypes as d, } from "../shared-lib/index.js"; import { STORAGE_KEYS as s } from "./managers/storage-manager.js"; import u from "./managers/subscription-manager.js"; import yt from "./Push/utils/push-utils.js"; import h from "./util/request-header-utils.js"; export default class Wt { constructor(t, i, s, e, h, n, o, r, l, a) { (this.on = t), (this.baseUrl = i), (this.T = s), (this.un = e), (this.Cs = h), (this.B = n), (this.C = o), (this.$u = r), (this.Na = l), (this.S = a), (this.on = t), (this.baseUrl = i), (this.Tu = 0), (this.fE = o.dE() || 0), (this.Mu = null), (this.T = s), (this.un = e), (this.Cs = h), (this.B = n), (this.C = o), (this.Na = l), (this.S = a), (this.$u = r), (this.Pu = new u()), (this.Ou = null), (this.Uu = 50), (this._u = !1), (this.Xu = !1); } Qu(t, i) { return !t && !i && this.Na.Jh() >= this.Uu; } Yu(t) { let i = this.T.zu(); if (t.length > 0) { const s = this.Cs.getUserId(); for (const e of t) { const t = (!e.userId && !s) || e.userId === s; e.type === d.Iu && t && (i = !0); } } return i; } Zu(t = !1, i = !0, e, n, o, r = !1, u = !1) { i && this.sc(); const c = this.C.RE(), d = this.C.IE(); let m = !1; const f = (t, r, u = -1) => { const c = new Date().valueOf(); h.W(this.C, h.O.gl, c), -1 !== u && r.push(["X-Braze-Req-Tokens-Remaining", u.toString()]); let d = !1; l.Y({ url: this.baseUrl + "/data/", data: t, headers: r, tt: (i) => { null != t.respond_with && t.respond_with.triggers && (this.Tu = Math.max(this.Tu - 1, 0)), this.S.st(t, i, r) ? (this.Na.it(), this.B.fu(i), (null != t.respond_with && t.respond_with.user_id != this.Cs.getUserId()) || (null != t.device && this.C.Bt(s.gt.Za, t.device), null != t.sdk_metadata && (this.C.Bt(s.gt.rl, t.sdk_metadata), this.C.Bt(s.gt.ol, this.T.St())), this.$u(i), h.yl(this.C, h.O.gl, 1), "function" == typeof e && e())) : i.auth_error && (d = !0); }, error: () => { (d = !0), null != t.respond_with && t.respond_with.triggers && (this.Tu = Math.max(this.Tu - 1, 0)), this.S.al(t.events, t.attributes), "function" == typeof n && n(); }, nt: (t, s) => { "function" == typeof o && o(!d); const e = this.S.Ml(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.$l(this.C, h.O.gl); let t = this.Mu; (null == t || t < 1e3 * this.fE) && (t = 1e3 * this.fE); const i = Math.min(3e5, a(1e3 * this.fE, 3 * t)) + n; this.hc(i); } else this.hc(Math.max(1e3 * this.fE, n)); m = !0; } }, }); }, g = this.Yu(c), v = t || g; if (this.Qu(r, g)) return void N.info( "Declining to flush data due to 50 consecutive authentication failures", ); if (i && !this.S.vl(c, d, v)) return this.hc(), void ("function" == typeof o && o(!0)); const p = this.S.Rl(v, c, d, u); v && this.Tu++; let b = !1; if (p) for (const t of p) this.S.V( t.requestData, (i) => f(t.requestData, t.headers, i), h.O.gl, n, ), (b = !0); this.Na.wh() && i && !b ? this.hc() : g && (N.info("Invoking new session subscriptions"), this.Pu.X(), (this.Xu = !0)); } nc() { return this.Tu > 0; } hc(t = 1e3 * this.fE) { this._u || (this.sc(), (this.Ou = window.setTimeout(() => { if (document.hidden) { const t = "visibilitychange", i = () => { document.hidden || (document.removeEventListener(t, i, !1), this.Zu()); }; document.addEventListener(t, i, !1); } else this.Zu(); }, t)), (this.Mu = t)); } sc() { null != this.Ou && (clearTimeout(this.Ou), (this.Ou = null)); } initialize() { (this._u = !1), this.hc(); } destroy() { this.Pu.removeAllSubscriptions(), this.Na.Gh(), this.sc(), (this._u = !0), this.Zu(void 0, !1, void 0, void 0, void 0, void 0, !0), (this.Ou = null), (this.Xu = !1); } rn(t) { return this.Xu ? (t(), null) : this.Pu.Dt(t); } openSession() { const t = this.T.St() !== this.T.Vo(); t && (this.C.hE(s.iu.Qh), this.C.hE(s.iu.su)), this.Zu(!1, void 0, () => { this.C.Ut(s.gt.Ge); }), this.Jn(), t && import("./Push/push-manager-factory.js").then((t) => { if (this._u) return; const i = t.default.ea(); if ( null != i && (yt.isPushPermissionGranted() || yt.isPushBlocked()) ) { const t = () => { i.gn() ? N.info( "Push token maintenance is disabled, not refreshing token for backend.", ) : i.subscribe(); }, e = (i, s) => { s && t(); }, h = () => { const i = this.C.ft(s.gt.Vn); (null == i || i) && t(); }, n = et.Ds.Us; new et(n, N).jr(n.Ls.cu, e, h); } }); } lc() { this.C.Ut(s.gt.Xe), this.C.Ut(s.gt.Xs), this.C.Ut(s.gt.Fr); } uc() { var t, i; this.C.Ut(s.gt.zl), this.C.Ut(s.gt.SE); const e = h.O; for (const i in e) { const s = e[i]; this.S.El(s, null === (t = this.B) || void 0 === t ? void 0 : t._l(s)); } this.S.Xl(null === (i = this.B) || void 0 === i ? void 0 : i.Ol()); } changeUser(t, i, e) { const h = this.Cs.getUserId(); if (h !== t) { this.T.Ju(), this.lc(), null != h && this.Zu(void 0, !1, void 0, void 0, void 0), this.Cs.ou(t), e ? this.Na.setSdkAuthenticationSignature(e) : this.Na.yh(); for (let t = 0; t < i.length; t++) i[t].changeUser(null == h); this.S.fo(), null != h && this.C.Ut(s.gt.rE), this.C.Ut(s.gt.Za), this.C.Ut(s.gt.uE), this.uc(), this.openSession(), N.info('Changed user to "' + t + '".'); } else { let i = "Doing nothing."; e && this.Na.jh() !== e && (this.Na.setSdkAuthenticationSignature(e), (i = "Updated SDK authentication signature")), N.info(`Current user is already ${t}. ${i}`); } } requestImmediateDataFlush(t) { this.sc(), this.T.Vo(); this.Zu( void 0, void 0, void 0, () => { N.error("Failed to flush data, request will be retried automatically."); }, t, !0, ); } vr(t, i) { this.T.Vo(), N.info("Requesting explicit trigger refresh."), this.Zu(!0, void 0, t, i); } En(t, i) { const e = d.cc, h = { a: t, l: i }, n = c.ut(e, h); return ( n && (N.info(`Logged alias ${t} with label ${i}`), this.C.Bt(s.gt.uE, h)), n ); } Ln(t, i, s) { if (this.B.hu(i)) return ( N.info(`Custom Attribute "${i}" is blocklisted, ignoring.`), new G() ); const e = { key: i, value: s }, h = c.ut(t, e); if (h) { const t = "object" == typeof s ? JSON.stringify(s, null, 2) : s; N.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 = c.ut(d.dc, o, t || void 0); return ( r && N.info(`Set user last known location as ${JSON.stringify(o, null, 2)}`), r ); } br(t, i) { const s = this.T.Vo(); return new ue(this.Cs.getUserId(), d.mc, t, s, { cid: i }); } fc(t, i) { return new et(t, i); } Jn() { const t = et.Ds.Us; this.fc(t, N).setItem(t.Ls.gl, 1, { baseUrl: this.baseUrl, data: { api_key: this.on, device_id: this.un.ve().id }, userId: this.Cs.getUserId(), sdkAuthEnabled: this.Na.wh(), }); } wr(t) { for (const i of t) if (i.api_key === this.on) this.S.al(i.events, i.attributes); else { const t = et.Ds.Us; new et(t, N).setItem(t.Ls.$r, O.ce(), i); } } Hn(t, i, s) { if (this.B.hu(t)) return ( N.info(`Custom Attribute "${t}" is blocklisted, ignoring.`), new G() ); let e, h; return ( null === i && null === s ? ((e = d.gc), (h = { key: t })) : ((e = d.vc), (h = { key: t, latitude: i, longitude: s })), c.ut(e, h) ); } Kn(t, i) { const s = { group_id: t, status: i }; return c.ut(d.bc, s); } }