-
Notifications
You must be signed in to change notification settings - Fork 191
/
appinsights.ts
156 lines (139 loc) · 4.54 KB
/
appinsights.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { Injectable } from '@angular/core';
import { Title } from '@angular/platform-browser';
import {
NavigationEnd,
NavigationError,
NavigationStart,
Router,
} from '@angular/router';
import { filter } from 'rxjs/operators';
import { AppInsightsSettings } from '../../angulartics2-config';
import { Angulartics2 } from '../../angulartics2-core';
declare const appInsights: Microsoft.ApplicationInsights.IAppInsights;
export class AppInsightsDefaults implements AppInsightsSettings {
userId = null;
}
@Injectable({ providedIn: 'root' })
export class Angulartics2AppInsights {
loadStartTime: number = null;
loadTime: number = null;
metrics: { [name: string]: number } = null;
dimensions: { [name: string]: string } = null;
measurements: { [name: string]: number } = null;
constructor(
private angulartics2: Angulartics2,
private title: Title,
private router: Router
) {
if (typeof appInsights === 'undefined') {
console.warn('appInsights not found');
}
const defaults = new AppInsightsDefaults();
// Set the default settings for this module
this.angulartics2.settings.appInsights = {
...defaults,
...this.angulartics2.settings.appInsights,
};
this.angulartics2.setUsername.subscribe((x: string) => this.setUsername(x));
this.angulartics2.setUserProperties.subscribe((x) =>
this.setUserProperties(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));
this.angulartics2.exceptionTrack
.pipe(this.angulartics2.filterDeveloperMode())
.subscribe((x) => this.exceptionTrack(x));
this.router.events
.pipe(
this.angulartics2.filterDeveloperMode(),
filter((event) => event instanceof NavigationStart)
)
.subscribe((event) => this.startTimer());
this.router.events
.pipe(
filter(
(event) =>
event instanceof NavigationError || event instanceof NavigationEnd
)
)
.subscribe((error) => this.stopTimer());
}
startTimer() {
this.loadStartTime = Date.now();
this.loadTime = null;
}
stopTimer() {
this.loadTime = Date.now() - this.loadStartTime;
this.loadStartTime = null;
}
/**
* Page Track in Baidu Analytics
*
* @param path - Location 'path'
*
* @link https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackpageview
*/
pageTrack(path: string) {
appInsights.trackPageView(
this.title.getTitle(),
path,
this.dimensions,
this.metrics,
this.loadTime
);
}
/**
* Log a user action or other occurrence.
*
* @param name Name to identify this event in the portal.
* @param properties Additional data used to filter events and metrics in the portal. Defaults to empty.
*
* @link https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackevent
*/
eventTrack(name: string, properties: { [name: string]: string }) {
appInsights.trackEvent(name, properties, this.measurements);
}
/**
* Exception Track Event in GA
*
* @param properties - Comprised of the mandatory fields 'appId' (string), 'appName' (string) and 'appVersion' (string) and
* optional fields 'fatal' (boolean) and 'description' (string), error
*
* @link https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#trackexception
*/
exceptionTrack(properties: any) {
const description =
properties.event || properties.description || properties;
appInsights.trackException(description);
}
/**
* @link https://github.com/Microsoft/ApplicationInsights-JS/blob/master/API-reference.md#setauthenticatedusercontext
*/
setUsername(userId: string) {
this.angulartics2.settings.appInsights.userId = userId;
appInsights.setAuthenticatedUserContext(userId);
}
setUserProperties(
properties: Partial<{ userId: string; accountId: string }>
) {
if (properties.userId) {
this.angulartics2.settings.appInsights.userId = properties.userId;
}
if (properties.accountId) {
appInsights.setAuthenticatedUserContext(
this.angulartics2.settings.appInsights.userId,
properties.accountId
);
} else {
appInsights.setAuthenticatedUserContext(
this.angulartics2.settings.appInsights.userId
);
}
}
}