How to return the total pageview using Google Analytics SDK?

99封情书 提交于 2019-12-25 01:45:21

问题


I am able to return the pageviews for a page. But the API returns those pageviews separated by source. So for page /this-page-slug/ it returns X amount of views for direct, Y for referral, Z for mobile, W for none and so on. I want to get the total pageviews for all sources per path. How can I do that?

This is the function that I am using to get the results:

function getResults($analytics, $profileId) {
  // Calls the Core Reporting API and queries for the number of sessions
  // for the last seven days.
$optParams = array(
    'dimensions' => 'ga:source, ga:medium, ga:pagePath',
    'sort' => '-ga:pageviews');

   return $analytics->data_ga->get(
       'ga:' . $profileId,
       '2017-08-23',
       '2017-10-04',
       'ga:uniquePageviews,ga:pageviews,ga:pageviewsPerSession',
        $optParams);
}

回答1:


The way Google Analytics works is that metrics add up and dimensions break metrics down.

If you want to have the total you either add up the metrics per dimension in your query result, or you do a second query without dimensions (that's why the arguments array is called opt(ional)Params in the documentation, because the query works without dimensions, filters etc).




回答2:


I was able to find my own solution from Eike's post. The solution was to exclude ga:source and ga:medium from dimensions. Like so:

function getResults($analytics, $profileId) {
  // Calls the Core Reporting API and queries for the number of sessions
  // for the last seven days.
$optParams = array(
    'dimensions' => 'ga:pagePath',
    'sort' => '-ga:pageviews');

   return $analytics->data_ga->get(
       'ga:' . $profileId,
       '2017-08-23',
       '2017-10-04',
       'ga:uniquePageviews,ga:pageviews,ga:pageviewsPerSession',
        $optParams);
}


来源:https://stackoverflow.com/questions/46573995/how-to-return-the-total-pageview-using-google-analytics-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!