S
S
Sergey Bard2017-03-22 22:12:43
Google
Sergey Bard, 2017-03-22 22:12:43

How to analyze search queries by device using Google Search Console APIs?

Hi everybody. I work with Google Search Console APIs, I need to get an analysis of search queries by device.
I do everything 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());
        
        $enddate = date("Y-m-d",time()-(3*(24*60*60)));
        $startdate = date("Y-m-d",time()-(3*(24*60*60)));
        
        $ar_fil_dim = new Google_Service_Webmasters_ApiDimensionFilter();
        $ar_fil_dim -> setDimension("device");
        $ar_fil_dim -> setOperator("contains");
        $ar_fil_dim -> setExpression("MOBILE");
        
        $ar_fil_q = new Google_Service_Webmasters_ApiDimensionFilterGroup();
        $ar_fil_q -> setFilters($ar_fil_dim);
        
        $webmasters1 = new Google_Service_Webmasters_SearchAnalyticsQueryRequest();
        $webmasters1->setStartDate($startdate);
        $webmasters1->setEndDate($enddate);
        $webmasters1->setSearchType("web");
        $webmasters1->setDimensions("query");
        $webmasters1->setDimensionFilterGroups($ar_fil_q);
        $webmasters1->setRowLimit(1000);
        
        $siteUrl = 'нужный сайт';
        $result = $webmasters->searchanalytics->query($siteUrl, $webmasters1) ;

        echo '<pre>';
          print_r($result);
        echo '</pre>';

In the example, MOBILE is specified to receive only from mobile devices, I tried everything I could find, but the fact is that the code does not respond to any filters at all and always gives the same result. . I have already tried everything I found on the net, but I still can’t understand why the code does not respond to filters.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Bard, 2017-03-23
@serg_small_developer

doing like this

$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)));
$enddate = "2017-03-18";
$startdate = "2017-03-18";

$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) ;

but he does not want to filter only on the request that I wrote, and gives all requests without a filter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question