forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
105 lines (87 loc) · 2.34 KB
/
index.ts
File metadata and controls
105 lines (87 loc) · 2.34 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
99
100
101
102
103
104
105
import VERSION from '../../version';
import * as amplitude from './amplitude';
import * as google from './google';
import * as sentry from './sentry';
import {
ANONYMOUS_UID_KEY,
DO_NOT_TRACK_ENABLED,
getHashedUserId,
isAllowedEvent,
} from './utils';
import * as vero from './vero';
if (process.env.NODE_ENV === 'production') {
setTimeout(() => {
identify('[Amplitude] Version', VERSION);
}, 5000);
}
export { getHashedUserId };
export const initializeSentry = sentry.initialize;
export const DNT = DO_NOT_TRACK_ENABLED;
export async function logError(err: Error) {
sentry.captureException(err);
if (window.console && console.error) console.error(err);
}
// Used to configure stuff?
export async function identify(key: string, value: any) {
if (!DO_NOT_TRACK_ENABLED) {
amplitude.identify(key, value);
sentry.configureScope(scope => {
scope.setExtra(key, value);
});
}
}
export async function setAnonymousId() {
if (!DO_NOT_TRACK_ENABLED) {
let anonymousUid = localStorage.getItem(ANONYMOUS_UID_KEY);
if (!anonymousUid) {
anonymousUid = String(
Math.random()
.toString(36)
.substring(2)
);
localStorage.setItem(ANONYMOUS_UID_KEY, anonymousUid);
}
vero.setAnonymousUserId(anonymousUid);
}
}
export async function setUserId(userId: string) {
if (!DO_NOT_TRACK_ENABLED) {
const hashedId = getHashedUserId(userId);
amplitude.setUserId(hashedId);
sentry.setUserId(hashedId);
vero.setUserId(hashedId);
}
}
export async function resetUserId() {
if (!DO_NOT_TRACK_ENABLED) {
amplitude.resetUserId();
sentry.resetUserId();
}
}
export function trackPageview() {
if (!DO_NOT_TRACK_ENABLED) {
const data = {
version: VERSION,
path: location.pathname + location.search,
};
amplitude.track('pageview', data);
vero.trackPageview();
google.trackPageView();
}
}
export default function track(eventName, secondArg: Object = {}) {
if (!DO_NOT_TRACK_ENABLED && isAllowedEvent(eventName, secondArg)) {
const data = {
...secondArg,
version: VERSION,
path: location.pathname + location.search,
};
amplitude.track(eventName, data);
google.track(eventName, data);
vero.track(eventName, data);
sentry.logBreadcrumb({
type: 'analytics',
message: eventName,
});
}
}