Introduction
Using Python’s Analytics Data API, you can flexibly retrieve GA4 data for analysis and reporting. Exploring the steps for handling GA4 data from Python to incorporate into a Hugo static site generator.
What is the Analytics Data API?
The Analytics Data API is an API for programmatically accessing GA4 data. It allows you to retrieve report data and create custom reports. It requires a GCP account.
Using from Python
When using the Analytics Data API from Python, use the google-api-python-client and google-analytics-data packages.
pip install google-api-python-client
pip install google-analytics-data
Enabling the Analytics Data API
Enable the API
- Create a Cloud Platform project and enable the Google Analytics Data API v1
- Download the
credentials.jsonfile and save it to your working directoryAdd a service account to your Google Analytics 4 property
- Get the service account email address from the
client_emailfield in thecredentials.jsonfile- Add the user to your Google Analytics 4 property using the obtained email address (viewer permission only)
Configure authentication
- Set the
GOOGLE_APPLICATION_CREDENTIALSenvironment variable to specify the service account credentials- Set the environment variable value to the path of the service account JSON file downloaded in step 1
Install the client library
- Install the Google Analytics Data API client library for your chosen programming language
Make API calls
- Once authentication and client library setup is complete, use the Google Analytics Data API to query your Google Analytics 4 property
Renamed the credentials.json file to ga4_prifile.json.
Setting Environment Variables
export GOOGLE_APPLICATION_CREDENTIALS=./ga4_prifile.json
export GA_PROPERTY_ID=384740337
After installing the packages and setting environment variables, you can call the API to retrieve report data as follows.
import os
from google.analytics.data_v1beta import BetaAnalyticsDataClient
from google.analytics.data_v1beta.types import DateRange
from google.analytics.data_v1beta.types import Dimension
from google.analytics.data_v1beta.types import Metric
from google.analytics.data_v1beta.types import RunReportRequest
def run_report(property_id):
client = BetaAnalyticsDataClient()
request = RunReportRequest(
property=f"properties/{property_id}",
dimensions=[Dimension(name="date")],
metrics=[Metric(name="sessions")],
date_ranges=[DateRange(start_date="365daysAgo", end_date="today")],
)
response = client.run_report(request)
print("Report result:")
for row in response.rows:
print(row.dimension_values[0].value, row.metric_values[0].value)
def main():
ga_property_id = os.environ['GA_PROPERTY_ID']
run_report(ga_property_id)
if __name__ == "__main__":
main()
This script retrieves the top 15 most viewed pages by page views in the past year.
Output
Most viewed articles in the last year:
10713views: PostgreSQLのpg_dump、pg_restoreについてまとめる6917views: Excelのグラフで横軸を1時間ごとのグラフにする方法4065views: MySQLのFLUSH PRIVILEGESが必要なケース3971views: Auroraの各バージョンのサポート期間3616views: EC2上からpsqlでAurora(PostgreSQL)に接続するまで3573views: PostgreSQLでfunctionの定義を確認する方法2768views: OracleとPostgreSQL(+Redshift)のchar、varcharのバイトと文字数の違い2720views: Data Pump(expdp/impdp)使用時のコマンドとオプション2610views: PostgreSQLの監視のためのログ設定について2385views: いまいちどOracle Databaseのデータ移行方法について考えてみる2193views: PostgreSQLでja_JP.UTF-8のデータベース作成時のlocaleエラー2115views: サーバ側と通信するCipher suite (暗号スイート) の調査方法2060views: PostgreSQLの自動Vacuumの実行タイミングと関連するパラメータ1940views: PostgreSQLで月初、月末日、翌月月初を取得する1709views: S3オブジェクトのmd5やEtagの関係性について整理する