UNPKG

avo-inspector

Version:

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

221 lines (220 loc) 9.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AvoInspector = void 0; var AvoInspectorEnv_1 = require("./AvoInspectorEnv"); var AvoSchemaParser_1 = require("./AvoSchemaParser"); var AvoSessionTracker_1 = require("./AvoSessionTracker"); var AvoBatcher_1 = require("./AvoBatcher"); var AvoNetworkCallsHandler_1 = require("./AvoNetworkCallsHandler"); var AvoStorage_1 = require("./AvoStorage"); var AvoDeduplicator_1 = require("./AvoDeduplicator"); var utils_1 = require("./utils"); var libVersion = require("../package.json").version; var AvoInspector = /** @class */ (function () { // constructor(apiKey: string, env: AvoInspectorEnv, version: string) { function AvoInspector(options) { var _this = this; // the constructor does aggressive null/undefined checking because same code paths will be accessible from JS if ((0, utils_1.isValueEmpty)(options.env)) { this.environment = AvoInspectorEnv_1.AvoInspectorEnv.Dev; console.warn("[Avo Inspector] No environment provided. Defaulting to dev."); } else if (Object.values(AvoInspectorEnv_1.AvoInspectorEnv).indexOf(options.env) === -1) { this.environment = AvoInspectorEnv_1.AvoInspectorEnv.Dev; console.warn("[Avo Inspector] Unsupported environment provided. Defaulting to dev. Supported environments - Dev, Staging, Prod."); } else { this.environment = options.env; } if ((0, utils_1.isValueEmpty)(options.apiKey)) { throw new Error("[Avo Inspector] No API key provided. Inspector can't operate without API key."); } else { this.apiKey = options.apiKey; } if ((0, utils_1.isValueEmpty)(options.version)) { throw new Error("[Avo Inspector] No version provided. Many features of Inspector rely on versioning. Please provide comparable string version, i.e. integer or semantic."); } else { this.version = options.version; } if (this.environment === AvoInspectorEnv_1.AvoInspectorEnv.Dev) { AvoInspector._batchSize = 1; AvoInspector._shouldLog = true; } else { AvoInspector._batchSize = 30; AvoInspector._batchFlushSeconds = 30; AvoInspector._shouldLog = false; } AvoInspector.avoStorage = new AvoStorage_1.AvoStorage(AvoInspector._shouldLog); var avoNetworkCallsHandler = new AvoNetworkCallsHandler_1.AvoNetworkCallsHandler(this.apiKey, this.environment.toString(), options.appName || "", this.version, libVersion); this.avoBatcher = new AvoBatcher_1.AvoBatcher(avoNetworkCallsHandler); this.sessionTracker = new AvoSessionTracker_1.AvoSessionTracker(this.avoBatcher); this.avoDeduplicator = new AvoDeduplicator_1.AvoDeduplicator(); try { if (process.env.BROWSER) { // XXX make node/browser split clearer if (typeof window !== "undefined") { window.addEventListener("load", function () { _this.sessionTracker.startOrProlongSession(Date.now()); }, false); } } else { this.sessionTracker.startOrProlongSession(Date.now()); } } catch (e) { console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e); } } Object.defineProperty(AvoInspector, "batchSize", { get: function () { return this._batchSize; }, set: function (newSize) { if (newSize < 1) { this._batchSize = 1; } else { this._batchSize = newSize; } }, enumerable: false, configurable: true }); Object.defineProperty(AvoInspector, "batchFlushSeconds", { get: function () { return this._batchFlushSeconds; }, enumerable: false, configurable: true }); Object.defineProperty(AvoInspector, "shouldLog", { get: function () { return this._shouldLog; }, set: function (enable) { this._shouldLog = enable; }, enumerable: false, configurable: true }); AvoInspector.prototype.trackSchemaFromEvent = function (eventName, eventProperties) { try { if (this.avoDeduplicator.shouldRegisterEvent(eventName, eventProperties, false)) { if (AvoInspector.shouldLog) { console.log("Avo Inspector: supplied event " + eventName + " with params " + JSON.stringify(eventProperties)); } var eventSchema = this.extractSchema(eventProperties, false); this.trackSchemaInternal(eventName, eventSchema, null, null); return eventSchema; } else { if (AvoInspector.shouldLog) { console.log("Avo Inspector: Deduplicated event: " + eventName); } return []; } } catch (e) { console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e); return []; } }; AvoInspector.prototype._avoFunctionTrackSchemaFromEvent = function (eventName, eventProperties, eventId, eventHash) { try { if (this.avoDeduplicator.shouldRegisterEvent(eventName, eventProperties, true)) { if (AvoInspector.shouldLog) { console.log("Avo Inspector: supplied event " + eventName + " with params " + JSON.stringify(eventProperties)); } var eventSchema = this.extractSchema(eventProperties, false); this.trackSchemaInternal(eventName, eventSchema, eventId, eventHash); return eventSchema; } else { if (AvoInspector.shouldLog) { console.log("Avo Inspector: Deduplicated event: " + eventName); } return []; } } catch (e) { console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e); return []; } }; AvoInspector.prototype.trackSchema = function (eventName, eventSchema) { try { if (this.avoDeduplicator.shouldRegisterSchemaFromManually(eventName, eventSchema)) { if (AvoInspector.shouldLog) { console.log("Avo Inspector: supplied event " + eventName + " with schema " + JSON.stringify(eventSchema)); } this.trackSchemaInternal(eventName, eventSchema, null, null); } else { if (AvoInspector.shouldLog) { console.log("Avo Inspector: Deduplicated event: " + eventName); } } } catch (e) { console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e); } }; AvoInspector.prototype.trackSchemaInternal = function (eventName, eventSchema, eventId, eventHash) { try { this.sessionTracker.startOrProlongSession(Date.now()); this.avoBatcher.handleTrackSchema(eventName, eventSchema, eventId, eventHash); } catch (e) { console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e); } }; AvoInspector.prototype.enableLogging = function (enable) { AvoInspector._shouldLog = enable; }; AvoInspector.prototype.extractSchema = function (eventProperties, shouldLogIfEnabled) { if (shouldLogIfEnabled === void 0) { shouldLogIfEnabled = true; } try { this.sessionTracker.startOrProlongSession(Date.now()); if (this.avoDeduplicator.hasSeenEventParams(eventProperties, true)) { if (shouldLogIfEnabled && AvoInspector.shouldLog) { console.warn("Avo Inspector: WARNING! You are trying to extract schema shape that was just reported by your Avo functions. " + "This is an indicator of duplicate inspector reporting. " + "Please reach out to support@avo.app for advice if you are not sure how to handle this."); } } if (AvoInspector.shouldLog) { console.log("Avo Inspector: extracting schema from " + JSON.stringify(eventProperties)); } return AvoSchemaParser_1.AvoSchemaParser.extractSchema(eventProperties); } catch (e) { console.error("Avo Inspector: something went wrong. Please report to support@avo.app.", e); return []; } }; AvoInspector.prototype.setBatchSize = function (newBatchSize) { AvoInspector._batchSize = newBatchSize; }; AvoInspector.prototype.setBatchFlushSeconds = function (newBatchFlushSeconds) { AvoInspector._batchFlushSeconds = newBatchFlushSeconds; }; AvoInspector._batchSize = 30; AvoInspector._batchFlushSeconds = 30; AvoInspector._shouldLog = false; return AvoInspector; }()); exports.AvoInspector = AvoInspector;