-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathsegment.ts
97 lines (88 loc) · 2.48 KB
/
segment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { Injectable } from '@angular/core';
import { Angulartics2 } from '../../angulartics2-core';
declare var analytics: SegmentAnalytics.AnalyticsJS;
@Injectable({ providedIn: 'root' })
export class Angulartics2Segment {
constructor(private angulartics2: Angulartics2) {
this.angulartics2.setUserProperties.subscribe(x => this.setUserProperties(x));
this.angulartics2.setUserPropertiesOnce.subscribe(x => this.setUserProperties(x));
this.angulartics2.setAlias.subscribe(x => this.setAlias(x));
}
startTracking(): void {
this.angulartics2.pageTrack
.pipe(this.angulartics2.filterDeveloperMode())
.subscribe(x => this.pageTrack(x.path));
this.angulartics2.eventTrack
.pipe(this.angulartics2.filterDeveloperMode())
.subscribe(x => this.eventTrack(x.action, x.properties));
}
/**
* https://segment.com/docs/libraries/analytics.js/#page
*
* analytics.page([category], [name], [properties], [options], [callback]);
*/
pageTrack(path: string) {
// TODO : Support optional parameters where the parameter order and type changes their meaning
try {
analytics.page(path);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
/**
* https://segment.com/docs/libraries/analytics.js/#track
*
* analytics.track(event, [properties], [options], [callback]);
*/
eventTrack(action: string, properties: any) {
try {
analytics.track(action, properties);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
/**
* https://segment.com/docs/libraries/analytics.js/#identify
*
* analytics.identify([userId], [traits], [options], [callback]);
*/
setUserProperties(properties: any) {
try {
if (properties.userId) {
analytics.identify(properties.userId, properties);
} else {
analytics.identify(properties);
}
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
/**
* https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/#reset--logout
*
* analytics.reset();
*/
unsetUserProperties() {
analytics.reset();
}
/**
* https://segment.com/docs/libraries/analytics.js/#alias
*
* analytics.alias(userId, previousId, options, callback);
*/
setAlias(alias: any) {
try {
analytics.alias(alias);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
}