-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
909 lines (803 loc) Β· 28 KB
/
app.py
File metadata and controls
909 lines (803 loc) Β· 28 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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
from flask import (
Flask, request, jsonify, render_template_string,
make_response, redirect, session, url_for
)
from flask_socketio import SocketIO
from flask_cors import CORS
import json, os, time, uuid
from datetime import datetime
import requests # for geo lookup
app = Flask(__name__)
app.secret_key = "CHANGE_ME_SECRET_KEY" # change this
ADMIN_PASSWORD = "admin123" # change this
PROFILE_FILE = "profiles.json"
# CORS (extra safety, even though pixel tracking doesn't need it)
CORS(app, resources={r"/*": {
"origins": "*",
"allow_headers": "*",
"methods": ["GET", "POST", "OPTIONS"],
}})
socketio = SocketIO(app, cors_allowed_origins="*")
# -------------------------
# BLOG DATA
# -------------------------
BLOGS = [
{
"slug": "top-5-tech-trends-2025",
"cat_slug": "tech",
"category": "Tech",
"title": "Top 5 Tech Trends You Must Watch in 2025",
"thumb": "https://images.pexels.com/photos/1181671/pexels-photo-1181671.jpeg",
"excerpt": "From AI copilots to quantum-safe security, these trends will reshape how we work, code, and stay secure."
},
{
"slug": "how-hackers-track-you-online",
"cat_slug": "cybersecurity",
"category": "Cybersecurity",
"title": "How Hackers Track You Online Using Pixels and Cookies",
"thumb": "https://images.pexels.com/photos/5380664/pexels-photo-5380664.jpeg",
"excerpt": "Tiny JavaScript and invisible images can follow your clicks, scrolls, and interests across websites."
},
{
"slug": "todays-breaking-digital-news",
"cat_slug": "news",
"category": "News",
"title": "Todayβs Breaking Digital News You Might Have Missed",
"thumb": "https://images.pexels.com/photos/3944454/pexels-photo-3944454.jpeg",
"excerpt": "A quick wrap-up of the latest security breaches, policy changes, and tech announcements."
},
{
"slug": "stock-market-and-tech-stocks",
"cat_slug": "stock",
"category": "Stock",
"title": "How Big Tech Stocks Drive the Market",
"thumb": "https://images.pexels.com/photos/669610/pexels-photo-669610.jpeg",
"excerpt": "Tech giants can move entire indices with a single earnings report. Hereβs what that means for small investors."
},
{
"slug": "instagram-algorithm-secrets",
"cat_slug": "instagram",
"category": "Instagram",
"title": "Instagram Algorithm Secrets: What It Really Likes",
"thumb": "https://images.pexels.com/photos/1092671/pexels-photo-1092671.jpeg",
"excerpt": "Reels, saves, and watch time matter more than likes. Learn the signals that actually move your content."
},
{
"slug": "simple-cyber-hygiene-checklist",
"cat_slug": "cybersecurity",
"category": "Cybersecurity",
"title": "Simple Cyber Hygiene Checklist for Everyday Users",
"thumb": "https://images.pexels.com/photos/5380595/pexels-photo-5380595.jpeg",
"excerpt": "A minimal set of habits that dramatically reduces your risk of being hacked."
},
]
def find_blog(cat_slug, slug):
for b in BLOGS:
if b["cat_slug"] == cat_slug and b["slug"] == slug:
return b
return None
# -------------------------
# FILE DB HELPERS
# -------------------------
def load_profiles():
if not os.path.exists(PROFILE_FILE):
return {}
with open(PROFILE_FILE, "r", encoding="utf-8") as f:
try:
return json.load(f)
except json.JSONDecodeError:
return {}
def save_profiles(data):
with open(PROFILE_FILE, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2)
# -------------------------
# GEO LOOKUP
# -------------------------
def get_geo(ip: str):
try:
r = requests.get(f"http://ip-api.com/json/{ip}", timeout=3)
data = r.json()
if data.get("status") == "success":
return {
"country": data.get("country", "Unknown"),
"state": data.get("regionName", "Unknown"),
"city": data.get("city", "Unknown"),
}
except Exception:
pass
return {"country": "Unknown", "state": "Unknown", "city": "Unknown"}
# -------------------------
# USER-AGENT PARSER
# -------------------------
def parse_user_agent(ua: str):
ua = (ua or "").lower()
device, os_name, browser = "Desktop", "Unknown", "Unknown"
if "android" in ua:
os_name, device = "Android", "Mobile"
if "iphone" in ua or "ios" in ua:
os_name, device = "iOS", "Mobile"
if "windows" in ua:
os_name = "Windows"
if "mac os" in ua or "macintosh" in ua:
os_name = "macOS"
if "linux" in ua:
os_name = "Linux"
if "edg" in ua:
browser = "Edge"
elif "chrome" in ua:
browser = "Chrome"
elif "firefox" in ua:
browser = "Firefox"
elif "safari" in ua and "chrome" not in ua:
browser = "Safari"
return {"device": device, "os": os_name, "browser": browser}
# -------------------------
# CORS HEADERS
# -------------------------
@app.after_request
def cors_headers(resp):
resp.headers["Access-Control-Allow-Origin"] = "*"
resp.headers["Access-Control-Allow-Headers"] = "*"
resp.headers["Access-Control-Allow-Methods"] = "GET,POST,OPTIONS"
return resp
# -------------------------
# TRACKER JS β PIXEL USING /track.gif
# -------------------------
TRACKER_JS = r"""
<script>
let tracker = {
clicks: 0,
scrollDepth: 0,
timeSpent: 0,
startTime: Date.now(),
page: window.location.pathname
};
document.addEventListener("click", () => { tracker.clicks++; });
document.addEventListener("scroll", () => {
const d = Math.round((window.scrollY + window.innerHeight) / document.body.scrollHeight * 100);
tracker.scrollDepth = Math.max(tracker.scrollDepth, d);
});
setInterval(() => {
tracker.timeSpent = Math.round((Date.now() - tracker.startTime) / 1000);
}, 1000);
// FingerprintJS (optional)
let fpVisitorId = null;
const fpScript = document.createElement("script");
fpScript.src = "https://cdn.jsdelivr.net/npm/@fingerprintjs/fingerprintjs@3/dist/fp.min.js";
fpScript.async = true;
fpScript.onload = () => {
FingerprintJS.load()
.then(fp => fp.get())
.then(result => {
fpVisitorId = result.visitorId;
})
.catch(() => {});
};
document.head.appendChild(fpScript);
function buildPayload() {
return {
clicks: tracker.clicks,
scrollDepth: tracker.scrollDepth,
timeSpent: tracker.timeSpent,
page: tracker.page,
screen: `${screen.width}x${screen.height}`,
userAgent: navigator.userAgent,
platform: navigator.platform,
language: navigator.language,
referrer: document.referrer,
url: location.href,
origin: location.origin,
fpVisitorId: fpVisitorId
};
}
function sendTracking() {
const payload = JSON.stringify(buildPayload());
const url = "{{ tracker_url }}" + "?data=" + encodeURIComponent(payload);
const img = new Image();
img.src = url;
console.log("Pixel sent:", url);
}
// send on tab hide and every 5s
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "hidden") sendTracking();
});
setInterval(sendTracking, 5000);
</script>
"""
# -------------------------
# BASE PAGE TEMPLATE
# -------------------------
BASE_TEMPLATE = """
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>{{ title }}</title>
<style>
body { background:#020617; color:#e5e7eb; font-family:system-ui; margin:0; }
header {
padding:16px 24px;
border-bottom:1px solid #111827;
background:#020617dd;
backdrop-filter:blur(10px);
position:sticky; top:0; z-index:10;
}
.logo { font-weight:700; font-size:12px; letter-spacing:0.08em; color:#22d3ee; }
main { max-width:960px; margin:24px auto 40px; padding:0 16px; }
.grid { display:grid; gap:18px; grid-template-columns:repeat(auto-fit,minmax(260px,1fr)); }
.card {
background:#020617; border-radius:16px; border:1px solid #111827;
box-shadow:0 18px 40px rgba(0,0,0,.7); overflow:hidden;
}
.thumb { width:100%; height:160px; object-fit:cover; display:block; }
.meta { padding:14px 16px; }
.pill { display:inline-block; padding:3px 9px; border-radius:999px;
border:1px solid #1f2937; font-size:10px; color:#9ca3af; margin-bottom:6px; }
h3 { margin:4px 0 6px; font-size:16px; }
p { margin:0; font-size:13px; color:#9ca3af; }
a { color:inherit; text-decoration:none; }
a:hover h3 { text-decoration:underline; }
</style>
</head>
<body>
<header><div class="logo">LINUXNDROID β’ BLOG DEMO</div></header>
<main>{{ body|safe }}</main>
{{ tracker|safe }}
</body>
</html>
"""
def render_page(title, body_html):
tracker_html = render_template_string(
TRACKER_JS,
tracker_url=url_for("track_gif", _external=True),
)
return render_template_string(
BASE_TEMPLATE,
title=title,
body=body_html,
tracker=tracker_html,
)
# -------------------------
# LANDING PAGE β PURE BLOG HOMEPAGE
# -------------------------
@app.route("/")
def landing():
import random
posts = BLOGS.copy()
random.shuffle(posts)
cards = []
for b in posts:
url = url_for("blog_post", cat_slug=b["cat_slug"], slug=b["slug"])
card = f"""
<article class="card">
<a href="{url}">
<img class="thumb" src="{b['thumb']}" alt="{b['title']}">
<div class="meta">
<div class="pill">{b['category']}</div>
<h3>{b['title']}</h3>
<p>{b['excerpt']}</p>
</div>
</a>
</article>
"""
cards.append(card)
body = f"""
<section class="grid">
{''.join(cards)}
</section>
"""
html = render_page("Blog Home", body)
resp = make_response(html)
if "tracking_id" not in request.cookies:
resp.set_cookie("tracking_id", str(uuid.uuid4()), max_age=31536000)
return resp
# -------------------------
# BLOG POST PAGE
# -------------------------
@app.route("/blog/<cat_slug>/<slug>")
def blog_post(cat_slug, slug):
blog = find_blog(cat_slug, slug)
if not blog:
return "Blog not found", 404
body = f"""
<article class="card" style="overflow:hidden;">
<img class="thumb" src="{blog['thumb']}" alt="{blog['title']}">
<div class="meta" style="padding:18px 18px 22px;">
<div class="pill">{blog['category']}</div>
<h3 style="font-size:20px;margin-top:6px;margin-bottom:10px;">{blog['title']}</h3>
<p style="margin-bottom:10px;">{blog['excerpt']}</p>
<p style="font-size:13px;color:#9ca3af;">
This is a demo article page. The real point here is that just by reading
this blog, your interest profile is updated in the admin dashboard under
the category <b>{blog['category']}</b>.
</p>
</div>
</article>
"""
return render_page(blog["title"], body)
# -------------------------
# ADMIN LOGIN
# -------------------------
LOGIN_TEMPLATE = """
<!doctype html>
<html>
<head><meta charset="utf-8"><title>Admin Login</title>
<style>
body { background:#020617; color:#e5e7eb; font-family:system-ui;
display:flex; align-items:center; justify-content:center; height:100vh; margin:0; }
.card {
background:#020617; border-radius:16px; border:1px solid #111827;
padding:24px; width:280px; box-shadow:0 18px 40px rgba(0,0,0,.7);
}
input {
width:100%; padding:8px 10px; border-radius:8px; border:1px solid #1f2937;
background:#020617; color:#e5e7eb; margin-top:10px;
}
button {
margin-top:14px; width:100%; padding:8px 10px; border-radius:999px;
border:none; background:linear-gradient(135deg,#22c55e,#22d3ee);
color:#020617; font-weight:600; cursor:pointer;
}
h2 { margin:0 0 6px; font-size:18px; }
p { margin:0; font-size:13px; color:#9ca3af; }
</style>
</head>
<body>
<form class="card" method="post">
<h2>Admin Dashboard</h2>
<p>Enter password to view analytics.</p>
<input type="password" name="password" placeholder="Password">
<button type="submit">Login</button>
</form>
</body>
</html>
"""
@app.route("/admin/login", methods=["GET", "POST"])
def admin_login():
if request.method == "POST":
if request.form.get("password") == ADMIN_PASSWORD:
session["is_admin"] = True
return redirect("/admin")
return render_template_string(
LOGIN_TEMPLATE.replace(
"</h2>",
"</h2><p style='color:#f97373;margin-bottom:4px;'>Wrong password</p>",
1,
)
)
return render_template_string(LOGIN_TEMPLATE)
@app.route("/admin/logout", methods=["POST"])
def admin_logout():
session.pop("is_admin", None)
return redirect("/admin/login")
# -------------------------
# STATS HELPER
# -------------------------
def compute_stats(profiles):
total_visitors = len(profiles)
total_visits = 0
page_views = {}
interests_count = {}
site_counts = {}
for profile in profiles.values():
visits = profile.get("visit_history", [])
total_visits += len(visits)
for v in visits:
page = v.get("page", "unknown")
page_views[page] = page_views.get(page, 0) + 1
for i in profile.get("interests", []):
interests_count[i] = interests_count.get(i, 0) + 1
for s in profile.get("sites", []):
site_counts[s] = site_counts.get(s, 0) + 1
return {
"total_visitors": total_visitors,
"total_visits": total_visits,
"page_views": page_views,
"interests_count": interests_count,
"site_counts": site_counts,
}
# -------------------------
# ADMIN DASHBOARD
# -------------------------
ADMIN_TEMPLATE = """
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Admin Analytics</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.socket.io/4.7.5/socket.io.min.js"></script>
<style>
body { background:#020617; color:#e5e7eb; font-family:system-ui; margin:0; }
header {
padding:16px 24px; border-bottom:1px solid #111827; background:#020617;
display:flex; justify-content:space-between; align-items:center;
}
.logo { font-weight:700; font-size:12px; letter-spacing:0.08em; color:#22d3ee; }
main { max-width:1100px; margin:24px auto 40px; padding:0 16px; }
.grid { display:grid; gap:16px; grid-template-columns:repeat(auto-fit,minmax(260px,1fr)); }
.card { background:#020617; border-radius:16px; border:1px solid #111827; padding:16px; box-shadow:0 18px 40px rgba(0,0,0,.7); }
h2 { margin:0 0 10px; font-size:18px; }
.stat { font-size:26px; font-weight:700; }
button {
border:none; border-radius:999px; padding:6px 12px; font-size:12px;
font-weight:600; cursor:pointer; background:#111827; color:#e5e7eb;
}
#liveFeed { font-size:12px; max-height:200px; overflow:auto; background:#020617; border-radius:12px; border:1px solid #111827; padding:8px; }
a { color:#22d3ee; text-decoration:none; }
</style>
</head>
<body>
<header>
<div class="logo">TRACKING DEMO β’ ADMIN</div>
<div>
<a href="/admin/list">Visitors</a>
<form style="display:inline;" method="post" action="/admin/logout">
<button type="submit">Logout</button>
</form>
</div>
</header>
<main>
<section class="grid">
<div class="card">
<h2>Visitors</h2>
<div class="stat" id="statVisitors">{{ stats.total_visitors }}</div>
<p style="font-size:12px;color:#9ca3af;">Unique profiles tracked.</p>
</div>
<div class="card">
<h2>Visits</h2>
<div class="stat" id="statVisits">{{ stats.total_visits }}</div>
<p style="font-size:12px;color:#9ca3af;">Total page views.</p>
</div>
</section>
<section class="grid" style="margin-top:18px;">
<div class="card">
<h2>Page Views</h2>
<canvas id="pageViewsChart" height="180"></canvas>
</div>
<div class="card">
<h2>Interest Segments</h2>
<canvas id="interestsChart" height="180"></canvas>
</div>
</section>
<section class="grid" style="margin-top:18px;">
<div class="card">
<h2>Cross-Domain Sites</h2>
<canvas id="sitesChart" height="160"></canvas>
</div>
<div class="card">
<h2>Live Events</h2>
<div id="liveFeed"></div>
</div>
</section>
</main>
<script>
const stats = {{ stats_json|safe }};
function buildChart(ctxId, labels, data, type="bar") {
if (!labels.length) return;
const ctx = document.getElementById(ctxId).getContext("2d");
new Chart(ctx, {
type,
data: { labels, datasets: [{ data, borderWidth: 1 }] },
options: {
plugins: { legend: { display:false } },
scales: {
x: { ticks:{ color:"#9ca3af" } },
y: { ticks:{ color:"#9ca3af" }, beginAtZero:true }
}
}
});
}
buildChart("pageViewsChart",
Object.keys(stats.page_views),
Object.values(stats.page_views),
"bar"
);
buildChart("interestsChart",
Object.keys(stats.interests_count),
Object.values(stats.interests_count),
"doughnut"
);
buildChart("sitesChart",
Object.keys(stats.site_counts),
Object.values(stats.site_counts),
"bar"
);
// Realtime via Socket.IO
const socket = io();
socket.on("stats_update", data => {
if (data.total_visitors !== undefined)
document.getElementById("statVisitors").innerText = data.total_visitors;
if (data.total_visits !== undefined)
document.getElementById("statVisits").innerText = data.total_visits;
});
socket.on("live_event", ev => {
const feed = document.getElementById("liveFeed");
const line = `[${new Date(ev.timestamp * 1000).toLocaleTimeString()}] `
+ `${ev.profile_key} β ${ev.page} (${ev.origin || "unknown origin"})`
+ ` eng=${ev.engagement} | ${ev.country || "Unknown"}, ${ev.state || "Unknown"}`;
const div = document.createElement("div");
div.textContent = line;
feed.prepend(div);
const children = feed.children;
if (children.length > 100) feed.removeChild(feed.lastChild);
});
</script>
</body>
</html>
"""
@app.route("/admin")
def admin_home():
if not session.get("is_admin"):
return redirect("/admin/login")
profiles = load_profiles()
stats = compute_stats(profiles)
return render_template_string(
ADMIN_TEMPLATE,
stats=stats,
stats_json=json.dumps(stats),
)
# -------------------------
# ADMIN LIST VISITORS
# -------------------------
@app.route("/admin/list")
def admin_list():
if not session.get("is_admin"):
return redirect("/admin/login")
profiles = load_profiles()
rows = []
for pid, profile in profiles.items():
visits = profile.get("visit_history", [])
interests = ", ".join(profile.get("interests", []))
country = state = "Unknown"
if visits:
last = visits[-1]
country = last.get("country", "Unknown")
state = last.get("state", "Unknown")
rows.append(
f"<tr>"
f"<td><a href='/admin/user/{pid}'>{pid}</a></td>"
f"<td>{interests}</td>"
f"<td>{country}</td>"
f"<td>{state}</td>"
f"<td>{len(visits)}</td>"
f"</tr>"
)
html = f"""
<html><head><meta charset='utf-8'><title>Visitors</title>
<style>
body {{ background:#020617;color:#e5e7eb;font-family:system-ui; }}
table {{ border-collapse:collapse;width:100%;margin-top:20px; }}
th,td {{ padding:8px 6px;border-bottom:1px solid #111827;font-size:13px; }}
a {{ color:#22d3ee; text-decoration:none; }}
</style>
</head><body>
<h2 style='padding:20px;'>Visitors</h2>
<table>
<tr><th>Profile ID</th><th>Interests</th><th>Country</th><th>State</th><th>Visits</th></tr>
{''.join(rows)}
</table>
<p style='padding:20px;'><a href='/admin'>β Back to Dashboard</a></p>
</body></html>
"""
return html
# -------------------------
# ADMIN VISITOR DETAIL
# -------------------------
VISITOR_TEMPLATE = """
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Visitor {{ pid }}</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<style>
body { background:#020617;color:#e5e7eb;font-family:system-ui;margin:0; }
header { padding:16px 24px;border-bottom:1px solid #111827;background:#020617; }
main { max-width:900px;margin:24px auto 40px;padding:0 16px; }
.card { background:#020617;border-radius:16px;border:1px solid #111827;padding:16px;box-shadow:0 18px 40px rgba(0,0,0,.7);margin-bottom:16px; }
table { width:100%;border-collapse:collapse;font-size:13px; }
td { padding:6px 0;border-bottom:1px solid #111827; }
pre { background:#020617;border-radius:12px;border:1px solid #111827;padding:10px;font-size:11px;max-height:260px;overflow:auto; }
a { color:#22d3ee;text-decoration:none; }
</style>
</head>
<body>
<header><h2>Visitor {{ pid }}</h2></header>
<main>
<div class="card">
<h3>Device & Location</h3>
<table>
<tr><td>Device</td><td>{{ device.device }}</td></tr>
<tr><td>OS</td><td>{{ device.os }}</td></tr>
<tr><td>Browser</td><td>{{ device.browser }}</td></tr>
<tr><td>Fingerprint</td><td>{{ profile.fingerprint }}</td></tr>
<tr><td>Cookies</td><td>{{ profile.tracking_ids }}</td></tr>
<tr><td>Country</td><td>{{ last_visit.country }}</td></tr>
<tr><td>State</td><td>{{ last_visit.state }}</td></tr>
<tr><td>City</td><td>{{ last_visit.city }}</td></tr>
<tr><td>IP Address</td><td>{{ last_visit.ip }}</td></tr>
<tr><td>First Seen</td><td>{{ first_seen }}</td></tr>
<tr><td>Last Seen</td><td>{{ last_seen }}</td></tr>
</table>
</div>
<div class="card">
<h3>Interests</h3>
<pre>{{ interests }}</pre>
</div>
<div class="card">
<h3>Cross-Domain Sites</h3>
<pre>{{ sites }}</pre>
</div>
<div class="card">
<h3>Engagement Over Time</h3>
<canvas id="engChart" height="120"></canvas>
</div>
<div class="card">
<h3>Visit History</h3>
<pre>{{ visits }}</pre>
</div>
<p><a href="/admin/list">β Back to Visitors</a></p>
</main>
<script>
const labels = {{ labels|safe }};
const values = {{ values|safe }};
if (labels.length) {
const ctx = document.getElementById("engChart").getContext("2d");
new Chart(ctx, {
type: "line",
data: { labels, datasets: [{ label:"Engagement", data: values, borderWidth:2 }] },
options: { scales: { y: { beginAtZero:true } } }
});
}
</script>
</body>
</html>
"""
@app.route("/admin/user/<pid>")
def admin_user(pid):
if not session.get("is_admin"):
return redirect("/admin/login")
profiles = load_profiles()
if pid not in profiles:
return "Profile not found"
profile = profiles[pid]
visits = profile.get("visit_history", [])
if visits:
first_seen = datetime.fromtimestamp(visits[0]["timestamp"]).strftime("%Y-%m-%d %H:%M:%S")
last_seen = datetime.fromtimestamp(visits[-1]["timestamp"]).strftime("%Y-%m-%d %H:%M:%S")
ua = visits[-1].get("userAgent", "")
device = parse_user_agent(ua)
labels = [f"Visit {i+1}" for i in range(len(visits))]
values = [v.get("engagement", 0) for v in visits]
last_visit = visits[-1]
else:
first_seen = last_seen = "No data"
device = {"device": "Unknown", "os": "Unknown", "browser": "Unknown"}
labels, values = [], []
last_visit = {"country": "Unknown", "state": "Unknown", "city": "Unknown", "ip": "Unknown"}
return render_template_string(
VISITOR_TEMPLATE,
pid=pid,
profile=profile,
device=device,
first_seen=first_seen,
last_seen=last_seen,
last_visit=last_visit,
interests=json.dumps(profile.get("interests", []), indent=2),
sites=json.dumps(profile.get("sites", []), indent=2),
visits=json.dumps(visits, indent=2),
labels=json.dumps(labels),
values=json.dumps(values),
)
# -------------------------
# TRACKING ENDPOINT β PIXEL
# -------------------------
@app.route("/track.gif")
def track_gif():
import base64
raw = request.args.get("data", "")
try:
payload = json.loads(raw)
except Exception:
payload = {"error": "decode_failed", "raw": raw}
profiles = load_profiles()
fp = payload.get("fpVisitorId")
tid = request.cookies.get("tracking_id")
page = payload.get("page")
origin = payload.get("origin")
if fp:
key = f"fp_{fp}"
elif tid:
key = f"ck_{tid}"
else:
key = "unknown_" + str(uuid.uuid4())
if key not in profiles:
profiles[key] = {
"fingerprint": fp,
"tracking_ids": [],
"interests": [],
"sites": [],
"visit_history": [],
"total_engagement": 0,
}
profile = profiles[key]
if tid and tid not in profile["tracking_ids"]:
profile["tracking_ids"].append(tid)
if origin and origin not in profile["sites"]:
profile["sites"].append(origin)
clicks = payload.get("clicks", 0)
scroll = payload.get("scrollDepth", 0)
timeSpent = payload.get("timeSpent", 0)
engagement = timeSpent + scroll * 2 + clicks * 5
profile["total_engagement"] += engagement
# GEO from IP
user_ip = request.headers.get("X-Forwarded-For", request.remote_addr)
geo = get_geo(user_ip)
visit_entry = {
"timestamp": time.time(),
"origin": origin,
"page": page,
"url": payload.get("url"),
"clicks": clicks,
"scroll": scroll,
"timeSpent": timeSpent,
"engagement": engagement,
"userAgent": payload.get("userAgent"),
"country": geo["country"],
"state": geo["state"],
"city": geo["city"],
"ip": user_ip,
}
profile["visit_history"].append(visit_entry)
# ------- Interest tagging from BLOG CATEGORY -------
new_interests = []
# If URL is a blog page
if page and page.startswith("/blog/"):
parts = page.split("/")
if len(parts) >= 3:
cat_slug = parts[2]
mapping = {
"tech": "Tech",
"cybersecurity": "Cybersecurity",
"news": "News",
"stock": "Stock",
"instagram": "Instagram",
}
cat_name = mapping.get(cat_slug, f"Blog: {cat_slug.title()}")
new_interests.append(cat_name)
# Keep for cross-domain external pages too
url_lower = (payload.get("url") or "").lower()
if "instagram" in url_lower:
new_interests.append("Instagram")
if "stock" in url_lower or "market" in url_lower:
new_interests.append("Stock")
for tag in new_interests:
if tag not in profile["interests"]:
profile["interests"].append(tag)
# ---------------------------------------------------
save_profiles(profiles)
stats = compute_stats(profiles)
socketio.emit("stats_update", stats, broadcast=True)
socketio.emit(
"live_event",
{
"profile_key": key,
"page": page,
"origin": origin,
"engagement": engagement,
"timestamp": time.time(),
"country": geo["country"],
"state": geo["state"],
},
broadcast=True,
)
gif = base64.b64decode(
"R0lGODlhAQABAPAAAP///wAAACH5BAAAAAAALAAAAAABAAEAAAICRAEAOw=="
)
return gif, 200, {"Content-Type": "image/gif"}
# -------------------------
# RUN
# -------------------------
if __name__ == "__main__":
socketio.run(app, host="0.0.0.0", port=5000, debug=True)