Search code examples
google-analyticsgoogle-analytics-apigoogle-analytics-firebasegoogle-analytics-filters

Multiple eventActions filters in one request


I'm currently trying to work with the analytics api v4, everything is working great so far, I just need to set multiple event actions with one request. I've been able to do it with the query explorer, so I'm thinking we can get it as well.

I'm setting my dimension and first dimension filter, but can't quite figure how to specify another ga:eventAction filter.

$eventActionDim2 = new Google_Service_AnalyticsReporting_Dimension();
$eventActionDim2->setName("ga:eventAction");

$viewAction = new Google_Service_AnalyticsReporting_DimensionFilter();
$viewAction->setDimensionName('ga:eventAction');
$viewAction->setExpressions("views");

Tried

$viewAction->setExpressions(array("views","click"));

Tried setting a new DimensionFilter not forgetting to add it in setFilters.

Must be something trivial, but I can't find how to make it work, so if someone could help me get it right, it would be nice ! Thanks

Edit: Even better thing would be to be able to get ga:totalEvents of each eventActions of a given eventCategory.

Answer: All I had to do was to properly make use of the setOperator method (default operator is REGEXP if you don't specify anything), which let you use regex in setExpressions. Here is the link of the useful doc: https://developers.google.com/analytics/devguides/reporting/core/v4/rest/v4/reports/batchGet#operator

$viewAction = new Google_Service_AnalyticsReporting_DimensionFilter();
$viewAction->setDimensionName('ga:eventAction');
$viewAction->setOperator("REGEXP");
$viewAction->setExpressions('view|click|contact_form');

Solution

  • Regular expressions within dimension filter should do the trick:

    ga:eventAction =~ (views|click)
    

    you might need to URL-encode some of the special characters, but Query Explorer should be a good starting point to validate the query.