2

I am having a small problem adding multiple dimensions to the API request for Search Console.

If I add the following to group by country it works fine.

 $filters->setDimension("country");
 $filters->setOperator("equals");
 $filters->setExpression($country);
 $filter->setFilters($filters);
 $filter->setFilters(array($filters));
 $request->setDimensionFilterGroups(array($filter));

But if I add another dimension below for device, it skips the country dimension and only runs the query with the device dimension. It's perfectly possible to run both according to this on the search console API site:

    'filters': [{
        'dimension': 'country',
        'expression': 'ind'
      }, {
        'dimension': 'device',
        'expression': 'MOBILE'
    }]

So, any idea how I can use them both on a query?

It's probably simply some PHP code, but I can't find any documentation on multiple dimensions or any examples anywhere with PHP in mind.

vishuB
  • 4,173
  • 5
  • 31
  • 49
Nick Smith
  • 311
  • 1
  • 8

3 Answers3

2

It was simple.

$filters->setDimension("country");

$filters->setOperator("equals");

$filters->setExpression($country);

$filters2->setDimension("device");

$filters2->setOperator("equals");

$filters2->setExpression($device);

$filter->setFilters($filters);

$filter->setFilters(array($filters,$filters2));

$request->setDimensionFilterGroups(array($filter));

Works like a charm.

Nick Smith
  • 311
  • 1
  • 8
1

Many thanks for sharing this, Nick - it really helped! One tiny thing: you won't need the following:

$filter->setFilters($filters);

as you're setting filters the right way with the next line of code.

Also, I was stuck on the following line:

$request->setDimensionFilterGroups(array($filter));

as I was passing the $filter object as it is instead of using an array. I really found this counter-intuitive: this is the only object setDimensionFilterGroups method needs; I'm still wondering why it needs an array instead.

Cheers,

Sal

0

Nick's answer did helped me start but currently it isn't complete. To help other devs looking for an answer, hope this would help you:

$filter_group = new Google_Service_Webmasters_ApiDimensionFilterGroup;
$groups = array(['device', 'mobile', 'equals'], ['country', 'GBR', 'equals']);
$filters = array();

if($groups) {
  foreach ($groups as $key => $group) {
    $filters[$key] = new Google_Service_Webmasters_ApiDimensionFilter;
    $filters[$key]->setDimension($group[0]);
    $filters[$key]->setExpression($group[1]);
    $filters[$key]->setOperator($group[2]);
  }

  $filter_group->setFilters($filters);
}

$request->setDimensionFilterGroups(array($filter_group));

Also take note, currently it won't work if in your filter group you have two or more same dimension but different value for filter or expression.

e.g.

$groups = array(['device', 'mobile', 'equals'], ['device', 'tablet', 'equals']);

It is because OR operation is currently not supported for filters.

Check here and this answer here.

Community
  • 1
  • 1
Robin Carlo Catacutan
  • 13,249
  • 11
  • 52
  • 85