Update app.py
Browse files
app.py
CHANGED
|
@@ -30,21 +30,34 @@ ga = BetaAnalyticsDataClient(credentials=creds)
|
|
| 30 |
# -------------------------
|
| 31 |
# FETCH ALL PAGE PATHS
|
| 32 |
# -------------------------
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
req = RunReportRequest(
|
| 35 |
property=f"properties/{GA4_PROPERTY_ID}",
|
| 36 |
dimensions=[{"name": "pagePath"}],
|
| 37 |
metrics=[{"name": "screenPageViews"}],
|
| 38 |
date_ranges=[{"start_date": start, "end_date": end}],
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
|
|
|
| 41 |
res = ga.run_report(req)
|
| 42 |
|
| 43 |
-
pages = sorted(
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
| 46 |
return pages
|
| 47 |
|
|
|
|
| 48 |
# -------------------------
|
| 49 |
# FETCH JOURNEY STEPS
|
| 50 |
# -------------------------
|
|
@@ -180,33 +193,99 @@ def run_journey(start, end, landing_page):
|
|
| 180 |
# -------------------------
|
| 181 |
# UI
|
| 182 |
# -------------------------
|
|
|
|
|
|
|
|
|
|
| 183 |
with gr.Blocks() as app:
|
| 184 |
gr.Markdown("## π Customer Journey Explorer (GA4)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
|
|
|
|
|
|
|
|
|
| 186 |
with gr.Row():
|
| 187 |
-
sd = gr.Textbox(
|
| 188 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
|
| 190 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
summary = gr.Markdown()
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
inputs=[sd, ed],
|
| 202 |
outputs=page_dd
|
| 203 |
)
|
| 204 |
|
| 205 |
-
|
| 206 |
-
|
|
|
|
| 207 |
inputs=[sd, ed, page_dd],
|
| 208 |
-
outputs=[
|
| 209 |
)
|
| 210 |
|
|
|
|
|
|
|
|
|
|
| 211 |
if __name__ == "__main__":
|
| 212 |
-
app.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
# -------------------------
|
| 31 |
# FETCH ALL PAGE PATHS
|
| 32 |
# -------------------------
|
| 33 |
+
@lru_cache(maxsize=3)
|
| 34 |
+
def fetch_top_page_paths(start, end, limit=300):
|
| 35 |
+
"""
|
| 36 |
+
Fetch TOP page paths by traffic and cache results.
|
| 37 |
+
"""
|
| 38 |
req = RunReportRequest(
|
| 39 |
property=f"properties/{GA4_PROPERTY_ID}",
|
| 40 |
dimensions=[{"name": "pagePath"}],
|
| 41 |
metrics=[{"name": "screenPageViews"}],
|
| 42 |
date_ranges=[{"start_date": start, "end_date": end}],
|
| 43 |
+
order_bys=[{
|
| 44 |
+
"metric": {"metric_name": "screenPageViews"},
|
| 45 |
+
"desc": True
|
| 46 |
+
}],
|
| 47 |
+
limit=limit
|
| 48 |
)
|
| 49 |
+
|
| 50 |
res = ga.run_report(req)
|
| 51 |
|
| 52 |
+
pages = sorted({
|
| 53 |
+
r.dimension_values[0].value
|
| 54 |
+
for r in res.rows
|
| 55 |
+
if r.dimension_values[0].value
|
| 56 |
+
})
|
| 57 |
+
|
| 58 |
return pages
|
| 59 |
|
| 60 |
+
|
| 61 |
# -------------------------
|
| 62 |
# FETCH JOURNEY STEPS
|
| 63 |
# -------------------------
|
|
|
|
| 193 |
# -------------------------
|
| 194 |
# UI
|
| 195 |
# -------------------------
|
| 196 |
+
# -------------------------
|
| 197 |
+
# UI β JOURNEY EXPLORER
|
| 198 |
+
# -------------------------
|
| 199 |
with gr.Blocks() as app:
|
| 200 |
gr.Markdown("## π Customer Journey Explorer (GA4)")
|
| 201 |
+
gr.Markdown(
|
| 202 |
+
"""
|
| 203 |
+
Analyze **end-to-end customer journeys**:
|
| 204 |
+
- Where users **started**
|
| 205 |
+
- Which pages they **navigated through**
|
| 206 |
+
- Where they **exited**
|
| 207 |
+
- Whether they **submitted any form before exit** (Yes / No)
|
| 208 |
+
"""
|
| 209 |
+
)
|
| 210 |
|
| 211 |
+
# -------------------------
|
| 212 |
+
# DATE CONTROLS
|
| 213 |
+
# -------------------------
|
| 214 |
with gr.Row():
|
| 215 |
+
sd = gr.Textbox(
|
| 216 |
+
label="Start Date (YYYY-MM-DD)",
|
| 217 |
+
value=str(date.today() - timedelta(days=30))
|
| 218 |
+
)
|
| 219 |
+
ed = gr.Textbox(
|
| 220 |
+
label="End Date (YYYY-MM-DD)",
|
| 221 |
+
value=str(date.today())
|
| 222 |
+
)
|
| 223 |
|
| 224 |
+
# -------------------------
|
| 225 |
+
# PAGE SELECTION
|
| 226 |
+
# -------------------------
|
| 227 |
+
with gr.Row():
|
| 228 |
+
page_dd = gr.Dropdown(
|
| 229 |
+
label="Starting Page Path",
|
| 230 |
+
choices=[],
|
| 231 |
+
allow_custom_value=True,
|
| 232 |
+
interactive=True,
|
| 233 |
+
info="Loads top pages by traffic. You can also type a custom page path."
|
| 234 |
+
)
|
| 235 |
|
| 236 |
+
with gr.Row():
|
| 237 |
+
load_pages_btn = gr.Button(
|
| 238 |
+
"β‘ Load Top Pages (Fast)",
|
| 239 |
+
variant="secondary"
|
| 240 |
+
)
|
| 241 |
+
run_journey_btn = gr.Button(
|
| 242 |
+
"Analyze Journey",
|
| 243 |
+
variant="primary"
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
# -------------------------
|
| 247 |
+
# OUTPUTS
|
| 248 |
+
# -------------------------
|
| 249 |
+
gr.Markdown("### π Journey Visualization")
|
| 250 |
+
journey_chart = gr.Plot()
|
| 251 |
+
|
| 252 |
+
gr.Markdown("### π Exit Pages")
|
| 253 |
+
exit_df = gr.Dataframe(
|
| 254 |
+
interactive=False,
|
| 255 |
+
wrap=True
|
| 256 |
+
)
|
| 257 |
|
| 258 |
+
gr.Markdown("### π§ Journey Summary")
|
| 259 |
+
journey_summary = gr.Markdown()
|
|
|
|
| 260 |
|
| 261 |
+
# -------------------------
|
| 262 |
+
# BUTTON ACTIONS
|
| 263 |
+
# -------------------------
|
| 264 |
+
|
| 265 |
+
# Load top page paths (cached + limited)
|
| 266 |
+
load_pages_btn.click(
|
| 267 |
+
fn=lambda s, e: gr.update(
|
| 268 |
+
choices=fetch_top_page_paths(s, e),
|
| 269 |
+
value=None
|
| 270 |
+
),
|
| 271 |
inputs=[sd, ed],
|
| 272 |
outputs=page_dd
|
| 273 |
)
|
| 274 |
|
| 275 |
+
# Run full journey analysis
|
| 276 |
+
run_journey_btn.click(
|
| 277 |
+
fn=run_journey,
|
| 278 |
inputs=[sd, ed, page_dd],
|
| 279 |
+
outputs=[journey_chart, exit_df, journey_summary]
|
| 280 |
)
|
| 281 |
|
| 282 |
+
# -------------------------
|
| 283 |
+
# APP LAUNCH
|
| 284 |
+
# -------------------------
|
| 285 |
if __name__ == "__main__":
|
| 286 |
+
app.launch(
|
| 287 |
+
server_name="0.0.0.0",
|
| 288 |
+
server_port=7860,
|
| 289 |
+
show_api=False,
|
| 290 |
+
show_error=False
|
| 291 |
+
)
|