-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathscript.js
More file actions
178 lines (153 loc) · 4.78 KB
/
Copy pathscript.js
File metadata and controls
178 lines (153 loc) · 4.78 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* globals JSINFO, DOKU_BASE, DokuCookie */
/**
* Modern Statistics Plugin
*/
class StatisticsPlugin {
constructor() {
this.data = {};
}
/**
* Initialize the statistics plugin
*/
async init() {
try {
this.buildTrackingData();
await this.logPageView();
this.attachEventListeners();
} catch (error) {
console.error('Statistics plugin initialization failed:', error);
}
}
/**
* Build tracking data object
*/
buildTrackingData() {
const now = Date.now();
const params = new URLSearchParams(window.location.search);
this.data = {
p: JSINFO.id,
r: document.referrer,
sx: screen.width,
sy: screen.height,
vx: window.innerWidth,
vy: window.innerHeight,
utm_source: params.get('utm_source') || '',
utm_medium: params.get('utm_medium') || '',
utm_campaign: params.get('utm_campaign') || '',
rnd: now
};
}
/**
* Log page view based on action
*/
async logPageView() {
const action = JSINFO.act === 'show' ? 'v' : 's';
await this.logView(action);
}
/**
* Attach event listeners for tracking
*/
attachEventListeners() {
// Track external link clicks
document.querySelectorAll('a.urlextern').forEach(link => {
link.addEventListener('click', this.logExternal.bind(this));
});
// Track page unload
window.addEventListener('beforeunload', this.logExit.bind(this));
}
/**
* Log a view or session
* @param {string} action 'v' = view, 's' = session
*/
async logView(action) {
const params = new URLSearchParams(this.data);
const url = `${DOKU_BASE}lib/plugins/statistics/dispatch.php?do=${action}&${params}`;
try {
// Use fetch with keepalive for better reliability
await fetch(url, {
method: 'GET',
keepalive: true,
cache: 'no-cache'
});
} catch (error) {
// Fallback to image beacon for older browsers
const img = new Image();
img.src = url;
}
}
/**
* Log clicks to external URLs
* @param {Event} event Click event
*/
logExternal(event) {
const params = new URLSearchParams(this.data);
const url = `${DOKU_BASE}lib/plugins/statistics/dispatch.php?do=o&ol=${encodeURIComponent(event.target.href)}&${params}`;
// Use sendBeacon for reliable tracking
if (navigator.sendBeacon) {
navigator.sendBeacon(url);
} else {
// Fallback for older browsers
const img = new Image();
img.src = url;
}
return true;
}
/**
* Log page exit as session info
*/
logExit() {
const params = new URLSearchParams(this.data);
const url = `${DOKU_BASE}lib/plugins/statistics/dispatch.php?do=s&${params}`;
if (navigator.sendBeacon) {
navigator.sendBeacon(url);
}
}
}
// Initialize when DOM is ready
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
new StatisticsPlugin().init();
});
} else {
// DOM already loaded
new StatisticsPlugin().init();
}
class ChartComponent extends HTMLElement {
connectedCallback() {
this.renderChart();
}
renderChart() {
const chartType = this.getAttribute('type');
const data = JSON.parse(this.getAttribute('data'));
console.log('data', data);
const canvas = document.createElement("canvas");
canvas.height = this.getAttribute('height') || 300;
canvas.width = this.getAttribute('width') || 300;
this.appendChild(canvas);
const ctx = canvas.getContext('2d');
// basic config
const config = {
type: chartType,
data: data,
options: {
responsive: false,
},
};
// percentage labels and tooltips for pie charts
if (chartType === "pie") {
// chartjs-plugin-datalabels needs to be registered
Chart.register(ChartDataLabels);
config.options.plugins = {
datalabels: {
formatter: (value, context) => {
const total = context.chart.data.datasets[0].data.reduce((a, b) => a + b, 0);
return ((value / total) * 100).toFixed(2) + '%'; // percentage
},
color: '#fff',
}
};
}
new Chart(ctx, config);
}
}
customElements.define('chart-component', ChartComponent);