analytics-event-tracking
Version:
Analytics Event Tracking, a private Node.js module in TypeScript
77 lines (76 loc) • 4.36 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AnalyticsClient = void 0;
const mixpanelClient_1 = require("./libs/mixpanelClient");
const moengageClient_1 = require("./libs/moengageClient");
class AnalyticsClient {
constructor(config) {
if (config.mixpanelToken) {
this.mixpanelClient = new mixpanelClient_1.MixpanelClient(config.mixpanelToken, config.mixpanelOptions);
}
if (config.moengageAppId && config.dataCenterNumber && config.authToken) {
if (!AnalyticsClient.moEngageInstance) {
AnalyticsClient.moEngageInstance = new moengageClient_1.MoEngageClient(config.moengageAppId, config.dataCenterNumber, config.authToken);
}
else {
console.warn("MoEngage Web Service has already been initialized.");
}
}
}
trackEvent(eventName_1) {
return __awaiter(this, arguments, void 0, function* (eventName, properties = {}, options, callback) {
var _a, _b, _c;
const trackInMixpanel = (_a = options === null || options === void 0 ? void 0 : options.mixpanel) !== null && _a !== void 0 ? _a : true;
const trackInMoEngage = (_b = options === null || options === void 0 ? void 0 : options.moengage) !== null && _b !== void 0 ? _b : true;
if (trackInMixpanel && this.mixpanelClient) {
this.mixpanelClient.track(eventName, properties, callback);
}
if (trackInMoEngage && AnalyticsClient.moEngageInstance) {
if (!((_c = options === null || options === void 0 ? void 0 : options.moengageOptions) === null || _c === void 0 ? void 0 : _c.customerId)) {
console.error("Customer ID is required to track events in MoEngage.");
throw new Error("Customer ID is missing for MoEngage tracking.");
}
try {
yield AnalyticsClient.moEngageInstance.trackEvent(options.moengageOptions.customerId, eventName, properties, {
platform: options.moengageOptions.platform,
appVersion: options.moengageOptions.appVersion,
currentTime: options.moengageOptions.currentTime,
userTimezoneOffset: options.moengageOptions.userTimezoneOffset,
});
}
catch (error) {
console.error("Failed to track event in MoEngage:", error);
}
}
});
}
createUser(attributes, options) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const trackInMoEngage = (_a = options === null || options === void 0 ? void 0 : options.moengage) !== null && _a !== void 0 ? _a : true;
if (trackInMoEngage && AnalyticsClient.moEngageInstance) {
if (!((_b = options === null || options === void 0 ? void 0 : options.moengageOptions) === null || _b === void 0 ? void 0 : _b.customerId)) {
console.error("Customer ID is required to create user in MoEngage.");
throw new Error("Customer ID is missing for MoEngage tracking.");
}
try {
yield AnalyticsClient.moEngageInstance.createUser(options.moengageOptions.customerId, attributes);
}
catch (error) {
console.error("Failed to create user in MoEngage:", error);
}
}
});
}
}
exports.AnalyticsClient = AnalyticsClient;
AnalyticsClient.moEngageInstance = null;