src/client/app/modules/analytics/services/analytics.service.ts
Base class Standardizes tracking actions and categorization
Properties |
Methods |
constructor(analytics: AnalyticsService)
|
track |
track(action: string, properties: IAnalyticsProperties)
|
Track actions, events, etc.
Returns :
void
|
Public analytics |
analytics:
|
Type : AnalyticsService
|
Decorators : Inject
|
Public category |
category:
|
Type : string
|
import { Injectable, Inject } from '@angular/core';
import * as _ from 'lodash';
import { Angulartics2, Angulartics2Segment } from 'angulartics2';
export interface IAnalyticsProperties {
category?: string;
label?: string;
value?: number;
}
export interface IAnalytics {
track(action: string, properties: IAnalyticsProperties): void;
}
/**
* Wrapper for Angulartics2
*/
@Injectable()
export class AnalyticsService implements IAnalytics {
constructor(private angulartics2: Angulartics2, private segment: Angulartics2Segment) {
// options
// https://github.com/angulartics/angulartics2/blob/master/src/core/angulartics2.ts#L90-L104
/*tics2.virtualPageviews(value: boolean);
tics2.excludeRoutes(routes: Array<string>);
tics2.firstPageview(value: boolean);
tics2.withBase(value: string);*/
this.devMode(false);
}
/**
* Track actions, events, etc.
**/
public track(action: string, properties: IAnalyticsProperties): void {
if (!this.devMode()) {
this.segment.eventTrack(action, properties);
}
}
/**
* Called automatically by default with Angular 2 Routing
* However, that can be turned off and this could be used manually
**/
public pageTrack(path: string, location: any) {
if (!this.devMode()) {
this.segment.pageTrack(path, location);
}
}
/**
* Identify authenticated users
**/
public identify(properties: any) {
if (!this.devMode()) {
this.segment.setUserProperties(properties);
}
}
/**
* Control whether analytics are tracked
* true: dev mode on, therefore do not track anything
* false: dev mode off, track everything
**/
public devMode(enable?: boolean): boolean {
if (typeof enable !== 'undefined') {
this.angulartics2.developerMode(enable);
}
return this.angulartics2.settings.developerMode;
}
}
/**
* Base class
* Standardizes tracking actions and categorization
*/
export class Analytics implements IAnalytics {
// sub-classes should define their category
public category: string;
constructor(@Inject(AnalyticsService) public analytics: AnalyticsService) {
}
/**
* Track actions, events, etc.
**/
track(action: string, properties: IAnalyticsProperties): void {
this.analytics.track(action, _.extend(properties, { category: this.category }));
}
}