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 b, } from "../shared-lib/index.js"; import v from "./common/event-logger.js"; import { removeAllVisibleInAppMessages as gi } 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.C = s), (this.eu = e), (this.Ss = h), (this.h = n), (this.j = o), (this.hc = r), (this.Gh = l), (this.B = u), (this.tu = t), (this.baseUrl = i), (this.nc = 0), (this.IS = o.dS() || 0), (this.lc = null), (this.C = s), (this.eu = e), (this.Ss = h), (this.h = n), (this.j = o), (this.Gh = l), (this.B = u), (this.hc = r), (this.uc = new f()), (this.cc = null), (this.dc = 50), (this.mc = !1), (this.fc = !1); } gc(t, i) { return !t && !i && this.Gh.Ih() >= this.dc; } vc(t) { let i = this.C.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; } bc(t = !1, i = !0, e, n, o, r = !1, u = !1) { i && this.wc(); const c = this.j.hS(), d = this.j.RS(); let m = !1; const f = (t, r, u = -1) => { const c = new Date().valueOf(); h.nt(this.j, 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.nc = Math.max(this.nc - 1, 0)), this.B.ut(t, i, r) ? (this.Gh.ct(), this.h.ld(i), (null != t.respond_with && t.respond_with.user_id != this.Ss.getUserId()) || (null != t.device && this.j.Pt(s.It.Ba, t.device), null != t.sdk_metadata && (this.j.Pt(s.It.Ka, t.sdk_metadata), this.j.Pt(s.It.Pa, this.C.$t())), this.hc(i), h.Dl(this.j, 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.nc = Math.max(this.nc - 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.Cl(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.j, h.it.pn); const t = this.h.vt(), i = this.h.gt(), s = this.h.bt(); let e = this.lc; (null == e || e < t) && (e = t); const o = Math.min(s, a(t, e * i)) + n; this.kc(o); } else this.kc(Math.max(1e3 * this.IS, n)); m = !0; } }, }); }, g = this.vc(c), p = t || g; if (this.gc(r, g)) return void b.info( "Declining to flush data due to 50 consecutive authentication failures", ); if (i && !this.B.fl(c, d, p)) return this.kc(), void ("function" == typeof o && o(!0)); const v = this.B.ml(p, c, d, u); p && this.nc++; let w = !1; if (v) for (const t of v) this.B.et( t.requestData, (i) => f(t.requestData, t.headers, i), h.it.pn, n, ), (w = !0); this.Gh.Fh() && i && !w ? this.kc() : g && (b.info("Invoking new session subscriptions"), this.uc.A(), (this.fc = !0)); } yc() { return this.nc > 0; } kc(t = 1e3 * this.IS) { this.mc || (this.wc(), (this.cc = window.setTimeout(() => { if (document.hidden) { const t = "visibilitychange", i = () => { document.hidden || (document.removeEventListener(t, i, !1), this.bc()); }; document.addEventListener(t, i, !1); } else this.bc(); }, t)), (this.lc = t)); } wc() { null != this.cc && (clearTimeout(this.cc), (this.cc = null)); } initialize() { (this.mc = !1), this.kc(); } destroy() { this.uc.removeAllSubscriptions(), this.Gh.xh(), this.wc(), (this.mc = !0), this.bc(void 0, !1, void 0, void 0, void 0, void 0, !0), (this.cc = null), (this.fc = !1); } rn(t) { return this.fc ? (t(), null) : this.uc.Ut(t); } openSession() { const t = this.C.$t() !== this.C.el(); t && (this.j.cS(s.Ou.Wh), this.j.cS(s.Ou.Cu)), this.bc(!1, void 0, () => { t && (this.j.Vt(s.It.jo), this.j.Vt(s.It.At)); }), this.Ea(), t && import("./Push/push-manager-factory.js").then((t) => { if (this.mc) return; const i = t.default.ra(); if ( null != i && (It.isPushPermissionGranted() || It.isPushBlocked()) ) { const t = () => { i.du() ? b.info( "Push token maintenance is disabled, not refreshing token for backend.", ) : i.subscribe(); }, e = (i, s) => { s && t(); }, h = () => { const i = this.j.St(s.It.Uu); (null == i || i) && t(); }, n = et._s.Xs; new et(n, b).kr(n.Ws.Fu, e, h); } }); } jc() { this.j.Vt(s.It.Fo), this.j.Vt(s.It.bi), this.j.Vt(s.It.pa); } Sc() { this.j.Vt(s.It.Bl), this.j.Vt(s.It.ES), this.j.Vt(s.It.Ml), this.j.Vt(s.It.Ll); } changeUser(t, i, e) { const h = this.Ss.getUserId(); if (h !== t) { this.C.Sm(), this.jc(), gi(), null != h && this.bc(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.j.Vt(s.It.eS), this.j.Vt(s.It.Ba), this.j.Vt(s.It.lS), this.Sc(), this.openSession(), b.info('Changed user to "' + t + '".'); } else { let i = "Doing nothing."; e && this.Gh.kh() !== e && (this.Gh.setSdkAuthenticationSignature(e), (i = "Updated SDK authentication signature")), b.info(`Current user is already ${t}. ${i}`); } } requestImmediateDataFlush(t) { this.wc(), this.C.el(); this.bc( void 0, void 0, void 0, () => { b.error("Failed to flush data, request will be retried automatically."); }, t, !0, ); } Ar(t, i) { this.C.el(), b.info("Requesting explicit trigger refresh."), this.bc(!0, void 0, t, i); } Gu(t, i) { const e = p.Ac, h = { a: t, l: i }, n = v.Dt(e, h); return ( n && (b.info(`Logged alias ${t} with label ${i}`), this.j.Pt(s.It.lS, h)), n ); } Mu(t, i, s) { if (this.h.qu(i)) return ( b.info(`Custom Attribute "${i}" is blocklisted, ignoring.`), new L() ); const e = { key: i, value: s }, h = v.Dt(t, e); if (h) { const t = "object" == typeof s ? JSON.stringify(s, null, 2) : s; b.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.Dt(p.Dc, o, t || void 0); return ( r && b.info(`Set user last known location as ${JSON.stringify(o, null, 2)}`), r ); } Fr(t, i) { const s = this.C.el(); return new Ie(this.Ss.getUserId(), p.$c, t, s, { cid: i }); } qc(t, i) { return new et(t, i); } Ea() { const t = et._s.Xs; this.qc(t, b).setItem(t.Ws.pn, 1, { baseUrl: this.baseUrl, data: { api_key: this.tu, device_id: this.eu.ve().id }, userId: this.Ss.getUserId(), sdkAuthEnabled: this.Gh.Fh(), }); } Dr(t) { for (const i of t) if (i.api_key === this.tu) this.B.Ua(i.events, i.attributes); else { const t = et._s.Xs; new et(t, b).setItem(t.Ws.Br, V.de(), i); } } Na(t, i, s) { if (this.h.qu(t)) return ( b.info(`Custom Attribute "${t}" is blocklisted, ignoring.`), new L() ); let e, h; return ( null === i && null === s ? ((e = p.Cc), (h = { key: t })) : ((e = p.Mc), (h = { key: t, latitude: i, longitude: s })), v.Dt(e, h) ); } va(t, i) { const s = { group_id: t, status: i }; return v.Dt(p.Tc, s); } }