This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
89 lines (74 loc) · 2.58 KB
/
index.js
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
var constants = require('./constants');
var ClientJS = require('clientjs');
var axios = require('axios');
var client = new window.ClientJS();
var settings = {};
function isPrivateEnv() {
return window.location.host.includes('private');
}
function init(options) {
// Don't post events if the site is 'private'
if (isPrivateEnv()) return false;
window.clearInterval(settings.heartbeatInterval);
window.clearTimeout(settings.heartbeatTimeout);
settings.projectName = options.projectName;
settings.secondaryProject = options.secondaryProject || options.pageName || options.projectName;
settings.sentEvents = {};
axios.post(constants.ANALYTICS_URL + '/events', Object.assign(options, {
projectName: settings.projectName,
secondaryProject: settings.secondaryProject,
eventType: 'view',
browser: client.getBrowserData(),
screen: client.getScreenPrint(),
mobile: client.isMobile(),
fingerprint: client.getFingerprint(),
referrer: document.referrer,
version: 1
}));
settings.heartbeatCount = 0;
settings.heartbeatTimeout = window.setTimeout(function () {
heartbeat();
settings.heartbeatInterval = window.setInterval(function () {
heartbeat();
}, 30000);
}, 30000);
};
function error(data) {
// Don't post events if the site is 'private'
if (isPrivateEnv()) return false;
axios.post(constants.ANALYTICS_URL + '/events', Object.assign(data, {
projectName: settings.projectName,
secondaryProject: data.secondaryProject || settings.secondaryProject,
eventType: 'error',
version: 1,
browser: client.getBrowserData(),
}));
};
function heartbeat() {
// Don't post events if the site is 'private'
if (isPrivateEnv()) return false;
axios.post(constants.ANALYTICS_URL + '/events', {
projectName: settings.projectName,
secondaryProject: settings.secondaryProject,
eventType: 'heartbeat',
iteration: settings.heartbeatCount += 1,
version: 1
});
};
function count(data) {
// Don't post events if the site is 'private'
if (isPrivateEnv()) return false;
if (data.unique && settings.sentEvents[data.uniqueKey || data.eventName]) {
return;
}
settings.sentEvents[data.uniqueKey || data.eventName] = true;
axios.post(constants.ANALYTICS_URL + '/events', Object.assign(data, {
projectName: settings.projectName,
secondaryProject: settings.secondaryProject,
eventType: data.eventType || 'count',
eventName: data.eventName
})).catch(function () {
settings.sentEvents[data.uniqueKey || data.eventName] = false;
});
};
module.exports = { init: init, count: count, error: error };