UNPKG

@kurkov87/ngx-analytics

Version:

Vendor-agnostic web analytics for Angular2 applications, based on angulartics2

95 lines (92 loc) 4.38 kB
import { Injectable } from '@angular/core'; import { Title } from '@angular/platform-browser'; import { NavigationEnd, NavigationError, NavigationStart, Router } from '@angular/router'; import { filter } from 'rxjs/operators'; import { NgxAnalytics } from 'ngx-analytics'; var __assign = (undefined && undefined.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; var AppInsightsDefaults = (function () { function AppInsightsDefaults() { this.userId = null; } return AppInsightsDefaults; }()); var NgxAnalyticsAppInsights = (function () { function NgxAnalyticsAppInsights(ngxAnalytics$$1, title, router$$1) { var _this = this; this.ngxAnalytics = ngxAnalytics$$1; this.title = title; this.router = router$$1; this.loadStartTime = null; this.loadTime = null; this.metrics = null; this.dimensions = null; this.measurements = null; if (typeof appInsights === 'undefined') { console.warn('appInsights not found'); } var defaults = new AppInsightsDefaults; this.ngxAnalytics.settings.appInsights = __assign({}, defaults, this.ngxAnalytics.settings.appInsights); this.ngxAnalytics.pageTrack.subscribe(function (x) { return _this.pageTrack(x.path); }); this.ngxAnalytics.eventTrack.subscribe(function (x) { return _this.eventTrack(x.action, x.properties); }); this.ngxAnalytics.exceptionTrack.subscribe(function (x) { return _this.exceptionTrack(x); }); this.ngxAnalytics.setUsername.subscribe(function (x) { return _this.setUsername(x); }); this.ngxAnalytics.setUserProperties.subscribe(function (x) { return _this.setUserProperties(x); }); this.router.events .pipe(filter(function (event) { return event instanceof NavigationStart; })) .subscribe(function (event) { return _this.startTimer(); }); this.router.events .pipe(filter(function (event) { return event instanceof NavigationError || event instanceof NavigationEnd; })) .subscribe(function (error) { return _this.stopTimer(); }); } NgxAnalyticsAppInsights.prototype.startTimer = function () { this.loadStartTime = Date.now(); this.loadTime = null; }; NgxAnalyticsAppInsights.prototype.stopTimer = function () { this.loadTime = Date.now() - this.loadStartTime; this.loadStartTime = null; }; NgxAnalyticsAppInsights.prototype.pageTrack = function (path) { appInsights.trackPageView(this.title.getTitle(), path, this.dimensions, this.metrics, this.loadTime); }; NgxAnalyticsAppInsights.prototype.eventTrack = function (name, properties) { appInsights.trackEvent(name, properties, this.measurements); }; NgxAnalyticsAppInsights.prototype.exceptionTrack = function (properties) { var description = properties.event || properties.description || properties; appInsights.trackException(description); }; NgxAnalyticsAppInsights.prototype.setUsername = function (userId) { this.ngxAnalytics.settings.appInsights.userId = userId; appInsights.setAuthenticatedUserContext(userId); }; NgxAnalyticsAppInsights.prototype.setUserProperties = function (properties) { if (properties.userId) { this.ngxAnalytics.settings.appInsights.userId = properties.userId; } if (properties.accountId) { appInsights.setAuthenticatedUserContext(this.ngxAnalytics.settings.appInsights.userId, properties.accountId); } else { appInsights.setAuthenticatedUserContext(this.ngxAnalytics.settings.appInsights.userId); } }; NgxAnalyticsAppInsights.decorators = [ { type: Injectable }, ]; NgxAnalyticsAppInsights.ctorParameters = function () { return [ { type: NgxAnalytics, }, { type: Title, }, { type: Router, }, ]; }; return NgxAnalyticsAppInsights; }()); export { AppInsightsDefaults, NgxAnalyticsAppInsights }; //# sourceMappingURL=appinsights.es2015.js.map