UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

343 lines (342 loc) 9.66 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 F from "./models/request-result.js"; import { logger as N, IndexedDBAdapter as st, Guid as K, 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.an = t), (this.baseUrl = i), (this.T = s), (this.hn = e), (this.Cs = h), (this.B = o), (this.C = n), (this.$l = r), (this.Pa = l), (this.S = a), (this.an = t), (this.baseUrl = i), (this.Tl = 0), (this.dE = n.IE() || 0), (this.Ml = null), (this.T = s), (this.hn = e), (this.Cs = h), (this.B = o), (this.C = n), (this.Pa = l), (this.S = a), (this.$l = r), (this.Pl = new u()), (this.Ol = null), (this.Ul = 50), (this._l = !1); } Kl(t, i) { return !t && !i && this.Pa.Kh() >= this.Ul; } Xl(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.Fl && t && (i = !0); } } return i; } Ql(t = !1, i = !1, e = !0, o, n, r, u = !1, c = !1) { e && this.Yl(); 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.Pa.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.oc, t.device), null != t.sdk_metadata && (this.C.Bt(s.gt.pu, t.sdk_metadata), this.C.Bt(s.gt.Ru, this.T.St())), this.$l(e), h.Mu(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.gu(t.events, t.attributes), "function" == typeof n && n(); }, nt: (t, i) => { "function" == typeof r && r(!d); const s = this.S.Pu(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.Uu(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.Zl(i); } else this.Zl(Math.max(1e3 * this.dE, o)); f = !0; } }, }); }, p = this.Xl(d), v = i || p; if (this.Kl(u, p)) return void N.info( "Declining to flush data due to 50 consecutive authentication failures", ); if (e && !this.S.yu(d, m, t, v)) return this.Zl(), void ("function" == typeof r && r(!0)); const b = this.S.wu(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.Pa.jh() && e && !w ? this.Zl() : p && (N.info("Invoking new session subscriptions"), this.Pl.X()); } dc() { return this.Tl > 0; } Zl(t = 1e3 * this.dE) { this._l || (this.Yl(), (this.Ol = window.setTimeout(() => { if (document.hidden) { const t = "visibilitychange", i = () => { document.hidden || (document.removeEventListener(t, i, !1), this.Ql()); }; document.addEventListener(t, i, !1); } else this.Ql(); }, t)), (this.Ml = t)); } Yl() { null != this.Ol && (clearTimeout(this.Ol), (this.Ol = null)); } initialize() { (this._l = !1), this.Zl(); } destroy() { this.Pl.removeAllSubscriptions(), this.Pa.Hh(), this.Yl(), (this._l = !0), this.Ql(void 0, void 0, !1, void 0, void 0, void 0, void 0, !0), (this.Ol = null); } on(t) { return this.Pl.Dt(t); } openSession() { const t = this.T.St() !== this.T.Qo(); t && (this.C.TE(s.iu.Na), this.C.TE(s.iu.su)), this.Ql(void 0, !1, void 0, () => { this.C.Ut(s.gt.Ge); }), this.Qn(), 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.wn() ? 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.zn); (null == i || i) && t(); }, o = st.Ls.Fs; new st(o, N).cr(o.Ms.cu, e, h); } }); } mc() { this.C.Ut(s.gt.Xe), this.C.Ut(s.gt.Hs), this.C.Ut(s.gt.Kr); } changeUser(t, i, e) { const h = this.Cs.getUserId(); if (h !== t) { this.T.Il(), this.mc(), null != h && this.Ql(void 0, !1, void 0, void 0, void 0), this.Cs.ou(t), e ? this.Pa.setSdkAuthenticationSignature(e) : this.Pa.Bh(); for (let t = 0; t < i.length; t++) i[t].changeUser(null == h); this.S.fo(), null != h && this.C.Ut(s.gt.Zt), this.C.Ut(s.gt.oc), this.C.Ut(s.gt._E), this.openSession(), N.info('Changed user to "' + t + '".'); } else { let i = "Doing nothing."; e && this.Pa.xh() !== e && (this.Pa.setSdkAuthenticationSignature(e), (i = "Updated SDK authentication signature")), N.info(`Current user is already ${t}. ${i}`); } } requestImmediateDataFlush(t) { this.Yl(), this.T.Qo(); this.Ql( 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.Ql(!0); } br(t, i) { this.T.Qo(), N.info("Requesting explicit trigger refresh."), this.Ql(void 0, !0, void 0, t, i); } Cn(t, i) { const e = d.fc, 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 F() ); 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.vc, 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.bc, t, s, { cid: i }); } wc(t, i) { return new st(t, i); } Qn() { const t = st.Ls.Fs; this.wc(t, N).setItem(t.Ms.Nu, 1, { baseUrl: this.baseUrl, data: { api_key: this.an, device_id: this.hn.ve().id }, userId: this.Cs.getUserId(), sdkAuthEnabled: this.Pa.jh(), }); } vr(t) { for (const i of t) if (i.api_key === this.an) this.S.gu(i.events, i.attributes); else { const t = st.Ls.Fs; new st(t, N).setItem(t.Ms.kr, K.ce(), i); } } Kn(t, i, s) { if (this.B.hu(t)) return ( N.info(`Custom Attribute "${t}" is blocklisted, ignoring.`), new F() ); let e, h; return ( null === i && null === s ? ((e = d.kc), (h = { key: t })) : ((e = d.yc), (h = { key: t, latitude: i, longitude: s })), c.ut(e, h) ); } Mn(t, i) { const s = { group_id: t, status: i }; return c.ut(d.jc, s); } }