ngx-analytics
Version:
Vendor-agnostic web analytics for Angular2 applications, based on angulartics2
193 lines (188 loc) • 8.9 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('ngx-analytics')) :
typeof define === 'function' && define.amd ? define(['exports', '@angular/core', 'ngx-analytics'], factory) :
(factory((global['ngx-analytics'] = global['ngx-analytics'] || {}, global['ngx-analytics'].ga = {}),global.ng.core,global['ngx-analytics']));
}(this, (function (exports,core,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 GoogleAnalyticsDefaults = (function () {
function GoogleAnalyticsDefaults() {
this.additionalAccountNames = [];
this.userId = null;
this.transport = '';
}
return GoogleAnalyticsDefaults;
}());
var NgxAnalyticsGoogleAnalytics = (function () {
function NgxAnalyticsGoogleAnalytics(ngxAnalytics$$1) {
var _this = this;
this.ngxAnalytics = ngxAnalytics$$1;
this.dimensionsAndMetrics = [];
var defaults = new GoogleAnalyticsDefaults();
this.ngxAnalytics.settings.ga = __assign({}, defaults, this.ngxAnalytics.settings.ga);
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.ngxAnalytics.userTimings.subscribe(function (x) { return _this.userTimings(x); });
}
NgxAnalyticsGoogleAnalytics.prototype.pageTrack = function (path) {
if (typeof _gaq !== 'undefined' && _gaq) {
_gaq.push(['_trackPageview', path]);
for (var _i = 0, _a = this.ngxAnalytics.settings.ga.additionalAccountNames; _i < _a.length; _i++) {
var accountName = _a[_i];
_gaq.push([accountName + '._trackPageview', path]);
}
}
if (typeof ga !== 'undefined' && ga) {
if (this.ngxAnalytics.settings.ga.userId) {
ga('set', '&uid', this.ngxAnalytics.settings.ga.userId);
for (var _b = 0, _c = this.ngxAnalytics.settings.ga.additionalAccountNames; _b < _c.length; _b++) {
var accountName = _c[_b];
ga(accountName + '.set', '&uid', this.ngxAnalytics.settings.ga.userId);
}
}
ga('send', 'pageview', path);
for (var _d = 0, _e = this.ngxAnalytics.settings.ga.additionalAccountNames; _d < _e.length; _d++) {
var accountName = _e[_d];
ga(accountName + '.send', 'pageview', path);
}
}
};
NgxAnalyticsGoogleAnalytics.prototype.eventTrack = function (action, properties) {
if (!properties || !properties.category) {
properties = properties || {};
properties.category = 'Event';
}
if (properties.value) {
var parsed = parseInt(properties.value, 10);
properties.value = isNaN(parsed) ? 0 : parsed;
}
if (typeof ga !== 'undefined') {
var eventOptions = {
eventCategory: properties.category,
eventAction: action,
eventLabel: properties.label,
eventValue: properties.value,
nonInteraction: properties.noninteraction,
page: properties.page || location.hash.substring(1) || location.pathname,
userId: this.ngxAnalytics.settings.ga.userId,
hitCallback: properties.hitCallback,
};
this.setDimensionsAndMetrics(properties);
if (this.ngxAnalytics.settings.ga.transport) {
ga('send', 'event', eventOptions, {
transport: this.ngxAnalytics.settings.ga.transport,
});
}
else {
ga('send', 'event', eventOptions);
}
for (var _i = 0, _a = this.ngxAnalytics.settings.ga.additionalAccountNames; _i < _a.length; _i++) {
var accountName = _a[_i];
ga(accountName + '.send', 'event', eventOptions);
}
}
else if (typeof _gaq !== 'undefined') {
_gaq.push([
'_trackEvent',
properties.category,
action,
properties.label,
properties.value,
properties.noninteraction,
]);
}
};
NgxAnalyticsGoogleAnalytics.prototype.exceptionTrack = function (properties) {
if (properties.fatal === undefined) {
console.log('No "fatal" provided, sending with fatal=true');
properties.fatal = true;
}
properties.exDescription = properties.description;
var eventOptions = {
exFatal: properties.fatal,
exDescription: properties.description,
};
ga('send', 'exception', eventOptions);
for (var _i = 0, _a = this.ngxAnalytics.settings.ga.additionalAccountNames; _i < _a.length; _i++) {
var accountName = _a[_i];
ga(accountName + '.send', 'exception', eventOptions);
}
};
NgxAnalyticsGoogleAnalytics.prototype.userTimings = function (properties) {
if (!properties ||
!properties.timingCategory ||
!properties.timingVar ||
!properties.timingValue) {
console.error('Properties timingCategory, timingVar, and timingValue are required to be set.');
return;
}
if (typeof ga !== 'undefined') {
ga('send', 'timing', properties);
for (var _i = 0, _a = this.ngxAnalytics.settings.ga.additionalAccountNames; _i < _a.length; _i++) {
var accountName = _a[_i];
ga(accountName + '.send', 'timing', properties);
}
}
};
NgxAnalyticsGoogleAnalytics.prototype.setUsername = function (userId) {
this.ngxAnalytics.settings.ga.userId = userId;
};
NgxAnalyticsGoogleAnalytics.prototype.setUserProperties = function (properties) {
this.setDimensionsAndMetrics(properties);
};
NgxAnalyticsGoogleAnalytics.prototype.setDimensionsAndMetrics = function (properties) {
var _this = this;
if (typeof ga === 'undefined') {
return;
}
this.dimensionsAndMetrics.forEach(function (elem) {
if (!properties.hasOwnProperty(elem)) {
ga('set', elem, undefined);
_this.ngxAnalytics.settings.ga.additionalAccountNames.forEach(function (accountName) {
ga(accountName + ".set", elem, undefined);
});
}
});
this.dimensionsAndMetrics = [];
Object.keys(properties).forEach(function (key) {
if (key.lastIndexOf('dimension', 0) === 0 ||
key.lastIndexOf('metric', 0) === 0) {
ga('set', key, properties[key]);
_this.ngxAnalytics.settings.ga.additionalAccountNames.forEach(function (accountName) {
ga(accountName + ".set", key, properties[key]);
});
_this.dimensionsAndMetrics.push(key);
}
});
};
NgxAnalyticsGoogleAnalytics.prototype.createGaSession = function (settings) {
document.write('<script>' +
'(function(i,s,o,g,r,a,m){i["GoogleAnalyticsObject"]=r;i[r]=i[r]||function(){' +
'(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),' +
'm=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)' +
'})(window,document,"script","https://www.google-analytics.com/analytics.js","ga");' +
'ga("create", "' + settings.trackingId + '" , "' + settings.domain + '");' +
'</script>');
};
NgxAnalyticsGoogleAnalytics.decorators = [
{ type: core.Injectable },
];
NgxAnalyticsGoogleAnalytics.ctorParameters = function () { return [
{ type: ngxAnalytics.NgxAnalytics, },
]; };
return NgxAnalyticsGoogleAnalytics;
}());
exports.GoogleAnalyticsDefaults = GoogleAnalyticsDefaults;
exports.NgxAnalyticsGoogleAnalytics = NgxAnalyticsGoogleAnalytics;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=ga.umd.js.map