0
votes

Here's how the calculated metric is defined in the Google Analytics web interface:

Name Profit

External Name calcMetric_Profit

Formatting Type Currency (Decimal)

Formula {{Revenue}} - {{Shipping}} - {{Tax}}

The "External Name" field has this description:

The external name is used to uniquely identify the calculated metric when querying via API. External names may contain alphanumeric and underscore characters only. Special characters, symbols, and spaces are not allowed.

When I request the metric from v4 of the reporting API, using this request:

POST https://analyticsreporting.googleapis.com/v4/reports:batchGet?key=<redacted>

{
 "reportRequests": [
  {
   "dateRanges": [
    {
     "startDate": "2018-01-01",
     "endDate": "2018-01-22"
    }
   ],
   "metrics": [
    {
     "expression": "ga:calcMetric_Profit"
    }
   ],
   "viewId": "<redacted>"
  }
 ]
}

I get this response:

400

{
 "error": {
  "code": 400,
  "message": "Invalid value 'ga:calcMetric_Profit' for metric parameter.",
  "status": "INVALID_ARGUMENT"
 }
}

Am I doing something wrong?

Is this related to calculated metrics still being in Beta?

1
When did you create the calculated metric, and when did you attempt to retrieve it via the API? There is usually a short lag between when you created it and when it's available in the API. - vinoaj
The metric was created at least 3 days ago, and I'm currently getting the same error message. - Tyler Murphy

1 Answers

0
votes

Change your startDate to one day after you created the calculated metric. Calculated metrics apply from the date they were created (there is no retrofilling). So if your startDate is before the date of creation, the API will throw an error, as the calculated metric doesn't exist for the dates prior to it being created.