Query GA4 properties directly via the Google Analytics Data API (analyticsdata.googleapis.com).
Go to https://console.cloud.google.com and create or select a project.
Go to APIs & Credentials > OAuth consent screen > Audience and set:
This avoids Google's app verification process (which requires a demo video for sensitive scopes like Analytics). Internal is fine for personal/team use. Note: this requires a Google Workspace account (not a personal @gmail.com).
If you must use External (e.g. you have a personal Gmail), set publishing status to "In production" and add the analytics.readonly scope under Data Access / Scopes.
Go to OAuth consent screen > Data Access (or Scopes) and add:
https://www.googleapis.com/auth/analytics.readonly
This is listed as a "sensitive scope" by Google. If your app is Internal, no verification is needed.
Go to: https://console.cloud.google.com/apis/library/analyticsdata.googleapis.com
Click Enable.
Go to APIs & Credentials > Credentials > Create Credentials > OAuth client ID
Save the Client ID and Client Secret.
Go to https://analytics.google.com > Admin (gear icon) > Property Settings. The Property ID is the numeric value at the top.
Run this on your local machine (needs a browser for the Google login flow):
pip install google-auth-oauthlib
python3 -c "from google_auth_oauthlib.flow import InstalledAppFlow; flow = InstalledAppFlow.from_client_config({'installed': {'client_id': 'YOUR_CLIENT_ID', 'client_secret': 'YOUR_CLIENT_SECRET', 'auth_uri': 'https://accounts.google.com/o/oauth2/auth', 'token_uri': 'https://oauth2.googleapis.com/token'}}, scopes=['https://www.googleapis.com/auth/analytics.readonly']); creds = flow.run_local_server(port=0); print('REFRESH TOKEN:', creds.refresh_token)"
Replace YOUR_CLIENT_ID and YOUR_CLIENT_SECRET with your values. A browser window will open for you to log in with Google. Copy the refresh token from the output.
GA4_PROPERTY_ID=123456789
GOOGLE_CLIENT_ID=your-client-id
GOOGLE_CLIENT_SECRET=your-client-secret
GOOGLE_REFRESH_TOKEN=your-refresh-token
analytics.readonly scope is probably not added to your OAuth consent screen. Go to Data Access/Scopes and add it, then regenerate your refresh token.python3 /mnt/skills/user/google-analytics/scripts/ga4_query.py \
--metrics screenPageViews \
--dimension pagePath \
--limit 20
python3 /mnt/skills/user/google-analytics/scripts/ga4_query.py \
--metrics screenPageViews,sessions,totalUsers \
--dimension pagePath \
--limit 20
python3 /mnt/skills/user/google-analytics/scripts/ga4_query.py \
--metrics sessions \
--dimension sessionSource \
--limit 20
python3 /mnt/skills/user/google-analytics/scripts/ga4_query.py \
--metrics sessions,totalUsers,conversions \
--dimensions sessionSource,sessionMedium \
--limit 20
python3 /mnt/skills/user/google-analytics/scripts/ga4_query.py \
--metrics sessions,bounceRate \
--dimension landingPage \
--limit 30
python3 /mnt/skills/user/google-analytics/scripts/ga4_query.py \
--metrics screenPageViews,sessions \
--dimension pagePath \
--start 2026-01-01 \
--end 2026-01-31 \
--limit 20
python3 /mnt/skills/user/google-analytics/scripts/ga4_query.py \
--metrics screenPageViews,sessions \
--dimension pagePath \
--filter "pagePath=~/blog/" \
--limit 20
python3 /mnt/skills/user/google-analytics/scripts/ga4_query.py \
--metrics conversions,sessions \
--dimensions sessionCampaignName,sessionSource \
--limit 20
python3 /mnt/skills/user/google-analytics/scripts/ga4_query.py \
--metrics sessions,totalUsers \
--dimension deviceCategory \
--limit 10
python3 /mnt/skills/user/google-analytics/scripts/ga4_query.py \
--metrics sessions,totalUsers \
--dimension country \
--limit 20
screenPageViews, sessions, totalUsers, newUsers, activeUsers, bounceRate, averageSessionDuration, conversions, eventCount, engagementRate, userEngagementDuration
pagePath, pageTitle, landingPage, sessionSource, sessionMedium, sessionCampaignName, country, city, deviceCategory, browser, date, week, month
Results are printed as a formatted table to stdout. Pipe to | python3 -m json.tool if you need raw JSON.
共 1 个版本