-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathAnalytics.tsx
261 lines (228 loc) · 8.22 KB
/
Analytics.tsx
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* Copyright (c) 2021 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License.AGPL.txt in the project root for license information.
*/
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import Cookies from "js-cookie";
import { v4 } from "uuid";
import { StartWorkspaceError } from "./start/StartPage";
import { RemoteTrackMessage } from "@gitpod/gitpod-protocol/lib/analytics";
export type Event =
| "invite_url_requested"
| "organisation_authorised"
| "dotfile_repo_changed"
| "feedback_submitted"
| "workspace_class_changed"
| "privacy_policy_update_accepted"
| "modal_dismiss"
| "ide_configuration_changed"
| "status_rendered"
| "error_rendered";
type InternalEvent = Event | "path_changed" | "dashboard_clicked";
export type EventProperties =
| TrackOrgAuthorised
| TrackInviteUrlRequested
| TrackDotfileRepo
| TrackFeedback
| TrackPolicyUpdateClick
| TrackModalDismiss
| TrackIDEConfigurationChanged
| TrackWorkspaceClassChanged
| TrackStatusRendered
| TrackErrorRendered;
type InternalEventProperties = EventProperties | TrackDashboardClick | TrackPathChanged;
export interface TrackErrorRendered {
sessionId: string;
instanceId?: string;
workspaceId: string;
type: string;
error: any;
}
export interface TrackStatusRendered {
sessionId: string;
instanceId?: string;
workspaceId: string;
type: string;
phase?: string;
}
export interface TrackWorkspaceClassChanged {}
export interface TrackIDEConfigurationChanged {
location: string;
name?: string;
version?: string;
}
export interface TrackModalDismiss {
manner: string;
title?: string;
specify?: string;
path: string;
}
export interface TrackOrgAuthorised {
installation_id: string;
setup_action: string | undefined;
}
export interface TrackInviteUrlRequested {
invite_url: string;
}
export interface TrackDotfileRepo {
previous?: string;
current: string;
}
export interface TrackFeedback {
score: number;
feedback: string;
href: string;
path: string;
error_object?: StartWorkspaceError;
error_message?: string;
}
export interface TrackPolicyUpdateClick {
path: string;
success: boolean;
}
interface TrackDashboardClick {
dnt?: boolean;
path: string;
button_type?: string;
label?: string;
destination?: string;
}
interface TrackPathChanged {
prev: string;
path: string;
}
interface Traits {
unsubscribed_onboarding?: boolean;
unsubscribed_changelog?: boolean;
unsubscribed_devx?: boolean;
}
//call this to track all events outside of button and anchor clicks
export function trackEvent(event: "invite_url_requested", properties: TrackInviteUrlRequested): void;
export function trackEvent(event: "organisation_authorised", properties: TrackOrgAuthorised): void;
export function trackEvent(event: "dotfile_repo_changed", properties: TrackDotfileRepo): void;
export function trackEvent(event: "feedback_submitted", properties: TrackFeedback): void;
export function trackEvent(event: "workspace_class_changed", properties: TrackWorkspaceClassChanged): void;
export function trackEvent(event: "privacy_policy_update_accepted", properties: TrackPolicyUpdateClick): void;
export function trackEvent(event: "modal_dismiss", properties: TrackModalDismiss): void;
export function trackEvent(event: "ide_configuration_changed", properties: TrackIDEConfigurationChanged): void;
export function trackEvent(event: "status_rendered", properties: TrackStatusRendered): void;
export function trackEvent(event: "error_rendered", properties: TrackErrorRendered): void;
export function trackEvent(event: Event, properties: EventProperties): void {
trackEventInternal(event, properties);
}
const trackEventInternal = (event: InternalEvent, properties: InternalEventProperties) => {
sendTrackEvent({
anonymousId: getAnonymousId(),
event,
properties,
});
};
// Please use trackEvent instead of this function
export function sendTrackEvent(message: RemoteTrackMessage): void {
sendAnalytics("trackEvent", message);
}
export const trackButtonOrAnchor = (target: HTMLAnchorElement | HTMLButtonElement | HTMLDivElement) => {
//read manually passed analytics props from 'data-analytics' attribute of event target
let passedProps: TrackDashboardClick | undefined;
if (target.dataset.analytics) {
try {
passedProps = JSON.parse(target.dataset.analytics) as TrackDashboardClick;
if (passedProps.dnt) {
return;
}
} catch (error) {
log.debug(error);
}
}
let trackingMsg: TrackDashboardClick = {
path: window.location.pathname,
label: target.textContent || undefined,
};
if (target instanceof HTMLButtonElement || target instanceof HTMLDivElement) {
//parse button data
if (target.classList.contains("secondary")) {
trackingMsg.button_type = "secondary";
} else {
trackingMsg.button_type = "primary"; //primary button is the default if secondary is not specified
}
//retrieve href if parent is an anchor element
if (target.parentElement instanceof HTMLAnchorElement) {
const anchor = target.parentElement as HTMLAnchorElement;
trackingMsg.destination = anchor.href;
}
}
if (target instanceof HTMLAnchorElement) {
const anchor = target as HTMLAnchorElement;
trackingMsg.destination = anchor.href;
}
const getAncestorProps = (curr: HTMLElement | null): TrackDashboardClick | undefined => {
if (!curr || curr instanceof Document) {
return;
}
const ancestorProps: TrackDashboardClick | undefined = getAncestorProps(curr.parentElement);
const currProps = JSON.parse(curr.dataset.analytics || "{}") as TrackDashboardClick;
return { ...ancestorProps, ...currProps };
};
const ancestorProps = getAncestorProps(target);
//props that were passed directly to the event target take precedence over those passed to ancestor elements, which take precedence over those implicitly determined.
trackingMsg = { ...trackingMsg, ...ancestorProps, ...passedProps };
trackEventInternal("dashboard_clicked", trackingMsg);
};
//call this when the path changes. Complete page call is unnecessary for SPA after initial call
export const trackPathChange = (props: TrackPathChanged) => {
trackEventInternal("path_changed", props);
};
type TrackLocationProperties = {
referrer: string;
path: string;
host: string;
url: string;
};
export const trackLocation = async (includePII: boolean) => {
const props: TrackLocationProperties = {
referrer: document.referrer,
path: window.location.pathname,
host: window.location.hostname,
url: window.location.href,
};
sendAnalytics("trackLocation", {
//if the user is authenticated, let server determine the id. else, pass anonymousId explicitly.
includePII: includePII,
anonymousId: getAnonymousId(),
properties: props,
});
};
export const identifyUser = async (traits: Traits) => {
sendAnalytics("identifyUser", {
anonymousId: getAnonymousId(),
traits: traits,
});
};
function sendAnalytics(operation: "trackEvent" | "trackLocation" | "identifyUser", message: any) {
fetch("/_analytics/" + operation, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(message),
credentials: "include",
});
}
const getCookieConsent = () => {
return Cookies.get("gp-analytical") === "true";
};
const getAnonymousId = (): string | undefined => {
if (!getCookieConsent()) {
//we do not want to read or set the id cookie if we don't have consent
return;
}
let anonymousId = Cookies.get("ajs_anonymous_id");
if (anonymousId) {
return anonymousId.replace(/^"(.+(?="$))"$/, "$1"); //strip enclosing double quotes before returning
} else {
anonymousId = v4();
Cookies.set("ajs_anonymous_id", anonymousId, { domain: "." + window.location.hostname, expires: 365 });
}
return anonymousId;
};