-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalytics.py
79 lines (68 loc) · 2.55 KB
/
analytics.py
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
import db
import sys
import time
import base64
import geoip2.database
from datetime import datetime
from collections import Counter
from flask import Flask, Response, render_template, request, jsonify
app = Flask(__name__)
reader = geoip2.database.Reader('./GeoLite2-Country.mmdb')
@app.route("/")
def index():
return render_template("index.html")
@app.route("/analytics")
def analytics():
start = int(request.args.get('start', 0))
end = int(request.args.get('end', time.time()))
data = db.load(start=start, end=end)
paths = Counter()
referrers = Counter()
countries = Counter()
devices = Counter()
resolutions = Counter()
for page_view in data:
countries[page_view['country']] += 1
paths[page_view['path']] += 1
referrers[page_view['referrer']] += 1
devices[page_view['device']] += 1
resolutions['{}x{}'.format(
page_view['width'], page_view['height'])] += 1
start_formatted = datetime.fromtimestamp(start).strftime("%c")
end_formatted = datetime.fromtimestamp(end).strftime("%c")
return render_template('analytics.html',
start=start_formatted,
end=end_formatted,
paths=paths.most_common(),
referrers=referrers.most_common(),
countries=countries.most_common(),
devices=devices.most_common(),
resolutions=resolutions.most_common())
@app.route('/pixel.gif')
def pixel():
ip = request.headers.get('X-Forwarded-For', request.remote_addr)
country = ''
try:
response = reader.country(ip)
country = response.country.name
except geoip2.errors.AddressNotFoundError:
pass
device = request.user_agent.browser + ' ' + \
request.user_agent.version.split('.')[0]
timestamp = int(time.time())
path = request.args.get('path', '')
title = request.args.get('title', '')
referrer = request.args.get('referrer', '')
width = int(request.args.get('resolution', '').split(',').pop(0))
height = int(request.args.get('resolution', '').split(',').pop())
db.save(timestamp=timestamp,
path=path,
title=title,
referrer=referrer,
country=country,
device=device,
width=width,
height=height)
gif = base64.b64decode( # transparent 1x1 GIF, 43 bytes
'R0lGODlhAQABAIAAAP8AAP8AACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==')
return Response(gif, mimetype="image/gif")