-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathposthog.ts
129 lines (114 loc) · 3.15 KB
/
posthog.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { inject, Injectable } from '@angular/core';
import { Angulartics2 } from '../../angulartics2-core';
declare let posthog: any;
@Injectable({ providedIn: 'root' })
export class Angulartics2Posthog {
private readonly angulartics2 = inject(Angulartics2);
constructor() {
this.angulartics2.setUsername.subscribe(x => this.setUsername(x));
this.angulartics2.setUserProperties.subscribe(x => this.setUserProperties(x));
this.angulartics2.setUserPropertiesOnce.subscribe(x => this.setUserPropertiesOnce(x));
this.angulartics2.setSuperProperties.subscribe(x => this.setSuperProperties(x));
this.angulartics2.setSuperPropertiesOnce.subscribe(x => this.setSuperPropertiesOnce(x));
this.angulartics2.setAlias.subscribe(x => this.setAlias(x));
}
startTracking(): void {
this.startPageTracking();
this.startEventTracking();
}
startPageTracking(): void {
this.angulartics2.pageTrack.pipe(this.angulartics2.filterDeveloperMode()).subscribe(x => {
this.pageTrack(x.path);
});
}
startEventTracking(): void {
this.angulartics2.eventTrack.pipe(this.angulartics2.filterDeveloperMode()).subscribe(x => {
this.eventTrack(x.action, x.properties);
});
}
pageTrack(path: string | undefined): void {
try {
posthog.capture('Page Viewed', { page: path });
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
eventTrack(action: string, properties?: any): void {
try {
posthog.capture(action, properties);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
setUsername(userId: string | { userId: string | number }): void {
try {
posthog.identify(userId);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
setUserProperties(properties: any | undefined): void {
try {
posthog.identify(properties["distinct_id"], properties);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
setUserPropertiesOnce(properties: any | undefined): void {
try {
posthog.capture('Set User Properties Once', {
$set_once: properties
});
}
catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
setSuperProperties(properties: any | undefined): void {
try {
posthog.capture('Set Super Properties', { $set: properties });
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
setSuperPropertiesOnce(properties: any | undefined): void {
try {
posthog.capture('Set Super Properties Once', { $set_once: properties });
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
setAlias(alias: string): void {
try {
posthog.alias(alias);
}
catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
setGroup(groupType: string, groupKey: string, properties: any) {
try {
posthog.group(groupType, groupKey, properties);
} catch (e) {
if (!(e instanceof ReferenceError)) {
throw e;
}
}
}
}