title: Google analytics api - get page views by URL
body: <p>I was able to run de "hello" app from google analytics api:</p>
  
  <pre><code>function getFirstProfileId($analytics) {
      // Get the user's first view (profile) ID.
  
      // Get the list of accounts for the authorized user.
      $accounts = $analytics-&gt;management_accounts-&gt;listManagementAccounts();
  
      if (count($accounts-&gt;getItems()) &gt; 0) {
          $items = $accounts-&gt;getItems();
          $firstAccountId = $items[0]-&gt;getId();
  
          // Get the list of properties for the authorized user.
          $properties = $analytics-&gt;management_webproperties
                  -&gt;listManagementWebproperties($firstAccountId);
  
          if (count($properties-&gt;getItems()) &gt; 0) {
              $items = $properties-&gt;getItems();
              $firstPropertyId = $items[0]-&gt;getId();
  
              // Get the list of views (profiles) for the authorized user.
              $profiles = $analytics-&gt;management_profiles-&gt;listManagementProfiles($firstAccountId, $firstPropertyId);
  
              if (count($profiles-&gt;getItems()) &gt; 0) {
                  $items = $profiles-&gt;getItems();
  
                  // Return the first view (profile) ID.
                  return $items[0]-&gt;getId();
  
              } else {
                  throw new Exception('No views (profiles) found for this user.');
              }
          } else {
              throw new Exception('No properties found for this user.');
          }
      } else {
          throw new Exception('No accounts found for this user.');
      }
  }
  
  function getResults($analytics, $profileId) {
      // Calls the Core Reporting API and queries for the number of sessions
      // for the last seven days.
      return $analytics-&gt;data_ga-&gt;get(
          'ga:' . $profileId,
          '7daysAgo',
          'today',
          'ga:sessions'
      );
  }
  </code></pre>
  
  <p>That returns the page views for the (whole) account. <strong>But</strong> I would like to get the page views ( for the whole range ) <strong>for a specific url</strong>,</p>
  
  <p>I'm trying to find how to do so, but the most <a href="https://developers.google.com/analytics/devguides/reporting/core/dimsmets#view=detail&amp;group=page_tracking&amp;jump=ga_pagevalue" rel="nofollow noreferrer">similar page</a> that I found, seems to be written in a different syntax. So I don't know how to enter those params in the current app.</p>
  
  <p>Any thoughts?</p>
  
  <p>--EDIT--</p>
  
  <p>For more information, the previous code, prints:</p>
  
  <p>But this code ( with the "syntax" of the link I provided before )</p>
  
  <pre><code>function getReport($analytics) {
  
    // Replace with your view ID, for example XXXX.
    $VIEW_ID = "40xxxyyy9"; // I got it from The Google Account Explorer 
  
    // Create the DateRange object.
    $dateRange = new Google_Service_AnalyticsReporting_DateRange();
    $dateRange-&gt;setStartDate("7daysAgo");
    $dateRange-&gt;setEndDate("today");
  
    // Create the Metrics object.
    $sessions = new Google_Service_AnalyticsReporting_Metric();
    $sessions-&gt;setExpression("ga:sessions");
    $sessions-&gt;setAlias("sessions");
  
    // Create the ReportRequest object.
    $request = new Google_Service_AnalyticsReporting_ReportRequest();
    $request-&gt;setViewId($VIEW_ID);
    $request-&gt;setDateRanges($dateRange);
    $request-&gt;setMetrics(array($sessions));
  
    $body = new Google_Service_AnalyticsReporting_GetReportsRequest();
    $body-&gt;setReportRequests( array( $request) );
    return $analytics-&gt;reports-&gt;batchGet( $body );
  }
  
  function printResults($reports) {
    for ( $reportIndex = 0; $reportIndex &lt; count( $reports ); $reportIndex++ ) {
      $report = $reports[ $reportIndex ];
      $header = $report-&gt;getColumnHeader();
      $dimensionHeaders = $header-&gt;getDimensions();
      $metricHeaders = $header-&gt;getMetricHeader()-&gt;getMetricHeaderEntries();
      $rows = $report-&gt;getData()-&gt;getRows();
  
      for ( $rowIndex = 0; $rowIndex &lt; count($rows); $rowIndex++) {
        $row = $rows[ $rowIndex ];
        $dimensions = $row-&gt;getDimensions();
        $metrics = $row-&gt;getMetrics();
        for ($i = 0; $i &lt; count($dimensionHeaders) &amp;&amp; $i &lt; count($dimensions); $i++) {
          print($dimensionHeaders[$i] . ": " . $dimensions[$i] . "\n");
        }
  
        for ($j = 0; $j &lt; count( $metricHeaders ) &amp;&amp; $j &lt; count( $metrics ); $j++) {
          $entry = $metricHeaders[$j];
          $values = $metrics[$j];
          print("Metric type: " . $entry-&gt;getType() . "\n" );
          for ( $valueIndex = 0; $valueIndex &lt; count( $values-&gt;getValues() ); $valueIndex++ ) {
            $value = $values-&gt;getValues()[ $valueIndex ];
            print($entry-&gt;getName() . ": " . $value . "\n");
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>Prints out:</p>
  
  <pre><code> Uncaught exception 'Google_Service_Exception' with message '{ "error": { "code": 403, "message": "Google Analytics Reporting API has not been used in project api-project-395xxxxxxx2105 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/analyticsreporting.googleapis.com/overview?project=api-project-395xxxxxxx2105 then retry.
  </code></pre>
  
  <p>And if I follow the console link to my project. It let me know that is already enabled ( otherwise the first code in the question shouldn't work )</p>