ga4

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GA4 - 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)

一次性设置步骤

  1. Enable Google Analytics Data API: https://console.cloud.google.com/apis/library/analyticsdata.googleapis.com
  2. Create OAuth credentials or use existing Google Cloud project
  3. Set environment variables:
    • GA4_PROPERTY_ID
      - Your GA4 property ID (numeric, e.g., "123456789")
    • GOOGLE_CLIENT_ID
      - OAuth client ID
    • GOOGLE_CLIENT_SECRET
      - OAuth client secret
    • GOOGLE_REFRESH_TOKEN
      - OAuth refresh token (from initial auth flow)
  1. 启用Google Analytics Data API:https://console.cloud.google.com/apis/library/analyticsdata.googleapis.com
  2. 创建OAuth凭据或使用现有的Google Cloud项目
  3. 设置环境变量:
    • GA4_PROPERTY_ID
      - 你的GA4属性ID(数字格式,例如:"123456789")
    • GOOGLE_CLIENT_ID
      - OAuth客户端ID
    • GOOGLE_CLIENT_SECRET
      - OAuth客户端密钥
    • GOOGLE_REFRESH_TOKEN
      - OAuth刷新令牌(来自初始授权流程)

Common Queries

常用查询示例

Top Pages (by pageviews)

热门页面(按页面浏览量排序)

bash
python3 scripts/ga4_query.py --metric screenPageViews --dimension pagePath --limit 30
bash
python3 scripts/ga4_query.py --metric screenPageViews --dimension pagePath --limit 30

Top Pages with Sessions & Users

包含会话数和用户数的热门页面

bash
python3 scripts/ga4_query.py --metrics screenPageViews,sessions,totalUsers --dimension pagePath --limit 20
bash
python3 scripts/ga4_query.py --metrics screenPageViews,sessions,totalUsers --dimension pagePath --limit 20

Traffic Sources

流量来源

bash
python3 scripts/ga4_query.py --metric sessions --dimension sessionSource --limit 20
bash
python3 scripts/ga4_query.py --metric sessions --dimension sessionSource --limit 20

Landing Pages

着陆页

bash
python3 scripts/ga4_query.py --metric sessions --dimension landingPage --limit 30
bash
python3 scripts/ga4_query.py --metric sessions --dimension landingPage --limit 30

Custom Date Range

自定义日期范围

bash
python3 scripts/ga4_query.py --metric sessions --dimension pagePath --start 2026-01-01 --end 2026-01-15
bash
python3 scripts/ga4_query.py --metric sessions --dimension pagePath --start 2026-01-01 --end 2026-01-15

Filter 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:
screenPageViews
,
sessions
,
totalUsers
,
newUsers
,
activeUsers
,
bounceRate
,
averageSessionDuration
,
conversions
,
eventCount
常用指标:
screenPageViews
,
sessions
,
totalUsers
,
newUsers
,
activeUsers
,
bounceRate
,
averageSessionDuration
,
conversions
,
eventCount

Available Dimensions

可用维度

Common dimensions:
pagePath
,
pageTitle
,
landingPage
,
sessionSource
,
sessionMedium
,
sessionCampaignName
,
country
,
city
,
deviceCategory
,
browser
,
date
常用维度:
pagePath
,
pageTitle
,
landingPage
,
sessionSource
,
sessionMedium
,
sessionCampaignName
,
country
,
city
,
deviceCategory
,
browser
,
date

Output Formats

输出格式

Default: Table format Add
--json
for JSON output Add
--csv
for CSV output
默认:表格格式 添加
--json
参数可输出JSON格式 添加
--csv
参数可输出CSV格式