@yext/analytics
Version:
An analytics library for Yext
97 lines • 5.02 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnalyticsEventReporter = void 0;
const setupSessionId_1 = require("./setupSessionId");
const package_json_1 = __importDefault(require("../package.json"));
const post_1 = require("./post");
const merge_1 = __importDefault(require("./merge"));
const setupRequestUrl_1 = require("./setupRequestUrl");
/** Represents an reporter is responsible for reporting analytics events. */
class AnalyticsEventReporter {
/**
* @param config - necessary analytics config: Must provide one and only
* one of API Key or Bearer Token.
*
* @param payload - (optional) desired event values to report
*/
constructor(config, payload) {
if (config.authorizationType !== 'apiKey' &&
config.authorizationType !== 'bearer') {
throw new Error('Authorization type must be either apiKey or bearer.');
}
if (!config.authorization) {
throw new Error('Authorization must be provided.');
}
this.config = config;
this.payload = payload;
}
with(payload) {
const currentPayload = this.payload === undefined ? payload : (0, merge_1.default)(this.payload, payload);
return new AnalyticsEventReporter(this.config, currentPayload);
}
report(newPayload) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const finalPayload = (0, merge_1.default)((_a = this.payload) !== null && _a !== void 0 ? _a : {}, newPayload !== null && newPayload !== void 0 ? newPayload : {});
/** If session tracking is disabled, set sessionId to undefined. If it is,
* enabled, and sessionId was not already manually added to the event payload,
* call getOrSetupSessionId to set value.
*/
if (!this.config.sessionTrackingEnabled) {
finalPayload.sessionId = undefined;
}
else if (!finalPayload.sessionId) {
finalPayload.sessionId = (0, setupSessionId_1.getOrSetupSessionId)();
}
finalPayload.clientSdk
? (finalPayload.clientSdk['ANALYTICS'] =
package_json_1.default.version)
: (finalPayload.clientSdk = { ['ANALYTICS']: package_json_1.default.version });
finalPayload.authorization =
this.config.authorizationType === 'apiKey'
? 'KEY ' + this.config.authorization
: 'Bearer ' + this.config.authorization;
const shouldUseBeacon = (0, post_1.useBeacon)(finalPayload, this.config.forceFetch);
const requestUrl = (0, setupRequestUrl_1.setupRequestUrl)(this.config.env, this.config.region);
// If useBeacon returns true, return boolean response of postWithBeacon as string.
if (shouldUseBeacon) {
return new Promise((resolve, reject) => {
if ((0, post_1.postWithBeacon)(requestUrl, finalPayload, this.config)) {
resolve('');
}
else {
reject('Failed Beacon Call');
}
});
}
/** If pageUrl is undefined, default to document.URL if it exists */
if (finalPayload.pageUrl === undefined && document.URL !== '') {
finalPayload.pageUrl = document.URL;
}
/** If referrerUrl is undefined, default to document.referrer if it exists */
if (finalPayload.referrerUrl === undefined && document.referrer !== '') {
finalPayload.referrerUrl = document.referrer;
}
/** If useBeacon returns false, use postWithFetch.
If result is successful, return result json.
If request fails, return errors. */
return (0, post_1.postWithFetch)(requestUrl, finalPayload, this.config)
.then((response) => response)
.catch((e) => e);
});
}
}
exports.AnalyticsEventReporter = AnalyticsEventReporter;
//# sourceMappingURL=AnalyticsEventReporter.js.map