-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats-dashboard.html
More file actions
571 lines (527 loc) · 27.9 KB
/
Copy pathstats-dashboard.html
File metadata and controls
571 lines (527 loc) · 27.9 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
<!doctype html>
<!--
═══════════════════════════════════════════════════════════════
REUSABLE STATS DASHBOARD SHELL
═══════════════════════════════════════════════════════════════
Drop-in analytics dashboard with KPIs, Smart Insights, date range
picker, export, and light/dark theme. All CSS + JS inline.
WHAT TO CUSTOMIZE (search for 🔧 PLACEHOLDER):
1. API endpoint — where the dashboard fetches its data
2. Branding — logo, name, subtitle
3. KPI definitions — what metrics to show
4. Color palette — CSS variables in :root
5. Auth / login — if your stats are behind a login
HOW IT WORKS:
- Calls your API endpoint with ?range=7d|30d|90d|all&scope=all|pages
- Expects JSON: { kpis:[{label,value,delta,deltaPct,deltaDir,sub}], insights:[...] }
- Renders KPI cards + Smart Insight cards
- Date range buttons filter the view
- Export to CSV / JSON
- Theme toggle persists in localStorage
ORIGIN: Extracted from stats.jeantobin.com (Eternal Harmony, 2026)
LICENSE: Free to reuse — built by Eternal Harmony AI
═══════════════════════════════════════════════════════════════
-->
<html lang="en" data-theme="dark">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="robots" content="noindex,nofollow,noarchive">
<title>🔧 Stats Dashboard — 🔧 Your Site Name</title>
<!-- 🔧 PLACEHOLDER: Replace Google Fonts with your own if desired -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Cormorant+Garamond:wght@400;500;600&family=Montserrat:wght@400;500;600&display=swap">
<style>
/* ═══════════════════════════════════════════════════════════
CSS VARIABLES — 🔧 PLACEHOLDER: Customize your brand colors
═══════════════════════════════════════════════════════════ */
:root {
--color-cream:#FAF8F5; --color-warm-white:#FFFFFF; --color-bg-alt:#F4F1EC;
--color-teal:#2A9D8F; --color-teal-dark:#238278; --color-teal-deep:#1B7A70;
--color-gold:#F2B731; --color-gold-hover:#E0A620; --color-gold-light:#F7D070;
--color-navy:#2C3E50; --color-navy-light:#3D5166;
--color-text:#2A2A2A; --color-text-light:#5A5A5A; --color-text-muted:#888888;
--color-border:#E8E4DE; --color-rose:#c47a7a;
--font-serif:'Cormorant Garamond','Georgia',serif;
--font-sans:'Montserrat','Helvetica Neue',Arial,sans-serif;
--shadow:0 4px 20px rgba(31,42,38,.06), 0 1px 3px rgba(31,42,38,.04);
--shadow-lg:0 12px 48px rgba(31,42,38,.10), 0 2px 6px rgba(31,42,38,.04);
--max-w:1200px;
}
/* Dark theme overrides */
[data-theme="dark"] {
--color-cream:#0E1B1A; --color-warm-white:#142625; --color-bg-alt:#0A1716;
--color-text:#ECE7DC; --color-text-light:#C5CFCC; --color-text-muted:#8E9C99;
--color-border:#1F3431;
--color-teal-deep:#6FE0D0; --color-teal-dark:#4DC9B8; --color-teal:#58D6C4;
--color-navy:#ECE7DC; --color-navy-light:#C5CFCC; --color-rose:#ff9b9b;
--shadow:0 4px 20px rgba(0,0,0,.4), 0 1px 3px rgba(0,0,0,.25);
--shadow-lg:0 12px 48px rgba(0,0,0,.55), 0 2px 6px rgba(0,0,0,.3);
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body {
min-height: 100vh; padding: 0 0 60px;
font-family: var(--font-sans); font-size: 15px; line-height: 1.6;
color: var(--color-text); background: var(--color-cream);
-webkit-font-smoothing: antialiased;
transition: background-color .4s ease, color .4s ease;
}
/* ═══════════════════════════════════════════════════════════
HEADER — 🔧 PLACEHOLDER: Replace logo + name
═══════════════════════════════════════════════════════════ */
header {
padding: 22px 28px; background: linear-gradient(180deg, var(--color-warm-white), var(--color-cream));
border-bottom: 1px solid var(--color-border);
display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
position: relative; z-index: 1;
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand .logo { width: 34px; height: auto; opacity: 0.95; }
/* 🔧 PLACEHOLDER: Replace logo filter for your brand color */
.brand .logo { filter: brightness(0) saturate(100%) invert(38%) sepia(85%) saturate(380%) hue-rotate(135deg) brightness(95%) contrast(90%); }
[data-theme="dark"] .brand .logo { filter: brightness(0) saturate(100%) invert(85%) sepia(40%) saturate(400%) hue-rotate(125deg) brightness(85%); }
.brand h1 {
margin: 0; font-family: var(--font-serif); font-weight: 500;
font-size: 26px; letter-spacing: .3px; color: var(--color-teal-deep);
}
.brand .sub { color: var(--color-text-light); font-size: 13px; font-style: italic; margin-top: 2px; }
.header-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.updated { font-size: 12px; color: var(--color-text-muted); margin-right: 6px; }
/* Buttons */
.icon-btn, .pill-btn {
background: var(--color-warm-white); border: 1px solid var(--color-border);
color: var(--color-text-light); cursor: pointer;
transition: all .2s; font-family: var(--font-sans);
}
.pill-btn {
padding: 8px 14px; border-radius: 20px; font-size: 12px; font-weight: 500;
letter-spacing: .3px; display: inline-flex; align-items: center; gap: 6px;
}
.pill-btn:hover { color: var(--color-teal-deep); border-color: var(--color-teal-deep); }
.pill-btn.danger:hover { color: var(--color-rose); border-color: var(--color-rose); }
.pill-btn.primary {
background: var(--color-teal-deep); color: var(--color-warm-white); border-color: var(--color-teal-deep);
}
.pill-btn.primary:hover { background: var(--color-teal-dark); border-color: var(--color-teal-dark); }
[data-theme="dark"] .pill-btn.primary { color: #0E1B1A; }
[data-theme="dark"] .pill-btn.primary:hover { color: #0E1B1A; }
/* Theme toggle */
.theme-toggle {
width: 44px; height: 44px; border-radius: 50%;
background: var(--color-warm-white); border: 1px solid var(--color-gold);
cursor: pointer; display: flex; align-items: center; justify-content: center;
color: var(--color-gold); position: relative;
padding: 0; outline: none;
transition: border-color 0.3s ease, box-shadow 0.4s ease, transform 0.3s ease;
box-shadow: 0 0 0 1px rgba(242, 183, 49, 0.20), 0 0 14px rgba(242, 183, 49, 0.32);
}
.theme-toggle:hover { transform: rotate(-12deg); }
.theme-toggle svg { width: 22px; height: 22px; transition: opacity 0.35s ease, transform 0.5s ease; position: absolute; }
.theme-toggle .icon-sun { opacity: 1; transform: scale(1) rotate(0deg); }
.theme-toggle .icon-moon { opacity: 0; transform: scale(0.5) rotate(-90deg); }
[data-theme="dark"] .theme-toggle .icon-sun { opacity: 0; transform: scale(0.5) rotate(90deg); }
[data-theme="dark"] .theme-toggle .icon-moon { opacity: 1; transform: scale(1) rotate(0deg); }
[data-theme="dark"] .theme-toggle {
border-color: var(--color-teal-deep); color: var(--color-teal-deep);
box-shadow: 0 0 0 1px rgba(111, 224, 208, 0.25), 0 0 16px rgba(111, 224, 208, 0.40);
}
/* ═══════════════════════════════════════════════════════════
TOOLBAR — Date range picker + Export
═══════════════════════════════════════════════════════════ */
.toolbar {
max-width: var(--max-w); margin: 24px auto 0; padding: 0 28px;
display: flex; align-items: center; justify-content: space-between;
gap: 16px; flex-wrap: wrap;
}
.range {
display: inline-flex; background: var(--color-warm-white); border: 1px solid var(--color-border);
border-radius: 10px; padding: 4px; gap: 2px; box-shadow: var(--shadow);
}
.range button {
background: transparent; border: none; cursor: pointer;
padding: 8px 14px; font-size: 12px; font-weight: 500; letter-spacing: .3px;
color: var(--color-text-light); border-radius: 7px;
font-family: var(--font-sans); transition: all .2s;
}
.range button:hover { color: var(--color-teal-deep); }
.range button.active {
background: var(--color-teal-deep); color: var(--color-warm-white);
}
[data-theme="dark"] .range button.active { color: #0E1B1A; }
.scope-btn {
background: transparent; border: none; cursor: pointer;
padding: 6px 12px; font-size: 11px; font-weight: 500; text-transform: uppercase; letter-spacing: 1px;
color: var(--color-text-light); border-radius: 6px;
font-family: var(--font-sans); transition: all .2s;
}
.scope-btn:hover { color: var(--color-teal-deep); }
.scope-btn.active { background: var(--color-teal-deep); color: var(--color-warm-white); }
[data-theme="dark"] .scope-btn.active { color: #0E1B1A; }
.toolbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
/* Export menu */
.menu-wrap { position: relative; }
.menu {
position: absolute; top: calc(100% + 6px); right: 0; z-index: 50;
background: var(--color-warm-white); border: 1px solid var(--color-border);
border-radius: 10px; box-shadow: var(--shadow-lg); padding: 6px;
min-width: 220px; opacity: 0; pointer-events: none; transform: translateY(-4px);
transition: opacity .15s, transform .15s;
}
.menu.open { opacity: 1; pointer-events: auto; transform: translateY(0); }
.menu button {
display: flex; align-items: center; gap: 10px; width: 100%; text-align: left;
background: transparent; border: none; cursor: pointer; padding: 10px 12px;
font-size: 13px; color: var(--color-text); border-radius: 6px;
font-family: var(--font-sans); transition: background .15s;
}
.menu button:hover { background: var(--color-bg-alt); color: var(--color-teal-deep); }
.menu .ext { color: var(--color-text-muted); font-size: 11px; margin-left: auto; }
/* Toast notification */
.toast {
position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(100px);
background: var(--color-teal-deep); color: var(--color-warm-white);
padding: 12px 22px; border-radius: 24px; font-size: 13px; font-weight: 500;
box-shadow: var(--shadow-lg); z-index: 200; opacity: 0;
transition: transform .3s ease, opacity .3s ease;
}
[data-theme="dark"] .toast { color: #0E1B1A; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
/* ═══════════════════════════════════════════════════════════
MAIN CONTENT — KPIs + Smart Insights
═══════════════════════════════════════════════════════════ */
main { padding: 24px 28px; max-width: var(--max-w); margin: 0 auto; position: relative; z-index: 1; }
/* === SMART INSIGHTS CARDS === */
.insights {
display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 14px; margin-bottom: 32px;
}
.insight {
background: var(--color-warm-white); padding: 18px 20px; border-radius: 12px;
box-shadow: var(--shadow), 0 0 0 1px rgba(42,157,143,0.04);
border: 1px solid var(--color-border); border-left: 4px solid var(--color-gold);
display: flex; gap: 14px; align-items: flex-start;
transition: transform 0.3s ease, box-shadow 0.35s ease, border-color 0.3s ease;
}
.insight:hover {
transform: translateY(-3px); border-color: var(--color-teal); border-left-color: var(--color-teal);
box-shadow: 0 0 0 1px rgba(42,157,143,0.18), 0 12px 50px -8px rgba(42,157,143,0.30), 0 4px 18px rgba(31,42,38,0.10);
}
[data-theme="dark"] .insight:hover {
border-color: var(--color-teal-deep); border-left-color: var(--color-teal-deep);
box-shadow: 0 0 0 1px rgba(111,224,208,0.30), 0 0 60px -10px rgba(111,224,208,0.40), 0 4px 22px rgba(0,0,0,0.45);
}
.insight.trend { border-left-color: var(--color-teal); }
.insight.milestone { border-left-color: var(--color-gold); }
.insight.highlight { border-left-color: var(--color-teal-deep); }
.insight .emoji { font-size: 26px; flex-shrink: 0; line-height: 1; padding-top: 2px; }
.insight h3 { margin: 0 0 4px; font-size: 15px; font-weight: 600; color: var(--color-text); font-family: var(--font-sans); }
.insight p { margin: 0; font-size: 13px; color: var(--color-text-light); line-height: 1.5; }
.insights-empty {
grid-column: 1 / -1; text-align: center; padding: 32px;
color: var(--color-text-muted); font-style: italic;
}
/* === KPI CARDS === */
.kpis {
display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 14px; margin-bottom: 32px;
}
.kpi {
background: var(--color-warm-white); padding: 20px; border-radius: 12px;
box-shadow: var(--shadow), 0 0 0 1px rgba(42,157,143,0.04);
border: 1px solid var(--color-border); border-top: 3px solid transparent;
transition: transform .25s ease, border-color .3s ease, box-shadow .35s ease;
}
.kpi:hover {
transform: translateY(-3px);
border-color: var(--color-teal); border-top-color: var(--color-gold);
box-shadow: 0 0 0 1px rgba(42,157,143,0.18), 0 12px 50px -8px rgba(42,157,143,0.35), 0 4px 18px rgba(31,42,38,0.10);
}
[data-theme="dark"] .kpi:hover {
border-color: var(--color-teal-deep); border-top-color: var(--color-teal-deep);
box-shadow: 0 0 0 1px rgba(111,224,208,0.30), 0 0 60px -10px rgba(111,224,208,0.40), 0 4px 22px rgba(0,0,0,0.45);
}
.kpi .label {
font-size: 11px; text-transform: uppercase; letter-spacing: 1.2px;
color: var(--color-text-light); margin: 0 0 8px; font-weight: 600;
}
.kpi .value {
font-size: 32px; font-weight: 500; color: var(--color-teal-deep);
font-family: var(--font-serif); line-height: 1;
}
.kpi .delta { display: inline-block; font-size: 11px; margin-top: 8px; padding: 2px 9px; border-radius: 10px; font-weight: 600; }
.kpi .delta.up { color: var(--color-teal-deep); background: rgba(42,157,143,.12); }
.kpi .delta.down { color: var(--color-rose); background: rgba(196,122,122,.12); }
.kpi .delta.flat { color: var(--color-text-muted); background: var(--color-bg-alt); }
.kpi .sub { font-size: 11px; color: var(--color-text-muted); margin-top: 6px; }
.section-title {
font-family: var(--font-serif); font-size: 22px; font-weight: 500;
margin: 0 0 16px; color: var(--color-teal-deep);
display: flex; align-items: baseline; gap: 12px;
}
.section-title::after { content: ''; flex: 1; height: 1px; background: var(--color-border); }
/* Loading / Error states */
.loading-state, .error-state {
text-align: center; padding: 60px 20px; color: var(--color-text-muted);
font-size: 14px;
}
.error-state { color: var(--color-rose); }
/* Responsive */
@media (max-width: 640px) {
header { padding: 16px; }
.brand h1 { font-size: 20px; }
.toolbar { padding: 0 16px; }
main { padding: 16px; }
.kpis { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
}
</style>
</head>
<body>
<!-- ═══════════════════════════════════════════════════════════
HEADER — 🔧 PLACEHOLDER: Replace logo src + brand name
═══════════════════════════════════════════════════════════ -->
<header>
<div class="brand">
<!-- 🔧 PLACEHOLDER: Replace with your logo (or remove <img> and use text-only) -->
<img class="logo" src="🔧 YOUR_LOGO.png" alt="🔧 Your Brand" width="34" height="34">
<div>
<h1>🔧 Your Site Name</h1>
<p class="sub">🔧 Stats Dashboard</p>
</div>
</div>
<div class="header-actions">
<span class="updated" id="updatedAt" aria-live="polite"></span>
<!-- Theme Toggle -->
<button type="button" class="theme-toggle" id="themeToggle" aria-label="Toggle light and dark mode" title="Toggle light / dark mode" aria-pressed="false">
<svg class="icon-sun" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<circle cx="12" cy="12" r="4.5"></circle>
<line x1="12" y1="2" x2="12" y2="4.5"></line><line x1="12" y1="19.5" x2="12" y2="22"></line>
<line x1="2" y1="12" x2="4.5" y2="12"></line><line x1="19.5" y1="12" x2="22" y2="12"></line>
<line x1="4.93" y1="4.93" x2="6.7" y2="6.7"></line><line x1="17.3" y1="17.3" x2="19.07" y2="19.07"></line>
<line x1="4.93" y1="19.07" x2="6.7" y2="17.3"></line><line x1="17.3" y1="6.7" x2="19.07" y2="4.93"></line>
</svg>
<svg class="icon-moon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="M21 12.8A9 9 0 1 1 11.2 3a7 7 0 0 0 9.8 9.8z"></path>
</svg>
</button>
</div>
</header>
<!-- ═══════════════════════════════════════════════════════════
TOOLBAR — Date Range + Scope + Export
═══════════════════════════════════════════════════════════ -->
<div class="toolbar">
<div class="range" id="rangeBar" role="group" aria-label="Date range">
<button data-range="7d" class="active">Week</button>
<button data-range="30d">Month</button>
<button data-range="90d">Quarter</button>
<button data-range="all">All Time</button>
</div>
<div class="toolbar-right">
<button class="scope-btn" id="scopeAll" aria-pressed="true">All Pages</button>
<!-- 🔧 PLACEHOLDER: Add more scope buttons if you track per-page stats -->
<div class="menu-wrap">
<button class="pill-btn" id="exportBtn">Export ▾</button>
<div class="menu" id="exportMenu">
<button data-format="csv" >Export as CSV <span class="ext">.csv</span></button>
<button data-format="json">Export as JSON <span class="ext">.json</span></button>
</div>
</div>
</div>
</div>
<!-- ═══════════════════════════════════════════════════════════
MAIN — KPIs + Smart Insights rendered by JS below
═══════════════════════════════════════════════════════════ -->
<main>
<section aria-labelledby="insights-title">
<h2 class="section-title" id="insights-title">Smart Insights</h2>
<div class="insights" id="insightsGrid" aria-live="polite"></div>
</section>
<section aria-labelledby="kpis-title">
<h2 class="section-title" id="kpis-title">Key Metrics</h2>
<div class="kpis" id="kpisGrid" aria-live="polite"></div>
</section>
</main>
<!-- Toast -->
<div class="toast" id="toast" role="status" aria-live="polite"></div>
<!-- ═══════════════════════════════════════════════════════════
JAVASCRIPT — Data fetching, rendering, theme, export
═══════════════════════════════════════════════════════════ -->
<script>
(function() {
'use strict';
/* ─── 🔧 PLACEHOLDER: Your API endpoint ─── */
var API_BASE = '🔧 https://your-api.example.com/stats';
/* ─── State ─── */
var state = {
range: '7d',
scope: 'all',
data: null // cached API response
};
/* ─── DOM refs ─── */
var $kpis = document.getElementById('kpisGrid');
var $insights = document.getElementById('insightsGrid');
var $updated = document.getElementById('updatedAt');
var $toast = document.getElementById('toast');
var $exportMenu = document.getElementById('exportMenu');
/* ─── Toast helper ─── */
function toast(msg) {
$toast.textContent = msg;
$toast.classList.add('show');
clearTimeout($toast._tid);
$toast._tid = setTimeout(function() { $toast.classList.remove('show'); }, 2200);
}
/* ═══════════════════════════════════════════════════════════
FETCH DATA
🔧 PLACEHOLDER: Modify fetchData() if your API needs auth
headers, different query params, or a different response shape.
Expected response shape:
{
kpis: [{ label, value, delta, deltaPct, deltaDir, sub }],
insights: [{ type, emoji, title, body }],
updated: "ISO timestamp"
}
═══════════════════════════════════════════════════════════ */
function fetchData() {
$kpis.innerHTML = '<div class="loading-state">Loading KPIs\u2026</div>';
$insights.innerHTML = '<div class="loading-state">Loading insights\u2026</div>';
var url = API_BASE + '?range=' + state.range + '&scope=' + state.scope;
/* 🔧 PLACEHOLDER: Add auth header if needed
var headers = { 'Authorization': 'Bearer YOUR_TOKEN' };
fetch(url, { headers: headers })
*/
fetch(url)
.then(function(r) {
if (!r.ok) throw new Error('HTTP ' + r.status);
return r.json();
})
.then(function(json) {
state.data = json;
renderKPIs(json.kpis || []);
renderInsights(json.insights || []);
if (json.updated) $updated.textContent = 'Updated ' + formatTime(json.updated);
})
.catch(function(err) {
$kpis.innerHTML = '<div class="error-state">Failed to load KPIs: ' + err.message + '</div>';
$insights.innerHTML = '<div class="error-state">Failed to load insights.</div>';
});
}
/* ─── Render KPIs ─── */
function renderKPIs(kpis) {
if (!kpis.length) { $kpis.innerHTML = '<div class="insights-empty">No data for this range.</div>'; return; }
$kpis.innerHTML = kpis.map(function(k) {
var deltaHtml = '';
if (k.delta != null) {
var cls = k.deltaDir === 'up' ? 'up' : k.deltaDir === 'down' ? 'down' : 'flat';
var arrow = k.deltaDir === 'up' ? '\u2191' : k.deltaDir === 'down' ? '\u2193' : '\u2194';
var pct = k.deltaPct != null ? ' (' + k.deltaPct + '%)' : '';
deltaHtml = '<span class="delta ' + cls + '">' + arrow + ' ' + k.delta + pct + '</span>';
}
var subHtml = k.sub ? '<div class="sub">' + k.sub + '</div>' : '';
return '<div class="kpi">' +
'<p class="label">' + esc(k.label) + '</p>' +
'<p class="value">' + esc(String(k.value)) + '</p>' +
deltaHtml + subHtml +
'</div>';
}).join('');
}
/* ─── Render Smart Insights ─── */
function renderInsights(insights) {
if (!insights.length) { $insights.innerHTML = '<div class="insights-empty">Nothing notable for this range.</div>'; return; }
$insights.innerHTML = insights.map(function(ins) {
var cls = ins.type || 'trend';
return '<article class="insight ' + cls + '">' +
'<span class="emoji" aria-hidden="true">' + (ins.emoji || '\u2728') + '</span>' +
'<div><h3>' + esc(ins.title) + '</h3><p>' + esc(ins.body) + '</p></div>' +
'</article>';
}).join('');
}
/* ─── Time formatting ─── */
function formatTime(iso) {
try {
var d = new Date(iso);
return d.toLocaleString('en-US', { month:'short', day:'numeric', hour:'numeric', minute:'2-digit' });
} catch(e) { return iso; }
}
/* ─── Escape HTML ─── */
function esc(s) {
return String(s).replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
}
/* ═══════════════════════════════════════════════════════════
EVENT HANDLERS
═══════════════════════════════════════════════════════════ */
/* Date range buttons */
document.getElementById('rangeBar').addEventListener('click', function(e) {
var btn = e.target.closest('button');
if (!btn || !btn.dataset.range) return;
state.range = btn.dataset.range;
this.querySelectorAll('button').forEach(function(b) { b.classList.remove('active'); });
btn.classList.add('active');
fetchData();
});
/* Scope toggle */
document.getElementById('scopeAll').addEventListener('click', function() {
var active = this.getAttribute('aria-pressed') === 'true';
state.scope = active ? 'pages' : 'all';
this.setAttribute('aria-pressed', String(!active));
this.textContent = active ? 'By Page' : 'All Pages';
this.classList.toggle('active');
fetchData();
});
/* Export menu */
document.getElementById('exportBtn').addEventListener('click', function(e) {
e.stopPropagation();
$exportMenu.classList.toggle('open');
});
document.addEventListener('click', function() { $exportMenu.classList.remove('open'); });
$exportMenu.addEventListener('click', function(e) {
var btn = e.target.closest('button');
if (!btn || !btn.dataset.format) return;
exportData(btn.dataset.format);
$exportMenu.classList.remove('open');
});
function exportData(format) {
if (!state.data) { toast('No data to export yet.'); return; }
var payload, mime, ext;
if (format === 'json') {
payload = JSON.stringify(state.data, null, 2);
mime = 'application/json';
ext = '.json';
} else {
/* CSV: flatten KPIs */
var rows = ['label,value,delta,deltaPct,deltaDir'];
(state.data.kpis || []).forEach(function(k) {
rows.push([k.label, k.value, k.delta || '', k.deltaPct || '', k.deltaDir || ''].join(','));
});
payload = rows.join('\n');
mime = 'text/csv';
ext = '.csv';
}
var blob = new Blob([payload], { type: mime });
var a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'stats-' + state.range + ext;
a.click();
toast('Exported as ' + format.toUpperCase());
}
/* Theme toggle */
(function() {
var html = document.documentElement;
var btn = document.getElementById('themeToggle');
/* Init from localStorage (default dark) */
try {
var saved = localStorage.getItem('stats-theme');
if (saved) html.setAttribute('data-theme', saved);
} catch(e) {}
btn.addEventListener('click', function() {
var current = html.getAttribute('data-theme');
var next = current === 'dark' ? 'light' : 'dark';
html.setAttribute('data-theme', next);
try { localStorage.setItem('stats-theme', next); } catch(e) {}
});
})();
/* ─── Initial load ─── */
fetchData();
})();
</script>
</body>
</html>