-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analytics): added google analytics service
- Loading branch information
Showing
11 changed files
with
214 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
libs/core/src/lib/services/google-analytics.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { GoogleAnalyticsService } from './google-analytics.service'; | ||
|
||
describe('GoogleAnalyticsService', () => { | ||
beforeEach(() => TestBed.configureTestingModule({})); | ||
|
||
it('should be created', () => { | ||
const service: GoogleAnalyticsService = TestBed.get(GoogleAnalyticsService); | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { NavigationEnd, Router, RouterEvent } from '@angular/router'; | ||
import { distinctUntilChanged, filter, tap } from 'rxjs/operators'; | ||
|
||
export enum EventCategory { | ||
SideNav = 'sideNav', | ||
Outbound = 'outboundLink', | ||
Login = 'login', | ||
} | ||
|
||
export enum EventAction { | ||
Play = 'play', | ||
Click = 'click', | ||
} | ||
|
||
/** | ||
* EventBus will use this service | ||
*/ | ||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class GoogleAnalyticsService { | ||
constructor(public router: Router) {} | ||
|
||
public initPageViewTracking() { | ||
this.router.events | ||
.pipe( | ||
filter(event => event instanceof NavigationEnd), | ||
distinctUntilChanged((previous: any, current: RouterEvent) => { | ||
return previous.url === current.url; | ||
}), | ||
) | ||
.subscribe((event: NavigationEnd) => { | ||
this.setPage(event.urlAfterRedirects); | ||
}); | ||
} | ||
|
||
/** | ||
* set user after login success. | ||
* @param userId | ||
*/ | ||
public setUsername(userId: string) { | ||
if (typeof ga === 'function') { | ||
ga('set', 'userId', userId); | ||
} | ||
} | ||
|
||
/** | ||
* set page after navigation success | ||
* @param path | ||
*/ | ||
public setPage(path: string) { | ||
if (typeof ga === 'function') { | ||
// TODO: remove dynamic data if needed. e.g., /user/USER_ID/profile | ||
ga('set', 'page', path); | ||
ga('send', 'pageview'); | ||
} | ||
} | ||
|
||
/** | ||
* @param eventCategory : 'Video' | ||
* @param eventAction : 'play' | ||
* @param eventLabel : 'Fall Campaign' | ||
* @param eventValue : 42 | ||
*/ | ||
public emitEvent(eventCategory: EventCategory, eventAction: string, eventLabel?: string, eventValue?: number) { | ||
if (typeof ga === 'function') { | ||
ga('send', 'event', { | ||
eventCategory: eventCategory, | ||
eventAction: eventAction, | ||
eventLabel: eventLabel, | ||
eventValue: eventValue, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* @param socialNetwork : 'facebook | ||
* @param socialAction : 'like' | ||
* @param socialTarget : 'http://foo.com' | ||
*/ | ||
public emitSocial(socialNetwork: string, socialAction: string, socialTarget: string) { | ||
if (typeof ga === 'function') { | ||
ga('send', 'social', { | ||
socialNetwork, | ||
socialAction, | ||
socialTarget, | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* @param timingCategory : 'JS Dependencies' | ||
* @param timingVar : 'load' | ||
* @param timingValue: 20 in milliseconds | ||
* @param timingLabel : 'Google CDN' | ||
*/ | ||
|
||
public emitTiming(timingCategory: string, timingVar: string, timingValue: number, timingLabel?: string) { | ||
if (typeof ga === 'function') { | ||
ga('send', 'timing', { | ||
timingCategory, | ||
timingVar, | ||
timingValue, | ||
timingLabel, | ||
}); | ||
} | ||
} | ||
|
||
public emitException(exDescription?: string, exFatal?: boolean) { | ||
if (typeof ga === 'function') { | ||
ga('send', 'exception', { | ||
exDescription, | ||
exFatal, | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.