-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
262 lines (224 loc) · 9.99 KB
/
Copy pathserver.py
File metadata and controls
262 lines (224 loc) · 9.99 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
from fastapi import FastAPI, HTTPException, Depends
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import DateRange, Dimension, Metric, RunReportRequest
from datetime import datetime, timedelta
from dotenv import load_dotenv
import os
import secrets
from fastapi import FastAPI, HTTPException, Depends, Query
import json
from google.oauth2 import service_account
import base64
load_dotenv()
app = FastAPI()
security = HTTPBasic()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
ADMIN_USERNAME = os.getenv("ADMIN_USERNAME")
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD")
GA4_PROPERTY_ID = os.getenv("GA4_PROPERTY_ID")
ga_base64 = os.getenv("GA_SERVICE_ACCOUNT_BASE64")
if not ga_base64:
raise RuntimeError("GA_SERVICE_ACCOUNT_BASE64 not set")
service_account_info = json.loads(
base64.b64decode(ga_base64).decode("utf-8")
)
credentials = service_account.Credentials.from_service_account_info(
service_account_info
)
def verify_admin(credentials: HTTPBasicCredentials = Depends(security)):
is_username_correct = secrets.compare_digest(credentials.username, ADMIN_USERNAME)
is_password_correct = secrets.compare_digest(credentials.password, ADMIN_PASSWORD)
if not (is_username_correct and is_password_correct):
raise HTTPException(status_code=401, detail="Invalid credentials")
return credentials.username
def get_ga4_data(client, start_date, end_date, metrics, dimensions=None):
request = RunReportRequest(
property=f"properties/{GA4_PROPERTY_ID}",
date_ranges=[DateRange(start_date=start_date, end_date=end_date)],
metrics=[Metric(name=m) for m in metrics],
dimensions=[Dimension(name=d) for d in dimensions] if dimensions else []
)
response = client.run_report(request)
results = []
for row in response.rows:
item = {}
if dimensions:
for i, dim in enumerate(row.dimension_values):
item[response.dimension_headers[i].name] = dim.value
for i, metric in enumerate(row.metric_values):
item[response.metric_headers[i].name] = metric.value
results.append(item)
return results
def format_duration(seconds):
"""Convert seconds to MM:SS format"""
try:
mins = int(float(seconds)) // 60
secs = int(float(seconds)) % 60
return f"{mins}:{secs:02d}"
except:
return "0:00"
def format_date_label(date_str):
"""Convert YYYYMMDD to 'Mon DD' format"""
try:
date_obj = datetime.strptime(date_str, "%Y%m%d")
return date_obj.strftime("%b %d")
except:
return date_str
@app.get("/analytics")
def get_analytics(
days: int = Query(
30,
ge=1, # minimum 1 day
le=365, # reasonable upper limit (you can increase)
description="Number of days to look back (7, 30, 90 commonly used)"
),
admin: str = Depends(verify_admin)
):
try:
client = BetaAnalyticsDataClient(credentials=credentials)
# ── Calculate date range ────────────────────────────────────────
today = datetime.now().date()
start_date_obj = today - timedelta(days=days - 1) # inclusive
start_date = start_date_obj.strftime("%Y-%m-%d")
end_date = today.strftime("%Y-%m-%d")
# ── Overview (all metrics for the selected period) ──────────────
overview_raw = get_ga4_data(
client,
start_date,
end_date,
[
"totalUsers", "newUsers", "sessions", "engagedSessions",
"screenPageViews", "averageSessionDuration", "bounceRate",
"engagementRate", "conversions"
]
)
overview = {}
if overview_raw and len(overview_raw) > 0:
data = overview_raw[0]
total_sessions = float(data.get("sessions", 0))
overview = {
"periodDays": days,
"startDate": start_date,
"endDate": end_date,
"totalUsers": int(float(data.get("totalUsers", 0))),
"newUsers": int(float(data.get("newUsers", 0))),
"sessions": int(total_sessions),
"engagedSessions": int(float(data.get("engagedSessions", 0))),
"pageViews": int(float(data.get("screenPageViews", 0))),
"avgSessionDuration": format_duration(data.get("averageSessionDuration", "0")),
"bounceRate": round(float(data.get("bounceRate", 0)) * 100, 1),
"engagementRate": round(float(data.get("engagementRate", 0)) * 100, 1),
"conversions": int(float(data.get("conversions", 0))),
"conversionRate": round(
float(data.get("conversions", 0)) / max(total_sessions, 1) * 100, 1
),
}
# ── Traffic over time (daily breakdown) ─────────────────────────
traffic_raw = get_ga4_data(
client,
start_date,
end_date,
["activeUsers", "sessions", "screenPageViews", "engagedSessions"],
["date"]
)
traffic_over_time = {
"labels": [],
"activeUsers": [],
"sessions": [],
"pageViews": [],
"engagedSessions": [],
}
for row in sorted(traffic_raw, key=lambda x: x.get("date", "")): # ensure chronological
traffic_over_time["labels"].append(format_date_label(row.get("date", "")))
traffic_over_time["activeUsers"].append(int(float(row.get("activeUsers", 0))))
traffic_over_time["sessions"].append(int(float(row.get("sessions", 0))))
traffic_over_time["pageViews"].append(int(float(row.get("screenPageViews", 0))))
traffic_over_time["engagedSessions"].append(int(float(row.get("engagedSessions", 0))))
# ── Top pages ───────────────────────────────────────────────────
top_pages_raw = get_ga4_data(
client,
start_date,
end_date,
["screenPageViews", "averageSessionDuration", "bounceRate", "engagedSessions"],
["pagePath"]
)[:10] # you can adjust the limit
top_pages = [
{
"page": item.get("pagePath", "(not set)"),
"views": int(float(item.get("screenPageViews", 0))),
"avgTime": format_duration(item.get("averageSessionDuration", "0")),
"bounceRate": round(float(item.get("bounceRate", 0)) * 100, 1),
"engagedSessions": int(float(item.get("engagedSessions", 0))),
}
for item in top_pages_raw
]
# ── Device breakdown ────────────────────────────────────────────
devices_raw = get_ga4_data(
client,
start_date,
end_date,
["activeUsers"],
["deviceCategory"]
)
device_map = {"desktop": 0, "mobile": 0, "tablet": 0}
total_device_users = 0
for item in devices_raw:
cat = item.get("deviceCategory", "other").lower()
users = int(float(item.get("activeUsers", 0)))
if cat in device_map:
device_map[cat] += users
total_device_users += users
device_category = {
"labels": ["Desktop", "Mobile", "Tablet"],
"data": [
round(device_map["desktop"] / max(total_device_users, 1) * 100, 1),
round(device_map["mobile"] / max(total_device_users, 1) * 100, 1),
round(device_map["tablet"] / max(total_device_users, 1) * 100, 1),
],
"colors": ["#3b82f6", "#10b981", "#8b5cf6"] # you can keep/change
}
# ── Traffic sources (channel groups) ────────────────────────────
sources_raw = get_ga4_data(
client,
start_date,
end_date,
["sessions", "activeUsers"],
["sessionDefaultChannelGroup"]
)[:8]
traffic_sources = {
"labels": [],
"sessions": [],
"users": [],
}
for item in sources_raw:
traffic_sources["labels"].append(item.get("sessionDefaultChannelGroup", "(other)"))
traffic_sources["sessions"].append(int(float(item.get("sessions", 0))))
traffic_sources["users"].append(int(float(item.get("activeUsers", 0))))
# Final response
return {
"status": "success",
"days": days,
"startDate": start_date,
"endDate": end_date,
"data": {
"overview": overview,
"trafficOverTime": traffic_over_time,
"topPages": top_pages,
"deviceCategory": device_category,
"trafficSources": traffic_sources,
# "userEngagement": ... → consider removing or improving later
},
"timestamp": datetime.now().isoformat()
}
except ValueError as ve:
raise HTTPException(status_code=400, detail=f"Invalid days value: {str(ve)}")
except Exception as e:
raise HTTPException(status_code=500, detail=f"Analytics error: {str(e)}")