UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

342 lines (341 loc) 9.64 kB
import l from "./util/net.js"; import ve from "./models/braze-event.js"; import c from "./common/event-logger.js"; import { randomInclusive as a } from "./util/math.js"; import E from "./models/request-result.js"; import { logger as N, IndexedDBAdapter as tt, Guid as G, 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 vt from "./Push/utils/push-utils.js"; import h from "./util/request-header-utils.js"; export default class Lt { constructor(t, i, s, e, h, o, n, r, l, a) { (this.un = t), (this.baseUrl = i), (this.T = s), (this.an = e), (this.Cs = h), (this.B = o), (this.C = n), (this.$l = r), (this.Ra = l), (this.S = a), (this.un = t), (this.baseUrl = i), (this.Tl = 0), (this.dE = n.IE() || 0), (this.Ml = null), (this.T = s), (this.an = e), (this.Cs = h), (this.B = o), (this.C = n), (this.Ra = l), (this.S = a), (this.$l = r), (this.Pl = new u()), (this.Ol = null), (this.Ul = 50), (this._l = !1); } Jl(t, i) { return !t && !i && this.Ra.Jh() >= this.Ul; } Kl(t) { let i = this.T.zl(); 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.Bl && t && (i = !0); } } return i; } Xl(t = !1, i = !1, e = !0, o, n, r, u = !1, c = !1) { e && this.Ql(); const d = this.C.AE(), m = this.C.OE(); let f = !1; const g = (t, i, u = -1) => { const c = new Date().valueOf(); h.W(this.C, h.O.Nu, c), -1 !== u && i.push(["X-Braze-Req-Tokens-Remaining", u.toString()]); let d = !1; l.Y({ url: this.baseUrl + "/data/", data: t, headers: i, tt: (e) => { null != t.respond_with && t.respond_with.triggers && (this.Tl = Math.max(this.Tl - 1, 0)), this.S.st(t, e, i) ? (this.Ra.it(), this.B.gl(e), (null != t.respond_with && t.respond_with.user_id != this.Cs.getUserId()) || (null != t.device && this.C.Bt(s.gt.ic, t.device), null != t.sdk_metadata && (this.C.Bt(s.gt.vu, t.sdk_metadata), this.C.Bt(s.gt.pu, this.T.St())), this.$l(e), h.ju(this.C, h.O.Nu, 1), "function" == typeof o && o())) : e.auth_error && (d = !0); }, error: () => { (d = !0), null != t.respond_with && t.respond_with.triggers && (this.Tl = Math.max(this.Tl - 1, 0)), this.S.Ru(t.events, t.attributes), "function" == typeof n && n(); }, nt: (t, i) => { "function" == typeof r && r(!d); const s = this.S.Iu(i); let o = 0; if (s) switch (s.type) { case "date": o = Math.max(s.value - new Date().valueOf(), 0); break; case "timestamp": o = s.value; } if (e && !f) { if (d) { h.Pu(this.C, h.O.Nu); let t = this.Ml; (null == t || t < 1e3 * this.dE) && (t = 1e3 * this.dE); const i = Math.min(3e5, a(1e3 * this.dE, 3 * t)) + o; this.Yl(i); } else this.Yl(Math.max(1e3 * this.dE, o)); f = !0; } }, }); }, p = this.Kl(d), v = i || p; if (this.Jl(u, p)) return void N.info( "Declining to flush data due to 50 consecutive authentication failures", ); if (e && !this.S.Tu(d, m, t, v)) return this.Yl(), void ("function" == typeof r && r(!0)); const b = this.S.yu(t, v, d, m, c); v && this.Tl++; let w = !1; if (b) for (const t of b) this.S.V( t.requestData, (i) => g(t.requestData, t.headers, i), h.O.Nu, n, ), (w = !0); this.Ra.wh() && e && !w ? this.Yl() : p && (N.info("Invoking new session subscriptions"), this.Pl.X()); } Zl() { return this.Tl > 0; } Yl(t = 1e3 * this.dE) { this._l || (this.Ql(), (this.Ol = window.setTimeout(() => { if (document.hidden) { const t = "visibilitychange", i = () => { document.hidden || (document.removeEventListener(t, i, !1), this.Xl()); }; document.addEventListener(t, i, !1); } else this.Xl(); }, t)), (this.Ml = t)); } Ql() { null != this.Ol && (clearTimeout(this.Ol), (this.Ol = null)); } initialize() { (this._l = !1), this.Yl(); } destroy() { this.Pl.removeAllSubscriptions(), this.Ra.Gh(), this.Ql(), (this._l = !0), this.Xl(void 0, void 0, !1, void 0, void 0, void 0, void 0, !0), (this.Ol = null); } rn(t) { return this.Pl.Ft(t); } openSession() { const t = this.T.St() !== this.T.Qo(); t && (this.C.TE(s.iu.Ia), this.C.TE(s.iu.su)), this.Xl(void 0, !1, void 0, () => { this.C.Ut(s.gt.Ge); }), this.Jn(), t && import("./Push/push-manager-factory.js").then((t) => { if (this._l) return; const i = t.default.ea(); if ( null != i && (vt.isPushPermissionGranted() || vt.isPushBlocked()) ) { const t = () => { i.vn() ? 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.qn); (null == i || i) && t(); }, o = tt.Ls.Fs; new tt(o, N).cr(o.Ms.cu, e, h); } }); } hc() { this.C.Ut(s.gt.Xe), this.C.Ut(s.gt.Is), this.C.Ut(s.gt.Lr); } changeUser(t, i, e) { const h = this.Cs.getUserId(); if (h !== t) { this.T.Fl(), this.hc(), null != h && this.Xl(void 0, !1, void 0, void 0, void 0), this.Cs.ou(t), e ? this.Ra.setSdkAuthenticationSignature(e) : this.Ra.yh(); for (let t = 0; t < i.length; t++) i[t].changeUser(null == h); null != h && this.C.Ut(s.gt.Zt), this.C.Ut(s.gt.ic), this.C.Ut(s.gt._E), this.openSession(), N.info('Changed user to "' + t + '".'); } else { let i = "Doing nothing."; e && this.Ra.jh() !== e && (this.Ra.setSdkAuthenticationSignature(e), (i = "Updated SDK authentication signature")), N.info(`Current user is already ${t}. ${i}`); } } requestImmediateDataFlush(t) { this.Ql(), this.T.Qo(); this.Xl( void 0, void 0, void 0, void 0, () => { N.error("Failed to flush data, request will be retried automatically."); }, t, !0, ); } requestFeedRefresh() { this.T.Qo(), this.Xl(!0); } br(t, i) { this.T.Qo(), N.info("Requesting explicit trigger refresh."), this.Xl(void 0, !0, void 0, t, i); } Cn(t, i) { const e = d.dc, h = { a: t, l: i }, o = c.ut(e, h); return ( o && (N.info(`Logged alias ${t} with label ${i}`), this.C.Bt(s.gt._E, h)), o ); } Ln(t, i, s) { if (this.B.hu(i)) return ( N.info(`Custom Attribute "${i}" is blocklisted, ignoring.`), new E() ); 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, o) { const n = { latitude: i, longitude: s }; null != e && (n.altitude = e), null != h && (n.ll_accuracy = h), null != o && (n.alt_accuracy = o); const r = c.ut(d.mc, n, t || void 0); return ( r && N.info(`Set user last known location as ${JSON.stringify(n, null, 2)}`), r ); } lr(t, i) { const s = this.T.Qo(); return new ve(this.Cs.getUserId(), d.fc, t, s, { cid: i }); } vc(t, i) { return new tt(t, i); } Jn() { const t = tt.Ls.Fs; this.vc(t, N).setItem(t.Ms.Nu, 1, { baseUrl: this.baseUrl, data: { api_key: this.un, device_id: this.an.ve().id }, userId: this.Cs.getUserId(), sdkAuthEnabled: this.Ra.wh(), }); } vr(t) { for (const i of t) if (i.api_key === this.un) this.S.Ru(i.events, i.attributes); else { const t = tt.Ls.Fs; new tt(t, N).setItem(t.Ms.kr, G.ce(), i); } } Hn(t, i, s) { if (this.B.hu(t)) return ( N.info(`Custom Attribute "${t}" is blocklisted, ignoring.`), new E() ); let e, h; return ( null === i && null === s ? ((e = d.bc), (h = { key: t })) : ((e = d.wc), (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.kc, s); } }