-
Notifications
You must be signed in to change notification settings - Fork 373
/
Copy pathmetrics.ts
118 lines (102 loc) · 3.69 KB
/
metrics.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
import Countly from 'countly-sdk-web'
const metricsConsent = localStorage.getItem('metrics_consent')
const banner = document.querySelector('.js-metrics-notification')
const declineWarning = document.querySelector('.js-metrics-notification-decline-warning')
const acceptButton = document.querySelector('.js-metrics-notification-accept')
const declineButton = document.querySelector('.js-metrics-notification-decline')
const declineWarningClose = document.querySelector('.js-metrics-notification-warning-close')
const bannerToggle = document.querySelector('.js-cookie-banner-toggle')
function addConsent (consent: string[]): void {
hideConsentBanner()
Countly.add_consent(consent)
if (Array.isArray(consent)) {
localStorage.setItem('metrics_consent', JSON.stringify(consent))
} else {
localStorage.setItem('metrics_consent', JSON.stringify([consent]))
}
}
function addConsentEventHandler (): void {
acceptButton?.removeEventListener('click', addConsentEventHandler)
addConsent(['all'])
}
function declineConsentEventHandler (): void {
addConsent(['necessary'])
hideConsentBanner()
displayDeclineWarning()
}
function displayDeclineWarning (): void {
declineWarning?.classList.remove('hidden')
bannerToggle?.setAttribute('disabled', '')
}
function declineWarningCloseEventHandler (): void {
declineWarningClose?.removeEventListener('click', declineWarningCloseEventHandler)
declineWarning?.classList.add('hidden')
bannerToggle?.removeAttribute('disabled')
}
function hideConsentBanner (): void {
acceptButton?.removeEventListener('click', addConsentEventHandler)
declineButton?.removeEventListener('click', declineConsentEventHandler)
banner?.classList.add('hidden')
bannerToggle?.removeAttribute('disabled')
}
/**
* Display the consent banner and handle the user's choice
*/
function displayConsentBanner (): void {
acceptButton?.addEventListener('click', addConsentEventHandler)
declineButton?.addEventListener('click', declineConsentEventHandler)
declineWarningClose?.addEventListener('click', declineWarningCloseEventHandler)
banner?.classList.remove('hidden')
bannerToggle?.setAttribute('disabled', '')
declineWarning?.classList.add('hidden')
}
function bannerToggleEventHandler (): void {
displayConsentBanner()
}
function loadCountly (): void {
bannerToggle?.addEventListener('click', bannerToggleEventHandler)
Countly.init({
app_key: '3c2c0819434074fc4d339ddd8e112a1e741ecb72',
url: 'https://countly.ipfs.io',
require_consent: true // this true means consent is required
})
/**
* @see https://support.count.ly/hc/en-us/articles/360037441932-Web-analytics-JavaScript-#features-for-consent
*/
const necessaryFeatures = ['sessions', 'views']
const marketingFeatures = ['attribution', 'users', 'location']
const performanceFeatures = ['events', 'crashes', 'apm']
const trackingFeatures = ['scrolls', 'clicks', 'forms', 'star-rating', 'feedback']
Countly.group_features({
all: [...necessaryFeatures, ...marketingFeatures, ...performanceFeatures, ...trackingFeatures],
necessary: necessaryFeatures,
marketing: marketingFeatures,
tracking: trackingFeatures,
performance: performanceFeatures
})
/**
* we can call all the helper methods we want, they won't record until consent is provided for specific features
*/
//
Countly.track_clicks()
Countly.track_errors()
Countly.track_forms()
Countly.track_links()
Countly.track_pageview()
Countly.track_scrolls()
Countly.track_sessions()
Countly.track_view()
if (metricsConsent != null) {
try {
addConsent(JSON.parse(metricsConsent))
} catch {
displayConsentBanner()
}
} else {
displayConsentBanner()
}
}
export {
loadCountly,
Countly
}