forked from adamlaska/circleci-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.js
More file actions
70 lines (58 loc) · 1.81 KB
/
user.js
File metadata and controls
70 lines (58 loc) · 1.81 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
import Cookies from 'js-cookie';
import { updateCookieExpiration } from '../utils';
export function setUserData(userData) {
window.userData = userData;
const { name, jekyllProperties } = window.currentPage;
if (window.userData.created_at) {
AnalyticsClient.trackPage(name, {
...jekyllProperties,
user_account_created_at: userData.created_at,
});
} else {
AnalyticsClient.trackPage(name, jekyllProperties);
}
// emit an event to let the system know that userData is ready/has changed
const userDataReady = new CustomEvent('userDataReady');
window.dispatchEvent(userDataReady);
}
export function setLoggedIn(userData) {
$(document.body).addClass('loggedin');
Cookies.set('cci-customer', 'true', { expires: 365 * 2 });
setUserData(userData);
}
export function setLoggedOut() {
$(document.body).removeClass('loggedin');
Cookies.set('cci-customer', 'false', { expires: 365 * 2 });
setUserData({});
}
export function fetchUserData() {
// Update cookie expiry (migrating from 10 years to 2 years)
updateCookieExpiration('cci-customer', 365 * 2);
if (Cookies.get('cci-customer') === 'true') {
$(document.body).addClass('loggedin');
}
$.ajax({
url: 'https://circleci.com/api/v1/me',
xhrFields: {
withCredentials: true,
},
dataType: 'json',
timeout: 10000, // 10 seconds
})
.done(function (userData) {
setLoggedIn(userData);
setAmplitudeId(); // set Amplitude required data
})
.fail(function () {
setLoggedOut();
});
}
$(fetchUserData);
export function setAmplitudeId() {
const DAYS_PER_MINUTE = 1 / 24 / 60;
const sessionId = window.AnalyticsClient.getSessionId();
Cookies.set('amplitude-session-id', sessionId, {
expires: 30 * DAYS_PER_MINUTE,
});
window.AnalyticsClient.trackUser(userData.analytics_id);
}