This repository has been archived by the owner on Jun 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.js
104 lines (94 loc) · 2.53 KB
/
stats.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const https = require('https')
const { acceptedLanguages } = require('@fluent/langneg')
function trackPageview(payload, headers) {
const data = JSON.stringify({
payload: {
website: '', // 'your-website-id',
url: '/',
referrer: '',
hostname: '', // 'your-hostname',
language: '', // 'en-US',
screen: '', // '1920x1080',
...payload,
},
type: 'pageview'
})
const req = https.request({
hostname: 'umami.volt.link',
port: 443,
path: '/api/send',
method: 'POST',
headers: {
'User-Agent': headers['user-agent'] || '',
'Accept-Language': headers['accept-language'] || '',
'Content-Type': 'application/json',
'Content-Length': data.length,
}
}, null)
req.on('error', error => console.error(error))
req.write(data)
req.end()
}
function trackEvent(payload, headers) {
const data = JSON.stringify({
payload: {
website: '', // 'your-website-id',
url: '/',
name: 'custom', // 'click',
// event_value: '', // 'signup-button',
hostname: '', // 'your-hostname',
language: '', // 'en-US',
screen: '1920x1080',
...payload,
},
type: 'event'
})
const req = https.request({
hostname: 'umami.volt.link',
port: 443,
path: '/api/send',
method: 'POST',
headers: {
'User-Agent': headers['user-agent'] || '',
'Accept-Language': headers['accept-language'] || '',
'Content-Type': 'application/json',
'Content-Length': data.length,
}
}, null)
req.on('error', error => console.error(error))
req.write(data)
req.end()
}
function sendInitialStats({
website = '',
url = '/',
hostname = ''
}, headers = {}) {
const userLocales = acceptedLanguages(headers['accept-language'] || '')
if (!!userLocales || Array.isArray(userLocales)) {
const defaultPayload = {
website,
url,
hostname,
language: userLocales.length > 0 ? userLocales[0] : '',
}
trackPageview(defaultPayload, headers)
for (let locale of userLocales) {
locale = locale.toLowerCase() // Not really correct but the system locales sadly don't conform to the standard.
const language = locale.split('-')[0]
if (language !== locale) {
trackEvent({
...defaultPayload,
event_value: `L: ${language}`, // Log just the language.
}, headers)
}
trackEvent({
...defaultPayload,
event_value: `L: ${locale}`, // Log the full locale.
}, headers)
}
}
}
module.exports = {
sendInitialStats,
}