How do I capture events (behavior information) using the Google Analytics API?

Asked 2 years ago, Updated 2 years ago, 88 views

Can I get analysis event information using Google Analytics API?

I was able to confirm that I could retrieve page view information using Python and Reporting API v4.

However, I can't get information on the event (details in the attached file).
Or rather, I tried to find out if I could get event information through API, but I don't know.

If anyone knows, please let me know.

Reference Site
Reporting API v4
https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet?hl=ja

What information do you want from the API?

python google-analytics-api

2022-09-30 18:01

1 Answers

Self-Solving!
Resolved by adding "ga:eventAction" to "dimensions" as shown below.

Documentation
https://ga-dev-tools.appspot.com/dimensions-metrics-explorer/?hl=ja

def get_report(analytics, next_page_token="0"):

request_body = {
    "reportRequests": [{
        "viewId": VIEW_ID,
        "pageSize": 100000,
        "pageToken": next_page_token,
        "dateRanges": [{'startDate':'7daysAgo', 'endDate':'today'}],
        "metrics": [{ "expression": "ga:pageviews"}, { "expression": "ga:users"}, { "expression": "ga:avgTimeOnPage"}],
        # "metrics": [{ "expression": "ga:users"}]
        "dimensions": [{"name":"ga:pagePath"}, {"name":"ga:pageTitle"}, {"name":"ga:eventAction"}],
        "orderBys": [{"fieldName": "ga:pageviews", "sortOrder": "DESCENDING"}]
    }]
}
return analysis.reports().batchGet(body=request_body).execute()


2022-09-30 18:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.