forked from adamlaska/circleci-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.js
More file actions
98 lines (84 loc) · 2.79 KB
/
analytics.js
File metadata and controls
98 lines (84 loc) · 2.79 KB
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
import Cookies from 'js-cookie';
import { isDataDog } from '../utils';
class AnalyticsClient {
static getSessionId() {
var existingSessionId = Number(Cookies.get('amplitude-session-id'));
// Number.isNaN polyfill:
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
const isNaN = Number.isNaN || ((value) => value !== value);
return !isNaN(existingSessionId) ? existingSessionId : Date.now();
}
/**
* Thin wrapper function around Segment Analytics track method
* @param name Name of the action the current user performed.
* @param properties A bag of metadata to send to Segment/Amplitude.
*/
static trackAction(name, properties) {
if (isDataDog()) return;
properties = properties ?? null;
window.analytics &&
window.analytics.track(name, properties, {
integrations: {
Amplitude: { session_id: AnalyticsClient.getSessionId() },
},
});
}
/**
* Thin wrapper function around Segment Analytics identify method
* @param userData A dictionary of traits you know about the user, like their email, name, source.
* @param traits Data about the current user session including source.
*/
static trackUser(id, traits) {
if (isDataDog()) return;
traits = traits ?? null;
window.analytics &&
window.analytics.identify(id, traits, {
integrations: {
Amplitude: { session_id: AnalyticsClient.getSessionId() },
},
});
}
/**
* Thin wrapper function around Segment Analytics page method
* @param name Name of the page the current user viewed.
* @param properties A bag of metadata to send to Segment/Amplitude.
*/
static trackPage(name, properties) {
if (isDataDog()) return;
properties = properties ?? null;
window.analytics &&
window.analytics.page('docs', name, properties, {
integrations: {
Amplitude: { session_id: AnalyticsClient.getSessionId() },
},
});
}
}
export default AnalyticsClient;
// legacy tracking code:
// This is still used in the src-shared/*.js.
// analytics.track wrapper
const setCookieMinutes = (name, value, path, expiration) => {
// expiration is set in minutes
let date = new Date();
date.setMinutes(date.getMinutes() + expiration);
date = date.toUTCString();
document.cookie = name + '=' + value + '; path=' + path + '; expires=' + date;
};
const trackEvent = (name, properties, options, callback) => {
if (!window.analytics) {
return;
}
analytics.track(name, properties, options, function () {
setCookieMinutes(
'amplitude-session-id',
AnalyticsClient.getSessionId(),
'/',
30,
);
if (callback) {
callback();
}
});
};
window.trackEvent = trackEvent; // enable use in src-shared files.