UNPKG

analytics-event-tracking

Version:

Analytics Event Tracking, a private Node.js module in TypeScript

89 lines (88 loc) 4.04 kB
"use strict"; 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.MoEngageClient = void 0; const axios_1 = __importDefault(require("axios")); class MoEngageClient { constructor(appId, dataCenterNumber, authToken) { this.isInitialized = false; this.appId = appId; this.dataCenterNumber = dataCenterNumber; this.authHeader = `Basic ${authToken}`; this.isInitialized = true; console.warn("MoEngage Web Service has been initialized."); } trackEvent(customerId_1, eventName_1, properties_1) { return __awaiter(this, arguments, void 0, function* (customerId, eventName, properties, options = {}) { if (!this.isInitialized) { throw new Error("MoEngage is not initialized."); } const url = `https://api-0${this.dataCenterNumber}.moengage.com/v1/event/${this.appId}?app_id=${this.appId}`; const payload = { type: "event", customer_id: customerId, actions: [ { action: eventName, attributes: properties, platform: options.platform || "Web", app_version: options.appVersion || "1.0.0", current_time: options.currentTime || new Date().toISOString(), user_timezone_offset: options.userTimezoneOffset || 19800, // Chennai, Kolkata, Mumbai, New Delhi }, ], }; try { const response = yield axios_1.default.post(url, payload, { headers: { "Content-Type": "application/json", Authorization: this.authHeader, }, }); console.log("Event tracked successfully:", response.data); } catch (error) { console.error("Failed to track event:", error); throw new Error("Error while tracking event."); } }); } createUser(customerId, attributes) { return __awaiter(this, void 0, void 0, function* () { if (!this.isInitialized) { throw new Error("MoEngage is not initialized."); } const url = `https://api-0${this.dataCenterNumber}.moengage.com/v1/customer/${this.appId}`; const payload = { type: "customer", customer_id: customerId, attributes: Object.assign({}, attributes), }; try { const response = yield axios_1.default.post(url, payload, { headers: { "Content-Type": "application/json", Authorization: this.authHeader, }, }); console.log("User created successfully:", response.data); } catch (error) { console.error("Failed to create user:", error); throw new Error("Error while creating user."); } }); } } exports.MoEngageClient = MoEngageClient;