Reputation: 45
I'm using https://github.com/google/google-api-php-client to get pageviews from google analytics. I'm following this guide: https://developers.google.com/analytics/devguides/reporting/core/v3/quickstart/service-php. my custom code:
function getResults(&$analytics, $profileId) {
return $analytics->data_ga->get(
'ga:' . $profileId,
'7daysAgo',
'today',
'ga:visits',
array(
'filters' => 'ga:pagePath==/project_z2o/',
'dimensions' => 'ga:pagePath',
'metrics' => 'ga:pageviews',
'sort' => '-ga:pageviews',
'max-results' => '25'
));
}
Is it possible if I want to get the pageviews every hour. If Yes, What query is it? If No, Is there any another ways that I can get it. Thanks.
Upvotes: 0
Views: 1801
Reputation: 32760
Edit, okay, I misunderstood your question. If you want to query the current hour you'D have to use the realtime API (and more often than hourly), as GA requires some processing time for incoming data, so current data is usually not available (i.e. what you want might not be possible or at least not very reliable). Realtime will return current data on a few select metrics and dimensions (which include path and pageviews). Plus you cannot specify hours for the timeframe so this is moot in any case, you'd have to select "today" as start- and endpoint and break down by the hour (you could add a filter by "ga:hour" and specify the current hour. As I pointed out this will return unreliable data, or no data at all).
Unless you really really need the data immediately you are better of to query yesterdays data and have it broken down by hour.
Old answer: I assume that you want the pageview metrics broken down by hour - you'd have to add "ga:hour" as dimension (which would return the hour of the day from 0 to 23). Alternatively you can use ga:nthHour which sequentially counts the hours within your timeframe (i.e. in a seven day timeframe it would count upward to 7*24 = 168 hours).
You can find this in the dimensions and metrics explorer, "time-based dimensions".
Upvotes: 3