UNPKG

ng2-appinsights

Version:
545 lines (525 loc) 23.7 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(require("@angular/core")); else if(typeof define === 'function' && define.amd) define(["@angular/core"], factory); else if(typeof exports === 'object') exports["ng2-appinsights"] = factory(require("@angular/core")); else root["ng2-appinsights"] = factory(root["@angular/core"]); })(this, function(__WEBPACK_EXTERNAL_MODULE_2__) { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ function(module, exports, __webpack_require__) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var errorhandler_1 = __webpack_require__(1); exports.AppInsightsErrorHandler = errorhandler_1.AppInsightsErrorHandler; var appinsights_module_1 = __webpack_require__(6); exports.AppInsightsModule = appinsights_module_1.AppInsightsModule; var appinsights_service_1 = __webpack_require__(4); exports.AppInsightsService = appinsights_service_1.AppInsightsService; var severitylevel_1 = __webpack_require__(3); exports.SeverityLevel = severitylevel_1.SeverityLevel; /***/ }, /* 1 */ /***/ function(module, exports, __webpack_require__) { "use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); 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 = __webpack_require__(2); var severitylevel_1 = __webpack_require__(3); var appinsights_service_1 = __webpack_require__(4); /** * App Insights Error Handler */ var AppInsightsErrorHandler = (function (_super) { __extends(AppInsightsErrorHandler, _super); function AppInsightsErrorHandler(appInsightsService) { var _this = _super.call(this, true) || this; _this.appInsightsService = appInsightsService; return _this; } AppInsightsErrorHandler.prototype.handleError = function (error) { this.appInsightsService.trackException(error, 'unhandled', null, null, severitylevel_1.SeverityLevel.Error); // delegate to the default handler _super.prototype.handleError.call(this, error); }; return AppInsightsErrorHandler; }(core_1.ErrorHandler)); AppInsightsErrorHandler = __decorate([ core_1.Injectable(), __metadata("design:paramtypes", [appinsights_service_1.AppInsightsService]) ], AppInsightsErrorHandler); exports.AppInsightsErrorHandler = AppInsightsErrorHandler; /***/ }, /* 2 */ /***/ function(module, exports) { module.exports = __WEBPACK_EXTERNAL_MODULE_2__; /***/ }, /* 3 */ /***/ function(module, exports) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * Severity Level Constants * * @export * @enum {number} */ var SeverityLevel; (function (SeverityLevel) { SeverityLevel[SeverityLevel["Verbose"] = 0] = "Verbose"; SeverityLevel[SeverityLevel["Information"] = 1] = "Information"; SeverityLevel[SeverityLevel["Warning"] = 2] = "Warning"; SeverityLevel[SeverityLevel["Error"] = 3] = "Error"; SeverityLevel[SeverityLevel["Critical"] = 4] = "Critical"; })(SeverityLevel = exports.SeverityLevel || (exports.SeverityLevel = {})); /***/ }, /* 4 */ /***/ function(module, exports, __webpack_require__) { "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); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = __webpack_require__(2); var appinsightslibrary_service_1 = __webpack_require__(5); /** * App Insights Service */ var AppInsightsService = (function () { function AppInsightsService(appInsightsLibraryService) { this.appInsights = appInsightsLibraryService.getAppInsightsInstance(); } /** * Initialize app insights * * @param {Config} config * * @memberOf AppInsightsService */ AppInsightsService.prototype.Init = function (config) { this.appInsights.downloadAndSetup(config); }; /** * Track Page View * * @param {string} [name] - The name used to identify the page in the portal. * Defaults to the document title. * @param {string} [url] - A relative or absolute URL that identifies the * page or similar item. Defaults to the window location. * @param {*} [properties] - Map of string to string: Additional data * used to filter pages in the portal. Defaults to empty. * @param {*} [measurements] - Map of string to number: Metrics associated * with this page, displayed in Metrics Explorer on the portal. * Defaults to empty. * @param {number} [duration] - The number of milliseconds it took to load * this page, displayed in Metrics Explorer on the portal. Defaults to empty. * If empty, end of page view duration is recorded when browser page load event is called. * * @memberOf AppInsightsService */ AppInsightsService.prototype.trackPageView = function (name, url, properties, measurements, duration) { this.appInsights.trackPageView(name, url, properties, measurements, duration); }; /** * Track Event * * @param {string} name - Identifies the event. Events with the same name are * counted and can be charted in Metric Explorer. * @param {*} [properties] - Map of string to string: Additional data used to * filter events in the portal. Defaults to empty. * @param {*} [measurements] - Map of string to number: Metrics associated with * this page, displayed in Metrics Explorer on the portal. Defaults to empty. * * @memberOf AppInsightsService */ AppInsightsService.prototype.trackEvent = function (name, properties, measurements) { this.appInsights.trackEvent(name, properties, measurements); }; ; /** * Track Metric * * @param {string} name - A string that identifies the metric. In the portal, * you can select metrics for display by name. * @param {number} average - Either a single measurement, or the average of * several measurements. Should be >=0 to be correctly displayed. * @param {number} [sampleCount] - Count of measurements represented by the * average. Defaults to 1. Should be >=1. * @param {number} [min] - The smallest measurement in the sample. Defaults * to the average. Should be >= 0. * @param {number} [max] - The largest measurement in the sample. Defaults to * the average. Should be >= 0. * @param {*} [properties] - Map of string to string: Additional data used to * filter events in the portal. * * @memberOf AppInsightsService */ AppInsightsService.prototype.trackMetric = function (name, average, sampleCount, min, max, properties) { this.appInsights.trackMetric(name, average, sampleCount, min, max, properties); }; /** * Track Exception * * @param {Error} exception - An Error from a catch clause. * @param {string} [handledAt] - Defaults to "unhandled". * @param {*} [properties] - Map of string to string: Additional data used to * filter exceptions in the portal. Defaults to empty. * @param {*} [measurements] - Map of string to number: Metrics associated * with this page, displayed in Metrics Explorer on the portal. Defaults to empty. * @param {SeverityLevel} [severityLevel] - SeverityLevel * * @memberOf AppInsightsService */ AppInsightsService.prototype.trackException = function (exception, handledAt, properties, measurements, severityLevel) { this.appInsights.trackException(exception, handledAt, properties, measurements, severityLevel); }; /** * Track Trace * * @param {string} message - Diagnostic data. Can be much longer than a name. * @param {*} [properties] - Map of string to string: Additional data used to * filter exceptions in the portal. Defaults to empty. * @param {*} [measurements] - Map of string to number: Metrics associated with this page, * displayed in Metrics Explorer on the portal. Defaults to empty. * * @memberOf AppInsightsService */ AppInsightsService.prototype.trackTrace = function (message, properties, measurements) { this.appInsights.trackTrace(message, properties, measurements); }; /** * Track dependency * * @param {string} id - Unique id, this is used by the backend o correlate server requests. * @param {string} method - Represents request verb (GET, POST, etc.) * @param {string} absoluteUrl - Absolute url used to make the dependency request * @param {string} pathName - Path part of the absolute url * @param {number} totalTime - Total request time * @param {boolean} success - Indicates if the request was sessessful * @param {number} resultCode - Response code returned by the dependency request * * @memberOf AppInsightsService */ AppInsightsService.prototype.trackDependency = function (id, method, absoluteUrl, pathName, totalTime, success, resultCode) { this.appInsights.trackDependency(id, method, absoluteUrl, pathName, totalTime, success, resultCode); }; /** * Flush * * @description * Immediately send all queued telemetry. Synchronous. * * @memberOf AppInsightsService */ AppInsightsService.prototype.flush = function () { this.appInsights.flush(); }; /** * Set authenticated user context * * @description * Set the authenticated user id and the account id in this session. * Use this when you have identified a specific signed-in user. * Parameters must not contain spaces or ,;=| * * @param {string} authenticatedUserId - An id that uniquely identifies a user of your app. * No spaces, comma, semicolon, equals or vertical bar. * @param {string} [accountId] - An optional account id, if your app groups users into accounts. * No spaces, comma, semicolon, equals or vertical bar. * * @memberOf AppInsightsService */ AppInsightsService.prototype.setAuthenticatedUserContext = function (authenticatedUserId, accountId) { this.appInsights.setAuthenticatedUserContext(authenticatedUserId, accountId); }; /** * Clear authenticated user context * * @description * Clears the authenticated user id and the account id from the user context, * and clears the associated cookie. * * @memberOf AppInsightsService */ AppInsightsService.prototype.clearAuthenticatedUserContext = function () { this.appInsights.clearAuthenticatedUserContext(); }; /** * Start Track Page * * @description * Starts the timer for tracking a page view. Use this instead of trackPageView * if you want to control when the page view timer starts and stops, but don't * want to calculate the duration yourself. This method doesn't send any telemetry. * Call stopTrackPage to log the end of the page view and send the event. * * @param {string} [name] - The name used to identify the page in the portal. * Defaults to the document title. * * @memberOf AppInsightsService */ AppInsightsService.prototype.startTrackPage = function (name) { this.appInsights.startTrackPage(name); }; /** * Stop Track Page * * @description * Stops the timer that was started by calling startTrackPage and sends the page view * telemetry with the specified properties and measurements. The duration of the page * view will be the time between calling startTrackPage and stopTrackPage. * * @param {string} [name] - The name used to identify the page in the portal. * Defaults to the document title. * @param {string} [url] - A relative or absolute URL that identifies the page or similar item. * Defaults to the window location. * @param {Object} [properties] - Map of string to string: Additional data used * to filter pages in the portal. Defaults to empty. * @param {Object} [measurements] - Map of string to number: Metrics associated with this page, * displayed in Metrics Explorer on the portal. Defaults to empty. * * @memberOf AppInsightsService */ AppInsightsService.prototype.stopTrackPage = function (name, url, properties, measurements) { this.appInsights.stopTrackPage(name, url, properties, measurements); }; return AppInsightsService; }()); AppInsightsService = __decorate([ core_1.Injectable(), __param(0, core_1.Inject(appinsightslibrary_service_1.AppInsightsLibraryService)), __metadata("design:paramtypes", [appinsightslibrary_service_1.AppInsightsLibraryService]) ], AppInsightsService); exports.AppInsightsService = AppInsightsService; /***/ }, /* 5 */ /***/ function(module, exports, __webpack_require__) { "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 = __webpack_require__(2); /** * 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; /***/ }, /* 6 */ /***/ function(module, exports, __webpack_require__) { "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; }; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = __webpack_require__(2); var appinsightslibrary_service_1 = __webpack_require__(5); var appinsights_service_1 = __webpack_require__(4); var errorhandler_1 = __webpack_require__(1); /** * App Insights Module * * @export * @class AppInsightsModule */ var AppInsightsModule = (function () { function AppInsightsModule() { } return AppInsightsModule; }()); AppInsightsModule = __decorate([ core_1.NgModule({ providers: [ appinsightslibrary_service_1.AppInsightsLibraryService, appinsights_service_1.AppInsightsService, { provide: core_1.ErrorHandler, useClass: errorhandler_1.AppInsightsErrorHandler, } ] }) ], AppInsightsModule); exports.AppInsightsModule = AppInsightsModule; /***/ } /******/ ]) }); ; //# sourceMappingURL=ng2-appinsights.js.map