UNPKG

angular-ga

Version:

Google Analytics for your Angular application

110 lines (104 loc) 4.24 kB
import { __spread, __decorate, __param } from 'tslib'; import { InjectionToken, EventEmitter, Optional, Inject, Injectable, NgModule } from '@angular/core'; import { ReplaySubject, timer, throwError, interval } from 'rxjs'; import { switchMap, takeUntil, filter, first, tap } from 'rxjs/operators'; var GA_TOKEN = new InjectionToken('angular-ga TrackingId'); var GA_OPTIONS = new InjectionToken('angular-ga Tracking Options'); var GoogleAnalyticsService = /** @class */ (function () { function GoogleAnalyticsService(trackingId, options) { this.event = new EventEmitter(); this.pageview = new EventEmitter(); this.queue = new ReplaySubject(); if (trackingId) { this.configure(trackingId, options); } } GoogleAnalyticsService.prototype.configure = function (trackingId, options) { var _this = this; if (options === void 0) { options = 'auto'; } this.ga('create', trackingId, options); this.ga('send', 'pageview'); this.event.subscribe(function (x) { _this.onEvent(x); }); this.pageview.subscribe(function (x) { _this.onPageView(x); }); var timer$ = timer(20000) .pipe(switchMap(function () { return throwError(new Error('Could not load GA')); })); interval(50) .pipe(takeUntil(timer$), filter(function () { return Boolean(window.ga); }), first(), switchMap(function () { return _this.queue; }), tap(function (args) { var _a; (_a = window).ga.apply(_a, __spread(args)); // tslint:disable-line:no-unsafe-any })) .subscribe(); }; GoogleAnalyticsService.prototype.set = function (key, value) { if (typeof key !== 'string' && typeof key !== 'object') { throw new TypeError("Expected `fieldName` to be of type `string` or `object`, got `" + typeof key + "`"); } if (typeof key === 'string' && value === undefined) { throw new TypeError('Expected `fieldValue` to not be `undefined`'); } if (typeof key === 'object') { this.ga('set', key); } else { this.ga('set', key, value); } }; GoogleAnalyticsService.prototype.onEvent = function (event) { this.ga('send', 'event', event.category, event.action, event.label, event.value); }; GoogleAnalyticsService.prototype.onPageView = function (pageview) { var fieldsObject = {}; if (pageview.title) { fieldsObject.title = pageview.title; } this.ga('send', 'pageview', pageview.page, fieldsObject); }; GoogleAnalyticsService.prototype.ga = function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } this.queue.next(args); }; GoogleAnalyticsService.ctorParameters = function () { return [ { type: String, decorators: [{ type: Optional }, { type: Inject, args: [GA_TOKEN,] }] }, { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [GA_OPTIONS,] }] } ]; }; GoogleAnalyticsService = __decorate([ Injectable(), __param(0, Optional()), __param(0, Inject(GA_TOKEN)), __param(1, Optional()), __param(1, Inject(GA_OPTIONS)) ], GoogleAnalyticsService); return GoogleAnalyticsService; }()); var GoogleAnalyticsModule = /** @class */ (function () { function GoogleAnalyticsModule() { } GoogleAnalyticsModule_1 = GoogleAnalyticsModule; GoogleAnalyticsModule.forRoot = function () { return { ngModule: GoogleAnalyticsModule_1, providers: [ GoogleAnalyticsService ] }; }; var GoogleAnalyticsModule_1; GoogleAnalyticsModule = GoogleAnalyticsModule_1 = __decorate([ NgModule({ imports: [], declarations: [], exports: [] }) ], GoogleAnalyticsModule); return GoogleAnalyticsModule; }()); /** * Generated bundle index. Do not edit. */ export { GA_OPTIONS, GA_TOKEN, GoogleAnalyticsModule, GoogleAnalyticsService }; //# sourceMappingURL=angular-ga.js.map