技能创建器
skill-creator
创建、改进和评估自定义技能,把专业流程转化为可重复使用的 AI 能力。
ga4-common-reports【数据与研究】Copy-paste recipes for the 6-7 reports every site owner actually wants: DAU/MAU/WAU, retention cohort, top pages by source, channel attribution, conversion funnel, geo breakdown, device split. Each recipe is a fully-formed Data API request. Trigger with "GA4 DAU", "GA4 retention", "GA4 top pages", "GA4 funnel", "GA4 channel report", "common GA4 repor…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Recipes for the reports that get asked for ~95% of the time. Each one is a complete runReport you can paste, change PROPERTY_ID, and run. Prerequisite: ga4-auth-setup done.
ga4-auth-setup and granted Viewer access to the target property.google-analytics-data installed and GA4_PROPERTY_ID set to the numeric property ID.The setup block (same for every recipe):
import os
from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import (
RunReportRequest, DateRange, Metric, Dimension,
FilterExpression, Filter, OrderBy,
)
PROPERTY = f"properties/{os.environ['GA4_PROPERTY_ID']}"
client = BetaAnalyticsDataClient()
req = RunReportRequest(
property=PROPERTY,
date_ranges=[DateRange(start_date="30daysAgo", end_date="yesterday")],
metrics=[Metric(name="activeUsers")],
dimensions=[Dimension(name="date")],
order_bys=[OrderBy(dimension=OrderBy.DimensionOrderBy(dimension_name="date"))],
)
resp = client.run_report(req)
for r in resp.rows:
print(f"{r.dimension_values[0].value} {r.metric_values[0].value}")
Why yesterday, not today: today's number is incomplete and will keep climbing through the day. For a clean rolling DAU, end the window at yesterday.
GA4 doesn't expose MAU as a single metric — you compute it from the same activeUsers rolled up over a wider date range. The trick: a single-row report with no date dimension returns the unique count over the entire window (de-duplicated across days).
# MAU (last 30 days)
mau = client.run_report(RunReportRequest(
property=PROPERTY,
date_ranges=[DateRange(start_date="29daysAgo", end_date="yesterday")],
metrics=[Metric(name="activeUsers")],
))
mau_count = int(mau.rows[0].metric_values[0].value) if mau.rows else 0
# WAU (last 7 days)
wau = client.run_report(RunReportRequest(
property=PROPERTY,
date_ranges=[DateRange(start_date="6daysAgo", end_date="yesterday")],
metrics=[Metric(name="activeUsers")],
))
wau_count = int(wau.rows[0].metric_values[0].value) if wau.rows else 0
print(f"MAU: {mau_count:,} WAU: {wau_count:,} Ratio (engagement): {wau_count/mau_count:.2%}")
Stickiness rule-of-thumb: WAU/MAU > 0.5 is good, > 0.7 is excellent, < 0.2 means most users visit once and bounce.
req = RunReportRequest(
property=PROPERTY,
date_ranges=[DateRange(start_date="7daysAgo", end_date="yesterday")],
metrics=[Metric(name="screenPageViews"), Metric(name="activeUsers"), Metric(name="averageSessionDuration")],
dimensions=[Dimension(name="pagePath")],
order_bys=[OrderBy(metric=OrderBy.MetricOrderBy(metric_name="screenPageViews"), desc=True)],
limit=25,
)
resp = client.run_report(req)
print(f"{'Path':<60} {'Views':>8} {'Users':>8} {'AvgSec':>8}")
for r in resp.rows:
print(f"{r.dimension_values[0].value[:58]:<60} "
f"{r.metric_values[0].value:>8} {r.metric_values[1].value:>8} "
f"{float(r.metric_values[2].value):>8.1f}")
req = RunReportRequest(
property=PROPERTY,
date_ranges=[DateRange(start_date="30daysAgo", end_date="yesterday")],
metrics=[Metric(name="activeUsers"), Metric(name="sessions"), Metric(name="engagedSessions")],
dimensions=[Dimension(name="sessionDefaultChannelGrouping")],
order_bys=[OrderBy(metric=OrderBy.MetricOrderBy(metric_name="activeUsers"), desc=True)],
)
resp = client.run_report(req)
print(f"{'Channel':<28} {'Users':>10} {'Sessions':>10} {'Engaged%':>10}")
for r in resp.rows:
users = int(r.metric_values[0].value)
sess = int(r.metric_values[1].value)
eng = int(r.metric_values[2].value)
eng_rate = eng / sess if sess else 0
print(f"{r.dimension_values[0].value:<28} {users:>10,} {sess:>10,} {eng_rate:>9.1%}")
GA4's default channel grouping has ~12 buckets: Direct, Organic Search, Paid Search, Organic Social, Paid Social, Email, Referral, Display, Video, Affiliates, Audio, etc. Use sessionSource + sessionMedium for finer-grained attribution (e.g. google / organic vs bing / organic).
GA4 has a built-in cohort exploration in the UI but the Data API doesn't expose it cleanly. The workaround: query DAU per week and compute rolling overlap. The cheap approximation:
# Weekly active users for the last 8 weeks
req = RunReportRequest(
property=PROPERTY,
date_ranges=[DateRange(start_date="56daysAgo", end_date="yesterday")],
metrics=[Metric(name="activeUsers")],
dimensions=[Dimension(name="isoYearIsoWeek")],
order_bys=[OrderBy(dimension=OrderBy.DimensionOrderBy(dimension_name="isoYearIsoWeek"))],
)
resp = client.run_report(req)
for r in resp.rows:
print(f"{r.dimension_values[0].value} {r.metric_values[0].value}")
For true cohort retention (e.g. "of users acquired in week N, what % came back in week N+1, N+2, N+3"), you need event-level data — use ga4-bigquery-export and write the cohort SQL directly. The Data API can't express the join.
GA4 funnels via API: query each step as a separate runReport filtered by the event that defines the step, then divide.
def step_users(event_name, days_ago=7):
return int(client.run_report(RunReportRequest(
property=PROPERTY,
date_ranges=[DateRange(start_date=f"{days_ago}daysAgo", end_date="yesterday")],
metrics=[Metric(name="activeUsers")],
dimension_filter=FilterExpression(filter=Filter(
field_name="eventName",
string_filter=Filter.StringFilter(
match_type=Filter.StringFilter.MatchType.EXACT,
value=event_name,
),
)),
)).rows[0].metric_values[0].value)
# Example funnel: landed → engaged → signed up → purchased
steps = [
("session_start", step_users("session_start")),
("user_engagement", step_users("user_engagement")),
("sign_up", step_users("sign_up")),
("purchase", step_users("purchase")),
]
top = steps[0][1] or 1
print(f"{'Step':<20} {'Users':>10} {'% of top':>10}")
for name, count in steps:
print(f"{name:<20} {count:>10,} {count/top:>9.1%}")
Limitation: this counts users who fired the event at any point in the window, NOT users who progressed through the funnel in order. For ordered funnels (true sequencing), use BigQuery export or the GA4 UI's Exploration → Funnel report.
req = RunReportRequest(
property=PROPERTY,
date_ranges=[DateRange(start_date="30daysAgo", end_date="yesterday")],
metrics=[Metric(name="activeUsers"), Metric(name="bounceRate")],
dimensions=[Dimension(name="country"), Dimension(name="deviceCategory")],
order_bys=[OrderBy(metric=OrderBy.MetricOrderBy(metric_name="activeUsers"), desc=True)],
limit=30,
)
resp = client.run_report(req)
for r in resp.rows:
country, device = r.dimension_values[0].value, r.dimension_values[1].value
users, bounce = r.metric_values[0].value, float(r.metric_values[1].value)
print(f"{country:<20} {device:<10} {users:>10} {bounce:>6.1%}")
A common signal: if one country dominates with low engagement + high bounce, it's often bot traffic from that country's cloud-host hubs (Singapore, Vietnam, China data centers are the usual suspects).
Each recipe prints a focused, ready-to-inspect report: date-series users, aggregate MAU/WAU, ranked pages or channels, a funnel, or geo/device rows. The results are API aggregates and should be interpreted with the date window and metric definitions shown in each recipe.
If a request fails, first confirm that the property ID is numeric, the credential has property access, and the metric/dimension pair is valid. Empty reports can be legitimate for an inactive property or a future/incomplete date range; use ga4-data-api-query to check compatibility and pagination before assuming data loss.
ga4-bigquery-export — the companion skill for event-level analysis and unsampled cohort queries.Three reasons to graduate to BigQuery export:
resp.metadata.data_loss_from_other_row=True. Sampled = approximate. BQ export = exact.See ga4-bigquery-export for the setup.
ga4-auth-setup — prerequisitega4-data-api-query — the underlying API the recipes here usega4-realtime-api — for "right now" data instead of any of the abovega4-bigquery-export — when these recipes hit their limits