UNPKG

avo-inspector

Version:

[![npm version](https://badge.fury.io/js/avo-inspector.svg)](https://badge.fury.io/js/avo-inspector)

143 lines (142 loc) 6.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AvoNetworkCallsHandler = void 0; var AvoGuid_1 = require("./AvoGuid"); var AvoSessionTracker_1 = require("./AvoSessionTracker"); var AvoInspector_1 = require("./AvoInspector"); var AvoInstallationId_1 = require("./AvoInstallationId"); var AvoNetworkCallsHandler = /** @class */ (function () { function AvoNetworkCallsHandler(apiKey, envName, appName, appVersion, libVersion) { this.samplingRate = 1.0; this.sending = false; this.apiKey = apiKey; this.envName = envName; this.appName = appName; this.appVersion = appVersion; this.libVersion = libVersion; } AvoNetworkCallsHandler.prototype.callInspectorWithBatchBody = function (inEvents, onCompleted) { var _this = this; if (this.sending) { onCompleted("Batch sending cancelled because another batch sending is in progress. Your events will be sent with next batch."); return; } var events = inEvents.filter(function (x) { return x != null; }); this.fixSessionAndTrackingIds(events); if (events.length === 0) { return; } if (Math.random() > this.samplingRate) { if (AvoInspector_1.AvoInspector.shouldLog) { console.log("Avo Inspector: last event schema dropped due to sampling rate."); } return; } if (AvoInspector_1.AvoInspector.shouldLog) { console.log("Avo Inspector: events", events); events.forEach(function (event) { if (event.type === "sessionStarted") { console.log("Avo Inspector: sending session started event."); } else if (event.type === "event") { var schemaEvent = event; console.log("Avo Inspector: sending event " + schemaEvent.eventName + " with schema " + JSON.stringify(schemaEvent.eventProperties)); } }); } this.sending = true; var xmlhttp = new XMLHttpRequest(); xmlhttp.open("POST", AvoNetworkCallsHandler.trackingEndpoint, true); xmlhttp.setRequestHeader("Content-Type", "text/plain"); xmlhttp.send(JSON.stringify(events)); xmlhttp.onload = function () { if (xmlhttp.status != 200) { onCompleted("Error ".concat(xmlhttp.status, ": ").concat(xmlhttp.statusText)); } else { var samplingRate = JSON.parse(xmlhttp.response).samplingRate; if (samplingRate !== undefined) { _this.samplingRate = samplingRate; } onCompleted(null); } }; xmlhttp.onerror = function () { onCompleted("Request failed"); }; xmlhttp.ontimeout = function () { onCompleted("Request timed out"); }; this.sending = false; }; AvoNetworkCallsHandler.prototype.fixSessionAndTrackingIds = function (events) { var knownSessionId = null; var knownTrackingId = null; events.forEach(function (event) { if (event.sessionId !== null && event.sessionId !== undefined && event.sessionId !== "unknown") { knownSessionId = event.sessionId; } if (event.trackingId !== null && event.trackingId !== undefined && event.trackingId !== "unknown") { knownTrackingId = event.trackingId; } }); events.forEach(function (event) { if (event.sessionId === "unknown") { if (knownSessionId != null) { event.sessionId = knownSessionId; } else { event.sessionId = AvoSessionTracker_1.AvoSessionTracker.sessionId; } } if (event.trackingId === "unknown") { if (knownTrackingId != null) { event.trackingId = knownTrackingId; } else { event.trackingId = AvoInstallationId_1.AvoInstallationId.getInstallationId(); } } }); }; AvoNetworkCallsHandler.prototype.bodyForSessionStartedCall = function () { var sessionBody = this.createBaseCallBody(); sessionBody.type = "sessionStarted"; return sessionBody; }; AvoNetworkCallsHandler.prototype.bodyForEventSchemaCall = function (eventName, eventProperties, eventId, eventHash) { var eventSchemaBody = this.createBaseCallBody(); eventSchemaBody.type = "event"; eventSchemaBody.eventName = eventName; eventSchemaBody.eventProperties = eventProperties; if (eventId != null) { eventSchemaBody.avoFunction = true; eventSchemaBody.eventId = eventId; eventSchemaBody.eventHash = eventHash; } else { eventSchemaBody.avoFunction = false; eventSchemaBody.eventId = null; eventSchemaBody.eventHash = null; } return eventSchemaBody; }; AvoNetworkCallsHandler.prototype.createBaseCallBody = function () { return { apiKey: this.apiKey, appName: this.appName, appVersion: this.appVersion, libVersion: this.libVersion, env: this.envName, libPlatform: "web", messageId: AvoGuid_1.default.newGuid(), trackingId: AvoInstallationId_1.AvoInstallationId.getInstallationId(), createdAt: new Date().toISOString(), sessionId: AvoSessionTracker_1.AvoSessionTracker.sessionId, samplingRate: this.samplingRate, }; }; AvoNetworkCallsHandler.trackingEndpoint = "https://api.avo.app/inspector/v1/track"; return AvoNetworkCallsHandler; }()); exports.AvoNetworkCallsHandler = AvoNetworkCallsHandler;