-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.js
More file actions
81 lines (67 loc) · 2.63 KB
/
analytics.js
File metadata and controls
81 lines (67 loc) · 2.63 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
// Analytics wrapper for privacy-respecting event tracking
const Analytics = {
// Check if analytics should be active
isEnabled: function() {
const dnt = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
const doNotTrack = dnt == "1" || dnt == "yes";
return !doNotTrack && typeof gtag !== 'undefined';
},
// Track page views
trackPageView: function(pageName) {
if (!this.isEnabled()) return;
gtag('event', 'page_view', {
page_title: pageName,
page_location: window.location.href,
page_path: window.location.pathname
});
},
// Track general events without sensitive data
trackEvent: function(category, action, label = null, value = null) {
if (!this.isEnabled()) return;
const eventParams = {
event_category: category,
event_label: label
};
if (value !== null) {
eventParams.value = value;
}
gtag('event', action, eventParams);
},
// Specific event tracking methods
trackValidatorAction: function(action, count = null) {
// Never send validator IDs or addresses
this.trackEvent('validators', action, null, count);
},
trackDutyCheck: function(dutyType) {
this.trackEvent('duties', 'check', dutyType);
},
trackNotificationSetup: function(type, enabled) {
this.trackEvent('notifications', 'setup', type, enabled ? 1 : 0);
},
trackBeaconNodeChange: function(isPublic) {
this.trackEvent('settings', 'beacon_node_change', isPublic ? 'public' : 'custom');
},
trackDashboardMode: function(action) {
this.trackEvent('interface', 'dashboard_mode', action);
},
trackError: function(errorType, errorMessage) {
// Sanitize error messages to remove any potential validator data
const sanitizedMessage = errorMessage.replace(/0x[a-fA-F0-9]+/g, '[REDACTED]')
.replace(/\d{4,}/g, '[INDEX]');
this.trackEvent('errors', errorType, sanitizedMessage);
}
};
// Initialize analytics on page load
document.addEventListener('DOMContentLoaded', function() {
if (Analytics.isEnabled()) {
// Track initial page view
Analytics.trackPageView('Home');
// Track navigation between pages
document.querySelectorAll('.nav-btn').forEach(btn => {
btn.addEventListener('click', function() {
const page = this.getAttribute('data-page');
Analytics.trackPageView(page);
});
});
}
});