ga4
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGA4 - Google Analytics 4 Data API
GA4 - Google Analytics 4 Data API
Query GA4 properties for analytics data: page views, sessions, users, traffic sources, conversions, and more.
查询GA4属性的分析数据:页面浏览量、会话数、用户数、流量来源、转化量等。
Setup (one-time)
一次性设置步骤
- Enable Google Analytics Data API: https://console.cloud.google.com/apis/library/analyticsdata.googleapis.com
- Create OAuth credentials or use existing Google Cloud project
- Set environment variables:
- - Your GA4 property ID (numeric, e.g., "123456789")
GA4_PROPERTY_ID - - OAuth client ID
GOOGLE_CLIENT_ID - - OAuth client secret
GOOGLE_CLIENT_SECRET - - OAuth refresh token (from initial auth flow)
GOOGLE_REFRESH_TOKEN
- 启用Google Analytics Data API:https://console.cloud.google.com/apis/library/analyticsdata.googleapis.com
- 创建OAuth凭据或使用现有的Google Cloud项目
- 设置环境变量:
- - 你的GA4属性ID(数字格式,例如:"123456789")
GA4_PROPERTY_ID - - OAuth客户端ID
GOOGLE_CLIENT_ID - - OAuth客户端密钥
GOOGLE_CLIENT_SECRET - - OAuth刷新令牌(来自初始授权流程)
GOOGLE_REFRESH_TOKEN
Common Queries
常用查询示例
Top Pages (by pageviews)
热门页面(按页面浏览量排序)
bash
python3 scripts/ga4_query.py --metric screenPageViews --dimension pagePath --limit 30bash
python3 scripts/ga4_query.py --metric screenPageViews --dimension pagePath --limit 30Top Pages with Sessions & Users
包含会话数和用户数的热门页面
bash
python3 scripts/ga4_query.py --metrics screenPageViews,sessions,totalUsers --dimension pagePath --limit 20bash
python3 scripts/ga4_query.py --metrics screenPageViews,sessions,totalUsers --dimension pagePath --limit 20Traffic Sources
流量来源
bash
python3 scripts/ga4_query.py --metric sessions --dimension sessionSource --limit 20bash
python3 scripts/ga4_query.py --metric sessions --dimension sessionSource --limit 20Landing Pages
着陆页
bash
python3 scripts/ga4_query.py --metric sessions --dimension landingPage --limit 30bash
python3 scripts/ga4_query.py --metric sessions --dimension landingPage --limit 30Custom Date Range
自定义日期范围
bash
python3 scripts/ga4_query.py --metric sessions --dimension pagePath --start 2026-01-01 --end 2026-01-15bash
python3 scripts/ga4_query.py --metric sessions --dimension pagePath --start 2026-01-01 --end 2026-01-15Filter by Page Path
按页面路径筛选
bash
python3 scripts/ga4_query.py --metric screenPageViews --dimension pagePath --filter "pagePath=~/blog/"bash
python3 scripts/ga4_query.py --metric screenPageViews --dimension pagePath --filter "pagePath=~/blog/"Available Metrics
可用指标
Common metrics: , , , , , , , ,
screenPageViewssessionstotalUsersnewUsersactiveUsersbounceRateaverageSessionDurationconversionseventCount常用指标:, , , , , , , ,
screenPageViewssessionstotalUsersnewUsersactiveUsersbounceRateaverageSessionDurationconversionseventCountAvailable Dimensions
可用维度
Common dimensions: , , , , , , , , , ,
pagePathpageTitlelandingPagesessionSourcesessionMediumsessionCampaignNamecountrycitydeviceCategorybrowserdate常用维度:, , , , , , , , , ,
pagePathpageTitlelandingPagesessionSourcesessionMediumsessionCampaignNamecountrycitydeviceCategorybrowserdateOutput Formats
输出格式
Default: Table format
Add for JSON output
Add for CSV output
--json--csv默认:表格格式
添加参数可输出JSON格式
添加参数可输出CSV格式
--json--csv