-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatomoRealtimeWiget.js
168 lines (129 loc) · 5.57 KB
/
MatomoRealtimeWiget.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
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
// Const for Matomo-API
const matomoUrl = "https://analytics.example.com"; // change it to the Matomo URL (analytic.example.com)
const tokenAuth = "API_KEY"; // change it -> API KEY (https://matomo.org/faq/general/faq_114/)
const siteId = args.widgetParameter || "1"; // Standard-Site-ID
// Realtime function
async function fetchRealTimeVisitors() {
const url = `${matomoUrl}?module=API&method=Live.getLastVisitsDetails&idSite=${siteId}&period=day&date=today&format=JSON&token_auth=${tokenAuth}`;
try {
const response = await new Request(url).loadJSON();
return response.slice(0, 10); // Nehmen Sie die letzten 10 Besuche
} catch (error) {
console.error(`Fehler beim Abrufen der Echtzeit-Besucher: ${error}`);
return []; // Leer, wenn ein Fehler auftritt
}
}
// create Widget
async function createWidget() {
let widget = new ListWidget();
widget.url = matomoUrl;
widget.backgroundColor = new Color("#000000");
const realTimeVisitors = await fetchRealTimeVisitors();
// Titke
const title = widget.addText("👤 Echtzeit-Besucher");
title.font = Font.boldSystemFont(14);
title.textColor = new Color("#ffffff");
widget.addSpacer(4);
// Add realtime users
realTimeVisitors.forEach(visitor => {
const visitorText = `${visitor.lastActionDateTime.split(' ')[1].substring(0, 5)} - ${visitor.country} - ${visitor.actionDetails.length} 👁️🗨️`;
const textElement = widget.addText(visitorText);
textElement.font = Font.systemFont(10);
textElement.textColor = new Color("#ffffff");
});
return widget;
}
async function createLargeWidget(realTimeVisitors) {
let widget = new ListWidget();
widget.url = matomoUrl;
widget.addSpacer(4);
// Titel
let title = widget.addText("👤 Real-Time Visitors");
title.font = Font.boldSystemFont(16);
title.textColor = Color.blue();
widget.addSpacer(5);
// Add realtime users
for (let i = 0; i < realTimeVisitors.length && i < 8; i++) {
const visitor = realTimeVisitors[i];
const time = visitor.lastActionDateTime.split(' ')[1].substring(0, 5); // Nur Uhrzeit
const visitorText = `${time} | ${visitor.country} | Actions: ${visitor.actions}`;
const deviceText = `Device: ${visitor.deviceType} | OS: ${visitor.operatingSystem} | Browser: ${visitor.browser}`;
const ipText = `IP: ${visitor.visitIp}`;
let visitorInfo = widget.addText(visitorText);
visitorInfo.font = Font.systemFont(12);
visitorInfo.textColor = Color.darkGray();
let deviceInfo = widget.addText(deviceText);
deviceInfo.font = Font.systemFont(10);
deviceInfo.textColor = Color.gray();
let ipInfo = widget.addText(ipText);
ipInfo.font = Font.systemFont(10);
ipInfo.textColor = Color.gray();
widget.addSpacer(3);
}
return widget;
}
async function createMediumWidget(realTimeVisitors) {
let widget = new ListWidget();
widget.url = matomoUrl;
widget.addSpacer(4);
// Titel
let title = widget.addText("👤 Real-Time Visitors");
title.font = Font.boldSystemFont(16);
title.textColor = Color.blue();
widget.addSpacer(5);
// Add realtime users
for (let i = 0; i < realTimeVisitors.length && i < 3; i++) {
const visitor = realTimeVisitors[i];
const time = visitor.lastActionDateTime.split(' ')[1].substring(0, 5);
const visitorText = `${time} | ${visitor.country} | Actions: ${visitor.actions}`;
const deviceText = `Device: ${visitor.deviceType} | OS: ${visitor.operatingSystem} | Browser: ${visitor.browser}`;
const ipText = `IP: ${visitor.visitIp}`;
let visitorInfo = widget.addText(visitorText);
visitorInfo.font = Font.systemFont(12);
visitorInfo.textColor = Color.darkGray();
let deviceInfo = widget.addText(deviceText);
deviceInfo.font = Font.systemFont(10);
deviceInfo.textColor = Color.gray();
let ipInfo = widget.addText(ipText);
ipInfo.font = Font.systemFont(10);
ipInfo.textColor = Color.gray();
widget.addSpacer(3);
}
return widget;
}
async function createSmallWidget(realTimeVisitors) {
let widget = new ListWidget();
widget.url = matomoUrl;
widget.addSpacer(4);
// Titel
let title = widget.addText("👤 Real-Time Visitors");
title.font = Font.boldSystemFont(16);
title.textColor = Color.blue();
widget.addSpacer(5);
// Add realtime users
for (let i = 0; i < realTimeVisitors.length && i < 5; i++) {
const visitor = realTimeVisitors[i];
const time = visitor.lastActionDateTime.split(' ')[1].substring(0, 5);
const visitorText = `${time} ${visitor.country} 👁️🗨️ ${visitor.actions}`;
let visitorInfo = widget.addText(visitorText);
visitorInfo.font = Font.systemFont(12);
visitorInfo.textColor = Color.darkGray();
widget.addSpacer(3);
}
return widget;
}
// Run script, show widget
async function run() {
let realTimeVisitors = await fetchRealTimeVisitors();
let widget;
if (config.widgetFamily === 'small') {
widget = await createSmallWidget(realTimeVisitors);
} else if (config.widgetFamily === 'medium') {
widget = await createMediumWidget(realTimeVisitors);
} else {
widget = await createLargeWidget(realTimeVisitors);
}
Script.setWidget(widget);
Script.complete();
}
run();