@kurkov87/ngx-analytics
Version:
Vendor-agnostic web analytics for Angular2 applications, based on angulartics2
100 lines (95 loc) • 5.05 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/platform-browser'), require('@angular/router'), require('rxjs/operators'), require('ngx-analytics')) :
typeof define === 'function' && define.amd ? define(['exports', '@angular/core', '@angular/platform-browser', '@angular/router', 'rxjs/operators', 'ngx-analytics'], factory) :
(factory((global['ngx-analytics'] = global['ngx-analytics'] || {}, global['ngx-analytics'].appinsights = {}),global.ng.core,global.ng.platformBrowser,global.ng.router,global.operators,global['ngx-analytics']));
}(this, (function (exports,core,platformBrowser,router,operators,ngxAnalytics) { 'use strict';
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(operators.filter(function (event) { return event instanceof router.NavigationStart; }))
.subscribe(function (event) { return _this.startTimer(); });
this.router.events
.pipe(operators.filter(function (event) { return event instanceof router.NavigationError || event instanceof router.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: core.Injectable },
];
NgxAnalyticsAppInsights.ctorParameters = function () { return [
{ type: ngxAnalytics.NgxAnalytics, },
{ type: platformBrowser.Title, },
{ type: router.Router, },
]; };
return NgxAnalyticsAppInsights;
}());
exports.AppInsightsDefaults = AppInsightsDefaults;
exports.NgxAnalyticsAppInsights = NgxAnalyticsAppInsights;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=appinsights.umd.js.map