Search code examples
google-analyticsgoogle-analytics-api

page visit and duration for each user id in google analytics


Connected python with google analytics, trying to pull raw data for each user-id, which page it has visited and how long spent on it. Test code from website look like this, i am lost on how to update this code.

the official metric website doesn't seem even have a col named user-id ... https://ga-dev-tools.appspot.com/dimensions-metrics-explorer/

POST https://analyticsreporting.googleapis.com/v4/reports:batchGet

{
  "reportRequests":
  [
    {
      "viewId": "XXXX",
      "dateRanges":
      [
        {"endDate": "2014-11-30", "startDate": "2014-11-01"}
      ],
      "metrics":
      [
        {"expression": "ga:users"}
      ],
      "dimensions":
      [
        {"name": "ga:city"}
      ]
    }
  ]
}

Solution

  • There is no dimension for user identification available through API. If you have GA360 and enabled the BigQuery integration, you can extract clientId / userId level information by querying the tables in BigQuery. According to this guide

    When you initially link an Analytics reporting view to BigQuery, Analytics exports 13 months or 10 billion hits (whichever is smaller) of historical data to BigQuery.

    Another solution is to create a Custom Dimension of user scope and pass clientId with it in Google Analytics. Then using API, you will able to fetch the required information.