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