Google-api-php-client: Google Analytics ignore page views before 2017-11-05

Created on 6 Jan 2020  路  2Comments  路  Source: googleapis/google-api-php-client

Environment details

  • OS: From my web site
  • PHP version: 7.2
  • Package name and version: Google Analytics API version 3

Steps to reproduce

  1. Search total page views from Google Analytics data using the Google API
  2. Taking as example one url with 63K views, the result from the API is 10K views
  3. If I try to take views before 2017-11-05 i'm having empty result

Code example

    $optParams = array(
        'dimensions' => 'ga:pageTitle,ga:pagePath',
        'filters' => 'ga:hostname==www.mydomain.com',
        'sort' => '-ga:pageviews',
        'start-index'=>1,
    );

    $res= $analytics->data_ga->get(
        'ga:' . $profileId,
        '2013-01-01',
        'today',
        'ga:pageviews',
        $optParams);
    $rows=$res["rows"];
    print_r($rows);

The result for this query is:

[7] => Array
    (
        [0] => Leccionario Bienal B铆blico Patr铆stico | mydomain.com
        [1] => /leccionario-bienal-biblico-patristico/
        [2] => 10828
    )

But this URL have 63K+ views!

Now, changing today param for one date before 2017-11-05:


    $optParams = array(
        'dimensions' => 'ga:pageTitle,ga:pagePath',
        'filters' => 'ga:hostname==www.mydomain.com',
        'sort' => '-ga:pageviews',
        'start-index'=>1,
    );

    $res= $analytics->data_ga->get(
        'ga:' . $profileId,
        '2013-01-01',
        '2017-11-02',
        'ga:pageviews',
        $optParams);
    $rows=$res["rows"];
    print_r($res);

The result for this query is:

[rows] => 
[sampleSize] => 
[sampleSpace] => 
[selfLink] => https://www.googleapis.com/analytics/v3/data/ga?ids=ga:69061508&dimensions=ga:pageTitle,ga:pagePath&metrics=ga:pageviews&sort=-ga:pageviews&filters=ga:hostname%3D%3Dwww.my-domain.com&start-date=2013-01-01&end-date=2017-11-02&start-index=1
[totalResults] => 0
[totalsForAllResults] => Array
    (
        [ga:pageviews] => 0
    )
question

All 2 comments

Hi @padrecedano !
Try using the Reporting API instead, which is newer and tied to a "View", which will more accurately match what you see in your Google Analytics dashboard.

  // Replace with your view ID, for example XXXX.
  $VIEW_ID = "<REPLACE_WITH_VIEW_ID>";
  $analytics = new Google_Service_AnalyticsReporting($client);

  // Create the DateRange object.
  $dateRange = new Google_Service_AnalyticsReporting_DateRange();
  $dateRange->setStartDate("2013-01-01");
  $dateRange->setEndDate("today");

  // Create the Metrics object.
  $sessions = new Google_Service_AnalyticsReporting_Metric();
  $sessions->setExpression("ga:pageviews");
  $sessions->setAlias("pageviews");

  // Create the ReportRequest object.
  $request = new Google_Service_AnalyticsReporting_ReportRequest();
  $request->setViewId($VIEW_ID);
  $request->setDateRanges($dateRange);
  $request->setMetrics(array($sessions));

  $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
  $body->setReportRequests(array($request));
  $response = $analytics->reports->batchGet($body);
  print_r($response);

@bshaffer It's working fine with code based on Reporting API.

Thanks a lot!

Was this page helpful?
0 / 5 - 0 ratings