S
S
Sergey Bard2017-03-23 11:40:24
Google
Sergey Bard, 2017-03-23 11:40:24

How to enable filter in Google Search Console APIs?

I asked a question here , but since it is already far below, I will update it, because I need to solve the problem faster, I
redid the script and now the code looks like this

require_once __DIR__ . '/vendor/autoload.php';
      function initializeWebmasters(){
       $KEY_FILE_LOCATION = __DIR__ . '/файл сервисного аккаунта.json';
        $client = new Google_Client();
        $client->setApplicationName("Google Webmasters Hello");
        $client->setAuthConfig($KEY_FILE_LOCATION);
        $client->setScopes(['https://www.googleapis.com/auth/webmasters.readonly']);
       return $client; 
      }
      $webmasters = new Google_Service_Webmasters(initializeWebmasters());
        
        $query = ['запрос'];
$searchTypes = ['web'];
$dataType = ['query'];

$enddate = date("Y-m-d",time()-(3*(24*60*60)));
$startdate = date("Y-m-d",time()-(3*(24*60*60)));

$ar_fil_dimqq = new Google_Service_Webmasters_ApiDimensionFilter();
$ar_fil_dimqq -> setDimension("query");
$ar_fil_dimqq -> setExpression($query);

$ar_fil_q = new Google_Service_Webmasters_ApiDimensionFilterGroup();
$ar_fil_q -> setFilters( $ar_fil_dimqq );

$webmasters1 = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$webmasters1->setStartDate($startdate);
$webmasters1->setEndDate($enddate);
$webmasters1->setSearchType($searchTypes);
$webmasters1->setDimensions($dataType);
$webmasters1->setDimensionFilterGroups($ar_fil_q);

$siteUrl = 'сайт';

$result = $webmasters->searchanalytics->query($siteUrl, $webmasters1) ;

I want to draw a conclusion by filters from the $query variable, but it does not see the filter and displays the full list of queries as in the console, please help me finish the script)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Max, 2017-03-23
@serg_small_developer

If I understand correctly what you want to end up with

$service = new Google_Service_Webmasters($client);
$filters = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
$filters->setStartDate($dtStart);
$filters->setEndDate($dtEnd);
$filters->dimensions = ['query', 'device'];

$filter = new Google_Service_Webmasters_ApiDimensionFilter();
$filter->setDimension('query');
$filter->setOperator('equals');
$filter->setExpression($query);

$filterGroup = new Google_Service_Webmasters_ApiDimensionFilterGroup();
$filterGroup->setFilters([$filter]);
$filters->setDimensionFilterGroups([$filterGroup]);

$response = $service->searchanalytics->query($host, $filters);

S
Sergey Bard, 2017-03-23
@serg_small_developer

Here is a working example

require_once __DIR__ . '/vendor/autoload.php';

      function initializeWebmasters(){
       $KEY_FILE_LOCATION = __DIR__ . '/service-account.json';
        $client = new Google_Client();
        $client->setApplicationName("Google Webmasters Hello");
        $client->setAuthConfig($KEY_FILE_LOCATION);
        $client->setScopes(['https://www.googleapis.com/auth/webmasters.readonly']);
       return $client;
      }
      $webmasters = new Google_Service_Webmasters(initializeWebmasters());
        
      $query = ['запрос'];
      $searchTypes = ['web'];
      $dataType = ['query','device'];

        $enddate = date("Y-m-d",time()-(3*(24*60*60)));
        $startdate = date("Y-m-d",time()-(3*(24*60*60)));
        
        $filter = new Google_Service_Webmasters_ApiDimensionFilter();
        $filter->setDimension('query');
        $filter->setOperator('equals');
        $filter->setExpression($query);
        
        $ar_fil_q = new Google_Service_Webmasters_ApiDimensionFilterGroup();
        $ar_fil_q -> setFilters( [$filter] );
        
        $webmasters1 = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
        $webmasters1->setStartDate($startdate);
        $webmasters1->setEndDate($enddate);
        $webmasters1->setSearchType($searchTypes);
        $webmasters1->setDimensions($dataType);
        $webmasters1->setDimensionFilterGroups( [$ar_fil_q] );
        
        $siteUrl = 'сайт';
        
        $result = $webmasters->searchanalytics->query($siteUrl, $webmasters1) ;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question