-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
analytics.ts
341 lines (305 loc) · 10.5 KB
/
analytics.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
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
import Analytics from "analytics-node";
import { Location } from "history";
import _ from "lodash";
import { Store } from "redux";
import * as protos from "src/js/protos";
import { versionsSelector } from "src/redux/nodes";
import { AdminUIState } from "src/redux/state";
import { history } from "src/redux/history";
import { COCKROACHLABS_ADDR } from "src/util/cockroachlabsAPI";
type ClusterResponse = protos.cockroach.server.serverpb.IClusterResponse;
interface TrackMessage {
event: string;
properties?: Object;
timestamp?: Date;
context?: Object;
}
/**
* List of current redactions needed for pages tracked by the Admin UI.
* TODO(mrtracy): It this list becomes more extensive, it might benefit from a
* set of tests as a double-check.
*/
export const defaultRedactions = [
// When viewing a specific database, the database name and table are part of
// the URL path.
{
match: new RegExp("/databases/database/.+/table/.+"),
replace: "/databases/database/[db]/table/[tbl]",
},
{
match: new RegExp("/database/.+/table/.+"),
replace: "/database/[db]/table/[tbl]",
},
{
match: new RegExp("/database/.+/tables"),
replace: "/database/[db]/tables",
},
{
match: new RegExp("/database/.+/table"),
replace: "/database/[db]/table",
},
{
match: new RegExp("/database/.+/grants"),
replace: "/database/[db]/grants",
},
{
match: new RegExp("/database/.+"),
replace: "/database/[db]",
},
// The clusterviz map page, which puts localities in the URL.
{
match: new RegExp("/overview/map((/.+)+)"),
useFunction: true, // I hate TypeScript.
replace: function countTiers(original: string, localities: string) {
const tierCount = localities.match(new RegExp("/", "g")).length;
let redactedLocalities = "";
for (let i = 0; i < tierCount; i++) {
redactedLocalities += "/[locality]";
}
return original.replace(localities, redactedLocalities);
},
},
// The statement details page, with a full SQL statement in the URL.
{
match: new RegExp("/statement/.*"),
replace: "/statement/[statement]",
},
];
type PageTrackReplacementFunction = (match: string, ...args: any[]) => string;
type PageTrackReplacement = string | PageTrackReplacementFunction;
/**
* A PageTrackRedaction describes a regular expression used to identify PII
* in strings that are being sent to analytics. If a string matches the given
* "match" RegExp, it will be replaced with the "replace" string before being
* sent to analytics.
*/
interface PageTrackRedaction {
match: RegExp;
replace: PageTrackReplacement;
useFunction?: boolean; // I hate Typescript.
}
/**
* AnalyticsSync is used to dispatch analytics event from the Admin UI to an
* analytics service (currently Segment). It combines information on individual
* events with user information from the redux state in order to properly
* identify events.
*/
export class AnalyticsSync {
/**
* queuedPages are used to store pages visited before the cluster ID
* is available. Once the cluster ID is available, the next call to page()
* will dispatch all queued locations to the underlying analytics API.
*/
private queuedPages: Location[] = [];
/**
* sentIdentifyEvent tracks whether the identification event has already
* been sent for this session. This event is not sent until all necessary
* information has been retrieved (current version of cockroachDB,
* cluster settings).
*/
private identifyEventSent = false;
/**
* Construct a new AnalyticsSync object.
* @param analyticsService Underlying interface to push to the analytics service.
* @param deprecatedStore The redux store for the Admin UI. [DEPRECATED]
* @param redactions A list of redaction regular expressions, used to
* scrub any potential personally-identifying information from the data
* being tracked.
*/
constructor(
private analyticsService: Analytics,
private deprecatedStore: Store<AdminUIState>,
private redactions: PageTrackRedaction[],
) {}
/**
* page should be called whenever the user moves to a new page in the
* application.
* @param location The location (URL information) of the page.
*/
page(location: Location) {
// If the cluster ID is not yet available, queue the location to be
// pushed later.
const cluster = this.getCluster();
if (cluster === null) {
this.queuedPages.push(location);
return;
}
const { cluster_id, reporting_enabled } = cluster;
// A cluster setting determines if diagnostic reporting is enabled. If
// it is not explicitly enabled, do nothing.
if (!reporting_enabled) {
if (this.queuedPages.length > 0) {
this.queuedPages = [];
}
return;
}
// If there are any queued pages, push them.
_.each(this.queuedPages, l => this.pushPage(cluster_id, l));
this.queuedPages = [];
// Push the page that was just accessed.
this.pushPage(cluster_id, location);
}
/**
* identify attempts to send an "identify" event to the analytics service.
* The identify event will only be sent once per session; if it has already
* been sent, it will be a no-op whenever called afterwards.
*/
identify() {
if (this.identifyEventSent) {
return;
}
// Do nothing if Cluster information is not yet available.
const cluster = this.getCluster();
if (cluster === null) {
return;
}
const { cluster_id, reporting_enabled, enterprise_enabled } = cluster;
if (!reporting_enabled) {
return;
}
// Do nothing if version information is not yet available.
const state = this.deprecatedStore.getState();
const versions = versionsSelector(state);
if (_.isEmpty(versions)) {
return;
}
this.analyticsService.identify({
userId: cluster_id,
traits: {
version: versions[0],
userAgent: window.navigator.userAgent,
enterprise: enterprise_enabled,
},
});
this.identifyEventSent = true;
}
/** Analytics Track for Segment: https://segment.com/docs/connections/spec/track/ */
track(msg: TrackMessage) {
const cluster = this.getCluster();
if (cluster === null) {
return;
}
// get cluster_id to id the event
const { cluster_id } = cluster;
const pagePath = this.redact(history.location.pathname);
// break down properties from message
const { properties, ...rest } = msg;
const props = {
pagePath,
...properties,
};
const message = {
userId: cluster_id,
properties: { ...props },
...rest,
};
this.analyticsService.track(message);
}
/**
* Return the ClusterID from the store, returning null if the clusterID
* has not yet been fetched. We can depend on the alertdatasync component
* to eventually retrieve this without having to request it ourselves.
*/
private getCluster(): ClusterResponse | null {
const state = this.deprecatedStore.getState();
// Do nothing if cluster ID has not been loaded.
const cluster = state.cachedData.cluster;
if (!cluster || !cluster.data) {
return null;
}
return cluster.data;
}
/**
* pushPage pushes a single "page" event to the analytics service.
*/
private pushPage = (userID: string, location: Location) => {
// Loop through redactions, if any matches return the appropriate
// redacted string.
const path = this.redact(location.pathname);
let search = "";
if (location.search && location.search.length > 1) {
const query = location.search.slice(1);
const params = new URLSearchParams(query);
params.forEach((value, key) => {
params.set(key, this.redact(value));
});
search = "?" + params.toString();
}
this.analyticsService.page({
userId: userID,
name: path,
properties: {
path,
search,
},
});
};
private redact(path: string): string {
_.each(this.redactions, r => {
if (r.match.test(path)) {
// Apparently TypeScript doesn't know how to dispatch functions.
// If there are two function overloads defined (as with
// String.prototype.replace), it is unable to recognize that
// a union of the two types can be successfully passed in as a
// parameter of that function. We have to explicitly
// disambiguate the types for it.
// See https://github.com/Microsoft/TypeScript/issues/14107
if (r.useFunction) {
path = path.replace(
r.match,
r.replace as PageTrackReplacementFunction,
);
} else {
path = path.replace(r.match, r.replace as string);
}
return false;
}
});
return path;
}
}
export let analytics: AnalyticsSync | undefined;
export function initializeAnalytics(store: Store<AdminUIState>) {
// Create a global instance of AnalyticsSync which can be used from various
// packages. If enabled, this instance will push to segment using the following
// analytics key.
const analyticsOpts = {
host: COCKROACHLABS_ADDR + "/api/segment",
};
const analyticsInstance = new Analytics(
"5Vbp8WMYDmZTfCwE0uiUqEdAcTiZWFDb",
analyticsOpts,
);
analytics = new AnalyticsSync(analyticsInstance, store, defaultRedactions);
// Attach a listener to the history object which will track a 'page' event
// whenever the user navigates to a new path.
let lastPageLocation: Location;
history.listen((location: Location) => {
// Do not log if the pathname is the same as the previous.
// Needed because history.listen() fires twice when using hash history, this
// bug is "won't fix" in the version of history we are using, and upgrading
// would imply a difficult upgrade to react-router v4.
// (https://github.com/ReactTraining/history/issues/427).
if (lastPageLocation && lastPageLocation.pathname === location.pathname) {
return;
}
lastPageLocation = location;
analytics.page(location);
// Identify the cluster.
analytics.identify();
});
// Record the initial page that was accessed; listen won't fire for the first
// page loaded.
analytics.page(history.location);
// Identify the cluster.
analytics.identify();
}