UNPKG

loop-modules

Version:

Shared modules for the Loop product suite.

64 lines (56 loc) 1.6 kB
// angular import { Router, NavigationStart } from '@angular/router'; export abstract class TrackableEntity { /** * The UTC timestamp the activity started * * @private * @type {number} * @memberOf TrackableEntity */ private started_at: number; /** * The UTC timestamp the activity ended * * @private * @type {number} * @memberOf TrackableEntity */ private ended_at: number; constructor(public router: Router, public routeUrl: string) { if(this.router.routerState.snapshot.url.indexOf(routeUrl) !== -1) { this.started_at = Date.now(); } this.router.events.subscribe((event: any) => { if(event instanceof NavigationStart) { this.ended_at = Date.now(); if(this.started_at) { this.track(this.started_at, this.ended_at); this.clearTracking(); } } }); } /** * Tracking function that will be implemented in the extended classes * * @abstract * @param {number} started_at The UTC timestamp the activity started * @param {number} ended_at The UTC timestamp the activity ended * @returns {*} * * @memberOf TrackableEntity */ abstract track(started_at: number, ended_at: number): any; /** * Clears the tracking meta data * * @private * * @memberOf TrackableEntity */ private clearTracking() { this.started_at = undefined; this.ended_at = undefined; } }