Query GA4 properties using the Google Analytics Data API v1.
Open Google Cloud Console.
Create or select a project.
Enable Google Analytics Data API.
Create a service account: IAM & Admin → Service Accounts → Create service account.
Finish the wizard.
ga-credentials.json (or any path you pass via --credentials / GOOGLE_APPLICATION_CREDENTIALS).…@….iam.gserviceaccount.com).Either:
ga-credentials.json in this skill directory, orGOOGLE_APPLICATION_CREDENTIALS to the absolute path of the JSON key.Never commit real keys. .gitignore excludes ga-credentials.json and config.json.
bash
python ga_query.py --action list-properties
bash
python ga_query.py --action realtime \
--property-id YOUR-GA4-PROPERTY-ID
bash
python ga_query.py --action historical \
--property-id YOUR-GA4-PROPERTY-ID \
--start-date 7daysAgo \
--end-date yesterday \
--metrics activeUsers,sessions,eventCount \
--dimensions country,deviceCategory
bash
python ga_query.py --action metadata \
--property-id YOUR-GA4-PROPERTY-ID
ArgumentDescriptionDefault--property-idNumeric GA4 property IDRequired (except list-properties)--credentialsService account JSON pathga-credentials.json
ArgumentDescriptionDefault--metricsComma-separated metricsactiveUsers``--dimensionsComma-separated dimensions(none)--minute-rangeMinutes ago window, e.g. 0-30``0-30
ArgumentDescriptionDefault--start-dateStart ( YYYY-MM-DD or relative)Required--end-dateEndRequired--metricsComma-separated metricsactiveUsers``--dimensionsComma-separated dimensions(none)--limitMax rows10000``--offsetPaging offset0
NameMeaningactiveUsersActive userssessionsSessionseventCountEvent countengagementRateEngagement rateaverageSessionDurationAvg session duration (seconds)screenPageViewsPage / screen viewsconversionsConversionstotalRevenueRevenue
NameMeaningcountryCountrycityCitydeviceCategorydesktop / mobile / tableteventNameEvent namepagePathPage pathsourceTraffic sourcemediumMediumcampaignCampaigndateDate
2024-01-15today, yesterday, 7daysAgo, 30daysAgoDefault: Markdown tables. Use --output json for machine-readable output.
bash
pip install google-analytics-data
Optional (traffic source report + DingTalk): pip install requests and set DINGTALK_WEBHOOK / DINGTALK_SECRET.