@braze/web-sdk
Version:
Braze SDK for web sites and other JS platforms.
357 lines (356 loc) • 10.1 kB
JavaScript
import a from "./util/net.js";
import ue from "./models/braze-event.js";
import c from "./common/event-logger.js";
import { randomInclusive as l } from "./util/math.js";
import G from "./models/request-result.js";
import {
logger as E,
IndexedDBAdapter as it,
Guid as L,
EventTypes as m,
} 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";
import { removeAllVisibleInAppMessages as oi } from "./InAppMessage/ui/remove-all-visible-in-app-messages.js";
export default class Wt {
constructor(t, i, s, e, h, n, o, r, l, a) {
(this.Pn = t),
(this.baseUrl = i),
(this.C = s),
(this.Dn = e),
(this.Cs = h),
(this.h = n),
(this.j = o),
(this.fc = r),
(this.La = l),
(this.B = a),
(this.Pn = t),
(this.baseUrl = i),
(this.vc = 0),
(this.bE = o.dE() || 0),
(this.bc = null),
(this.C = s),
(this.Dn = e),
(this.Cs = h),
(this.h = n),
(this.j = o),
(this.La = l),
(this.B = a),
(this.fc = r),
(this.wc = new u()),
(this.kc = null),
(this.yc = 50),
(this.jc = !1),
(this.Sc = !1);
}
Ac(t, i) {
return !t && !i && this.La.Jh() >= this.yc;
}
Dc(t) {
let i = this.C.Ul();
if (t.length > 0) {
const s = this.Cs.getUserId();
for (const e of t) {
const t = (!e.userId && !s) || e.userId === s;
e.type === m.sm && t && (i = !0);
}
}
return i;
}
$c(t = !1, i = !0, e, n, o, r = !1, u = !1) {
i && this.qc();
const c = this.j.RE(),
d = this.j.IE();
let m = !1;
const f = (t, r, u = -1) => {
const c = new Date().valueOf();
h.K(this.j, h.H.Yi, c),
-1 !== u && r.push(["X-Braze-Req-Tokens-Remaining", u.toString()]);
let d = !1;
a.O({
url: this.baseUrl + "/data/",
data: t,
headers: r,
W: (i) => {
null != t.respond_with &&
t.respond_with.triggers &&
(this.vc = Math.max(this.vc - 1, 0)),
this.B.Y(t, i, r)
? (this.La.Z(),
this.h.Nl(i),
(null != t.respond_with &&
t.respond_with.user_id != this.Cs.getUserId()) ||
(null != t.device && this.j.ft(s.ct.ac, t.device),
null != t.sdk_metadata &&
(this.j.ft(s.ct.ku, t.sdk_metadata),
this.j.ft(s.ct.wu, this.C.bt())),
this.fc(i),
h.Wu(this.j, h.H.Yi, 1),
"function" == typeof e && e()))
: i.auth_error && (d = !0);
},
error: () => {
(d = !0),
null != t.respond_with &&
t.respond_with.triggers &&
(this.vc = Math.max(this.vc - 1, 0)),
this.B.Bu(t.events, t.attributes),
"function" == typeof n && n();
},
tt: (t, s) => {
"function" == typeof o && o(!d);
const e = this.B.il(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.sl(this.j, h.H.Yi);
let t = this.bc;
(null == t || t < 1e3 * this.bE) && (t = 1e3 * this.bE);
const i = Math.min(3e5, l(1e3 * this.bE, 3 * t)) + n;
this.Cc(i);
} else this.Cc(Math.max(1e3 * this.bE, n));
m = !0;
}
},
});
},
g = this.Dc(c),
p = t || g;
if (this.Ac(r, g))
return void E.info(
"Declining to flush data due to 50 consecutive authentication failures",
);
if (i && !this.B.Fu(c, d, p))
return this.Cc(), void ("function" == typeof o && o(!0));
const v = this.B.Ku(p, c, d, u);
p && this.vc++;
let b = !1;
if (v)
for (const t of v)
this.B.J(
t.requestData,
(i) => f(t.requestData, t.headers, i),
h.H.Yi,
n,
),
(b = !0);
this.La.wh() && i && !b
? this.Cc()
: g &&
(E.info("Invoking new session subscriptions"),
this.wc.L(),
(this.Sc = !0));
}
Mc() {
return this.vc > 0;
}
Cc(t = 1e3 * this.bE) {
this.jc ||
(this.qc(),
(this.kc = window.setTimeout(() => {
if (document.hidden) {
const t = "visibilitychange",
i = () => {
document.hidden ||
(document.removeEventListener(t, i, !1), this.$c());
};
document.addEventListener(t, i, !1);
} else this.$c();
}, t)),
(this.bc = t));
}
qc() {
null != this.kc && (clearTimeout(this.kc), (this.kc = null));
}
initialize() {
(this.jc = !1), this.Cc();
}
destroy() {
this.wc.removeAllSubscriptions(),
this.La.Gh(),
this.qc(),
(this.jc = !0),
this.$c(void 0, !1, void 0, void 0, void 0, void 0, !0),
(this.kc = null),
(this.Sc = !1);
}
rn(t) {
return this.Sc ? (t(), null) : this.wc.wt(t);
}
openSession() {
const t = this.C.bt() !== this.C.Fo();
t && (this.j.hE(s.iu.Oa), this.j.hE(s.iu.su)),
this.$c(!1, void 0, () => {
t && (this.j.Nt(s.ct.Ne), this.j.Nt(s.ct.It));
}),
this.Au(),
t &&
import("./Push/push-manager-factory.js").then((t) => {
if (this.jc) return;
const i = t.default.ea();
if (
null != i &&
(vt.isPushPermissionGranted() || vt.isPushBlocked())
) {
const t = () => {
i.Rn()
? E.info(
"Push token maintenance is disabled, not refreshing token for backend.",
)
: i.subscribe();
},
e = (i, s) => {
s && t();
},
h = () => {
const i = this.j.lt(s.ct.Wo);
(null == i || i) && t();
},
n = it.Us.Rs;
new it(n, E).kr(n.Fs.cu, e, h);
}
});
}
Tc() {
this.j.Nt(s.ct.Se), this.j.Nt(s.ct.Ps), this.j.Nt(s.ct.yn);
}
Lc() {
var t, i;
this.j.Nt(s.ct.tl), this.j.Nt(s.ct.SE);
const e = h.H;
for (const i in e) {
const s = e[i];
this.B.al(s, null === (t = this.h) || void 0 === t ? void 0 : t.bl(s));
}
this.B.rl(null === (i = this.h) || void 0 === i ? void 0 : i.Rl());
}
changeUser(t, i, e) {
const h = this.Cs.getUserId();
if (h !== t) {
this.C.tm(),
this.Tc(),
oi(),
null != h && this.$c(void 0, !1, void 0, void 0, void 0),
this.Cs.ou(t),
e ? this.La.setSdkAuthenticationSignature(e) : this.La.yh();
for (let t = 0; t < i.length; t++) i[t].changeUser(null == h);
this.B.fo(),
null != h && this.j.Nt(s.ct.rE),
this.j.Nt(s.ct.ac),
this.j.Nt(s.ct.uE),
this.Lc(),
this.openSession(),
E.info('Changed user to "' + t + '".');
} else {
let i = "Doing nothing.";
e &&
this.La.jh() !== e &&
(this.La.setSdkAuthenticationSignature(e),
(i = "Updated SDK authentication signature")),
E.info(`Current user is already ${t}. ${i}`);
}
}
requestImmediateDataFlush(t) {
this.qc(), this.C.Fo();
this.$c(
void 0,
void 0,
void 0,
() => {
E.error("Failed to flush data, request will be retried automatically.");
},
t,
!0,
);
}
yr(t, i) {
this.C.Fo(),
E.info("Requesting explicit trigger refresh."),
this.$c(!0, void 0, t, i);
}
du(t, i) {
const e = m.Rc,
h = { a: t, l: i },
n = c.rt(e, h);
return (
n && (E.info(`Logged alias ${t} with label ${i}`), this.j.ft(s.ct.uE, h)),
n
);
}
bu(t, i, s) {
if (this.h.hu(i))
return (
E.info(`Custom Attribute "${i}" is blocklisted, ignoring.`), new G()
);
const e = { key: i, value: s },
h = c.rt(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 = c.rt(m.Fc, o, t || void 0);
return (
r &&
E.info(`Set user last known location as ${JSON.stringify(o, null, 2)}`),
r
);
}
$r(t, i) {
const s = this.C.Fo();
return new ue(this.Cs.getUserId(), m.Ic, t, s, { cid: i });
}
Pc(t, i) {
return new it(t, i);
}
Au() {
const t = it.Us.Rs;
this.Pc(t, E).setItem(t.Fs.Yi, 1, {
baseUrl: this.baseUrl,
data: { api_key: this.Pn, device_id: this.Dn.fe().id },
userId: this.Cs.getUserId(),
sdkAuthEnabled: this.La.wh(),
});
}
Fr(t) {
for (const i of t)
if (i.api_key === this.Pn) this.B.Bu(i.events, i.attributes);
else {
const t = it.Us.Rs;
new it(t, E).setItem(t.Fs.zr, L.oe(), i);
}
}
Iu(t, i, s) {
if (this.h.hu(t))
return (
E.info(`Custom Attribute "${t}" is blocklisted, ignoring.`), new G()
);
let e, h;
return (
null === i && null === s
? ((e = m.xc), (h = { key: t }))
: ((e = m.Nc), (h = { key: t, latitude: i, longitude: s })),
c.rt(e, h)
);
}
Cu(t, i) {
const s = { group_id: t, status: i };
return c.rt(m.Oc, s);
}
}