|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { NavigationEnd, Router, RouterEvent } from '@angular/router'; |
| 3 | +import { distinctUntilChanged, filter, tap } from 'rxjs/operators'; |
| 4 | + |
| 5 | +export enum EventCategory { |
| 6 | + SideNav = 'sideNav', |
| 7 | + Outbound = 'outboundLink', |
| 8 | + Login = 'login', |
| 9 | +} |
| 10 | + |
| 11 | +export enum EventAction { |
| 12 | + Play = 'play', |
| 13 | + Click = 'click', |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * EventBus will use this service |
| 18 | + */ |
| 19 | +@Injectable({ |
| 20 | + providedIn: 'root', |
| 21 | +}) |
| 22 | +export class GoogleAnalyticsService { |
| 23 | + constructor(public router: Router) {} |
| 24 | + |
| 25 | + public initPageViewTracking() { |
| 26 | + this.router.events |
| 27 | + .pipe( |
| 28 | + filter(event => event instanceof NavigationEnd), |
| 29 | + distinctUntilChanged((previous: any, current: RouterEvent) => { |
| 30 | + return previous.url === current.url; |
| 31 | + }), |
| 32 | + ) |
| 33 | + .subscribe((event: NavigationEnd) => { |
| 34 | + this.setPage(event.urlAfterRedirects); |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * set user after login success. |
| 40 | + * @param userId |
| 41 | + */ |
| 42 | + public setUsername(userId: string) { |
| 43 | + if (typeof ga === 'function') { |
| 44 | + ga('set', 'userId', userId); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * set page after navigation success |
| 50 | + * @param path |
| 51 | + */ |
| 52 | + public setPage(path: string) { |
| 53 | + if (typeof ga === 'function') { |
| 54 | + // TODO: remove dynamic data if needed. e.g., /user/USER_ID/profile |
| 55 | + ga('set', 'page', path); |
| 56 | + ga('send', 'pageview'); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param eventCategory : 'Video' |
| 62 | + * @param eventAction : 'play' |
| 63 | + * @param eventLabel : 'Fall Campaign' |
| 64 | + * @param eventValue : 42 |
| 65 | + */ |
| 66 | + public emitEvent(eventCategory: EventCategory, eventAction: string, eventLabel?: string, eventValue?: number) { |
| 67 | + if (typeof ga === 'function') { |
| 68 | + ga('send', 'event', { |
| 69 | + eventCategory: eventCategory, |
| 70 | + eventAction: eventAction, |
| 71 | + eventLabel: eventLabel, |
| 72 | + eventValue: eventValue, |
| 73 | + }); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @param socialNetwork : 'facebook |
| 79 | + * @param socialAction : 'like' |
| 80 | + * @param socialTarget : 'http://foo.com' |
| 81 | + */ |
| 82 | + public emitSocial(socialNetwork: string, socialAction: string, socialTarget: string) { |
| 83 | + if (typeof ga === 'function') { |
| 84 | + ga('send', 'social', { |
| 85 | + socialNetwork, |
| 86 | + socialAction, |
| 87 | + socialTarget, |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @param timingCategory : 'JS Dependencies' |
| 94 | + * @param timingVar : 'load' |
| 95 | + * @param timingValue: 20 in milliseconds |
| 96 | + * @param timingLabel : 'Google CDN' |
| 97 | + */ |
| 98 | + |
| 99 | + public emitTiming(timingCategory: string, timingVar: string, timingValue: number, timingLabel?: string) { |
| 100 | + if (typeof ga === 'function') { |
| 101 | + ga('send', 'timing', { |
| 102 | + timingCategory, |
| 103 | + timingVar, |
| 104 | + timingValue, |
| 105 | + timingLabel, |
| 106 | + }); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public emitException(exDescription?: string, exFatal?: boolean) { |
| 111 | + if (typeof ga === 'function') { |
| 112 | + ga('send', 'exception', { |
| 113 | + exDescription, |
| 114 | + exFatal, |
| 115 | + }); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments