Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GA: How to access Cohort Analysis via Analytics API?

Cohort dimensions and metrics are listed here. Yet, when I try to query it using API (e.g. using Query Explorer) an error 400 occurs.

One of queries I've tried is: metrics = ga:cohortActiveUsers and dimensions = ga:cohortNthDay .

Is is possible to query Cohort Analysis report via API?

like image 219
Olga Kaminska Avatar asked Oct 30 '22 06:10

Olga Kaminska


2 Answers

The problem you are having is because the Query explorer uses the v3 of the Google Analytics API. If you look at the Dimensions and Metrics Explorer you will notice that these dimensions were added in the Analytics Reporting API V4.

The error message you are getting is incorrect and should be corrected soon. It should state something more like This metric cannot be used in Version 3 of the API. You caught this while we where in the process of rolling out the new API. Which has now been officially released see change log

To make use of these new dimensions and metrics you must construct a V4 cohort request:

POST https://analyticsreporting.googleapis.com/v4/reports:batchGet
{
  "reportRequests": [{
    "viewId": "XXXX",
    # No date range is required in the request
    "dimensions": [{"name": "ga:cohort" },{"name": "ga:cohortNthDay" }],
    "metrics": [
      {"expression": "ga:cohortActiveUsers" },
      {"expression": "ga:cohortTotalUsers"}
    ],
    "cohortGroup": {
      "cohorts": [{
        "name": "cohort 1",
        "type": "FIRST_VISIT_DATE",
        "dateRange": { "startDate": "2015-08-01", "endDate": "2015-08-01"}
      },{
        "name": "cohort 2",
        "type": "FIRST_VISIT_DATE",
        "dateRange": {"startDate": "2015-07-01", "endDate": "2015-07-01"}
      }]
    }
  }]
}
like image 129
Matt Avatar answered Nov 08 '22 05:11

Matt


It is possible to compose a cohort requests by using the Request Composer tool, in the Cohort Request tab.

As you select the options in the Set query parameters section, the request payload is shown below.

Hope it helps.

like image 32
lbodevan Avatar answered Nov 08 '22 05:11

lbodevan