@goplayerzero/sdk-web
Version:
The official PlayerZero Web SDK
118 lines • 5.37 kB
JavaScript
export class PlayerZeroSdk {
get playerzero() {
var _a;
if (Boolean((_a = window === null || window === void 0 ? void 0 : window.playerzero) === null || _a === void 0 ? void 0 : _a.kill))
return Promise.resolve(window.playerzero);
else
return new Promise((resolve, reject) => {
window.addEventListener('playerzero_ready', () => {
var _a;
if (Boolean((_a = window === null || window === void 0 ? void 0 : window.playerzero) === null || _a === void 0 ? void 0 : _a.kill))
resolve(window.playerzero);
else
reject();
}, { once: true });
});
}
init(projectToken, configOrEndpoint) {
var _a;
if (this.isInitialized()) {
console.warn('PlayerZero has already been initialized. PlayerZero.init() can only be called once.');
return;
}
this.injectFetchWrappers();
const head = document.getElementsByTagName("head").item(0);
if (!head) {
setTimeout(() => this.init(projectToken, configOrEndpoint), 100);
return;
}
let endpoint;
let privacyFnUrl;
if (typeof configOrEndpoint === 'string') {
endpoint = configOrEndpoint;
}
else if (typeof configOrEndpoint === 'object' && configOrEndpoint !== null) {
endpoint = (_a = configOrEndpoint.endpoint) !== null && _a !== void 0 ? _a : 'https://go.playerzero.app';
privacyFnUrl = configOrEndpoint.privacyFnUrl;
}
else {
endpoint = 'https://go.playerzero.app';
}
const script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', `${endpoint}/record/${projectToken}`);
script.setAttribute('crossorigin', 'anonymous');
if (privacyFnUrl !== undefined) {
script.setAttribute('data-pz-privacy-src', privacyFnUrl);
}
head.appendChild(script);
}
identify(userId, metadata) {
this.playerzero.then((sdk) => sdk.identify(userId, metadata));
}
setUserVars(metadata) {
this.playerzero.then((sdk) => sdk.setUserVars(metadata));
}
track(event, metadata) {
this.playerzero.then((sdk) => sdk.track(event, metadata));
}
prompt() {
this.playerzero.then((sdk) => sdk.prompt());
}
devtools() {
return this.playerzero.then((sdk) => sdk.devtools());
}
kill() {
return this.playerzero.then((sdk) => sdk.kill());
}
isInitialized() {
var _a;
return Boolean((_a = window.playerzero) === null || _a === void 0 ? void 0 : _a.kill);
}
injectFetchWrappers() {
const originalFetch = window.fetch;
if (!window["playerzero"]) {
// @ts-ignore
window["playerzero"] = {};
}
window.playerzero.monkey_patch_ts = performance.now();
window.fetch = function (input, init) {
var _a, _b;
if (Boolean((_a = window.playerzero) === null || _a === void 0 ? void 0 : _a.before_fetch_apply) && Boolean((_b = window.playerzero) === null || _b === void 0 ? void 0 : _b.after_fetch_apply)) {
const urlCopy = (input instanceof Request) ? input.url : input.toString();
const passedOpts = init || {};
const origOptsBody = passedOpts.body;
if (input instanceof Request) {
// According to specification, init takes precedence over input
if (passedOpts.method === undefined)
passedOpts.method = input.method;
if (passedOpts.headers === undefined)
passedOpts.headers = input.headers;
}
let body = origOptsBody !== null && origOptsBody !== void 0 ? origOptsBody : ((input instanceof Request) ? input.body : undefined);
if (!(typeof body === 'string' || body instanceof FormData || body instanceof URLSearchParams) && body !== undefined && body !== null) {
body = '';
}
passedOpts.body = body;
const beforeFetchApplyResult = window.playerzero.before_fetch_apply(urlCopy, passedOpts);
passedOpts.body = origOptsBody;
// graphql aborts all of their responses; the promise wrapping allows us to get the response text before fetch is aborted
return new Promise((resolve, reject) => {
// eslint-disable-next-line prefer-rest-params
originalFetch(input, passedOpts)
.then((response) => {
window.playerzero.after_fetch_apply(response, beforeFetchApplyResult)
// eslint-disable-next-line @typescript-eslint/no-empty-function
.catch(() => {
})
.finally(() => resolve(response));
})
.catch(e => reject(e));
});
}
else
return originalFetch(input, init);
};
}
}
//# sourceMappingURL=playerzero.js.map