Search code examples
google-analyticsgoogle-analytics-apigoogle-analytics-firebase

What is a valid Google Analytics comparisonValue?


I am trying to hit the Google Analytics v4 API to get the number of events when my Event Action = "myStart". Here is my reportRequest for this value:

"reportRequests": [
  {
  "viewId": VIEW_ID,
  "dateRanges": [{
    "startDate": firstDay.format("YYYY-MM-DD"),
    "endDate": lastDay.format("YYYY-MM-DD")
  }],
  "metrics": [{"expression": "ga:eventValue"}],
  "metricFilterClauses": [{
    "filters": [{
      "metricName": "ga:eventAction",
      "operator": "EQUAL",
      "comparisonValue": "myStart"
    }]
  }]
}

The response I get is:

code: 400
message: 'Value for field request.metricFilterClauses.filters.comparisonValue = myStart is not valid.'
status: 'INVALID_ARGUMENT'

It would appear that 'myValue' is not what I should send, but the Google Analytics v4 documentation is horrible and doesn't give a solid example or explanation of what this value should be.

How can I get the number of eventAction = myStart events?


Solution

  • ga:eventAction is a dimension, so you should use a dimensionFilterClause not metricFilterClauses.

    You can tell which ones are metrics and which ones are dimensions by looking into the Dimensions and Metrics Explorer.

    I didn't test but I guess it will look something like this:

    "reportRequests": [
      {
      "viewId": VIEW_ID,
      "dateRanges": [{
        "startDate": firstDay.format("YYYY-MM-DD"),
        "endDate": lastDay.format("YYYY-MM-DD")
      }],
      "metrics": [{"expression": "ga:eventValue"}],
      "dimensionFilterClauses": [{
        "filters": [{
          "dimensionName": "ga:eventAction",
          "operator": "EXACT",
          "expressions": ["myStart"]
        }]
      }]
    }