0
votes

I have some custom reports in GA, I need to get them through API .

  reportRequests: [
      {
        viewId: VIEW_ID,
        dateRanges: [
          {
            startDate: '7daysAgo',
            endDate: 'today'
          }
        ],
        metrics: [
        {"expression": "ga:hOg8OmowQCyxAarL0pWvWg"},
        {"expression": "ga:sessions"}
        ]
      }

Where can I find expression for my custom?

I have tried with some Ids like ga:hOg8OmowQCyxAarL0pWvWg, but they all give me this error:

"Unknown metric(s): ga:hOg8OmowQCyxAarL0pWvWg For details see https://developers.google.com/analytics/devguides/reporting/core/dimsmets."

2

2 Answers

3
votes

if you are trying to access Custom metrics use 'ga:metricXX' where you replace XX by the metric index so it should be ga:metric01 or ga:metric02...etc

to find custom metric index: 1. Click Admin, and navigate to the property to which you want to add custom metrics. 2. In the PROPERTY column, click Custom Definitions > Custom Metrics.

1
votes

In order to request data from the google analytics reporting api you must send valid dimensions and metrics you can see the list of valid dimensions and metrics here Dimensions & Metrics Explorer

"Unknown metric(s): ga:hOg8OmowQCyxAarL0pWvWg For details see https://developers.google.com/analytics/devguides/reporting/core/dimsmets."

Means that the metric you are requesting (ga:hOg8OmowQCyxAarL0pWvWg) is not one of the allowed metrics on the list.

You cant request custom reports via the API your going to have to build the report using the metadata available to the api