Search code examples
google-analyticsgoogle-apigoogle-analytics-apigoogle-analytics-firebasegoogle-analytics-sdk

How to return the total pageview using Google Analytics SDK?


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

Solution

  • 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);
    }