forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvero.ts
More file actions
88 lines (75 loc) · 1.68 KB
/
vero.ts
File metadata and controls
88 lines (75 loc) · 1.68 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
import { global } from './utils';
let _script;
const _veroq: any[] = [
['init', { api_key: '348b8acbc93adcf7c3d647d2abb4f4c22fe880e9' }],
];
global.veroq = _veroq;
/**
* For some reason vero doesn't call it, so we do it for them.
*/
function processArray() {
if (
typeof global.__vero !== 'undefined' &&
typeof global.__vero.processVeroArray === 'function'
) {
global.__vero.processVeroArray(_veroq);
}
}
export const trackPageview = () => {
_veroq.push(['trackPageview']);
processArray();
};
// Already start tracking the first pageview
trackPageview();
function loadScript() {
return new Promise((resolve, reject) => {
const script = document.createElement('script');
script.async = true;
script.onload = resolve;
script.onerror = () => {
reject(new Error('Could not load script'));
};
script.src = '//d3qxef4rp70elm.cloudfront.net/m.js';
document.body.appendChild(script);
})
.then(() => {
processArray();
})
.catch(() => {
/* ignore */
});
}
let _hasSetAnonymousUserId = false;
export const setAnonymousUserId = (userId: string) => {
if (!_script) {
_script = loadScript();
}
_hasSetAnonymousUserId = true;
_veroq.push([
'user',
{
id: userId,
},
]);
processArray();
};
export const setUserId = (userId: string) => {
if (!_script) {
_script = loadScript();
}
if (_hasSetAnonymousUserId) {
_veroq.push([
'user',
{
id: userId,
},
]);
} else {
_veroq.push(['reidentify', userId]);
}
processArray();
};
export const track = (eventName: string, data: unknown) => {
_veroq.push(['track', eventName, data]);
processArray();
};