UNPKG

ng2-appinsights

Version:
114 lines 5.14 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("@angular/core"); /** * App Insights Library Service * The app insights module from the package applicationinsights-js * rewritten to typescript */ var AppInsightsLibraryService = (function () { function AppInsightsLibraryService() { this._appInsightsInitialized = false; this._appInsightsName = 'appInsights'; this.window = window; } AppInsightsLibraryService.prototype._createLazyMethod = function (name) { var aiObject = this.window[this._appInsightsName]; // Define a temporary method that queues-up a the real method call aiObject[name] = function () { // Capture the original arguments passed to the method var originalArguments = arguments; // If the queue is available, it means that the function wasn't yet replaced with actual function value if (aiObject.queue) { aiObject.queue.push(function () { return aiObject[name].apply(aiObject, originalArguments); }); } else { // otherwise execute the function aiObject[name].apply(aiObject, originalArguments); } }; }; AppInsightsLibraryService.prototype._defineLazyMethods = function () { var aiObject = this.window[this._appInsightsName]; // capture initial cookie if possible try { aiObject.cookie = document.cookie; } catch (e) { } aiObject.queue = []; var method = [ 'clearAuthenticatedUserContext', 'flush', 'setAuthenticatedUserContext', 'startTrackEvent', 'startTrackPage', 'stopTrackEvent', 'stopTrackPage', 'trackDependency', 'trackEvent', 'trackException', 'trackMetric', 'trackPageView', 'trackTrace' ]; while (method.length) { this._createLazyMethod(method.pop()); } }; AppInsightsLibraryService.prototype._getDownloadAndSetup = function () { var self = this; return function (aiConfig) { var aiObject = self.window[self._appInsightsName]; aiObject.config = aiConfig; // if script was previously downloaded and initialized, queue will be deleted, reinitialize it if (!aiObject.queue) { aiObject.queue = []; } var scriptElement = document.createElement('script'); scriptElement.src = aiConfig.url || 'https://az416426.vo.msecnd.net/scripts/a/ai.0.js'; document.head.appendChild(scriptElement); // collect global errors if (!aiConfig.disableExceptionTracking) { self._createLazyMethod('_onerror'); var originalOnError_1 = self.window['_onerror']; self.window['_onerror'] = function (message, url, lineNumber, columnNumber, error) { var handled = originalOnError_1 && originalOnError_1(message, url, lineNumber, columnNumber, error); if (handled !== true) { aiObject['_onerror'](message, url, lineNumber, columnNumber, error); } return handled; }; } }; }; AppInsightsLibraryService.prototype.getAppInsightsInstance = function () { if (!this.window[this._appInsightsName]) { this.window[this._appInsightsName] = { downloadAndSetup: this._getDownloadAndSetup(), // exposing it for unit tests only, not part of interface _defineLazyMethods: this._defineLazyMethods }; this._defineLazyMethods(); } return this.window[this._appInsightsName]; }; return AppInsightsLibraryService; }()); AppInsightsLibraryService = __decorate([ core_1.Injectable(), __metadata("design:paramtypes", []) ], AppInsightsLibraryService); exports.AppInsightsLibraryService = AppInsightsLibraryService; //# sourceMappingURL=appinsightslibrary.service.js.map