PHP Query Help

196 views
Skip to first unread message

Garrett Tichy

unread,
Aug 14, 2012, 4:13:43 PM8/14/12
to google-analytics...@googlegroups.com
This is my first attempt to work with the Core Reporting API.  I have successfully made it through the Hello Analytics tutorial and making API requests with no issue.  My problem lies with querying the API for using Dimensions, Metrics, and Filters.  Below is the code I am working with.. I am able to display how many visitors I have between the first of the month and the current day.  Then it displays how many of these came from organic search.  I am hoping someone can give me an example on querying the API with a more complex request.. perhaps including Dimensions, Metrics, Filters.. and then displaying then in rows.  Any help is much appreciated.  Below is my code so far...


function getResults($analytics, $profileId, $first_day, $today) {
   return $analytics->data_ga->get(
       'ga:' . $profileId,
       $first_day,
       $today,
       'ga:visits, ga:organicSearches');
}

//OUTPUT THE RESULTS
function printResults(&$results) {
  if (count($results->getRows()) > 0) {
    $profileName = $results->getProfileInfo()->getProfileName();
    $rows = $results->getRows();
    $visits = $rows[0][0];
    $organic = $rows[0][1];
    print "<h1>$profileName</h1>";
   
    echo '<table border="1" cellpadding="5">';
  
    echo '<tr>';
    echo '<td>Visits</td>';
    echo '<td>Organic</td>';
    echo '</tr>';
    
    echo '<tr>';
    echo '<td>'. $visits . '</td>';
    echo '<td>'. $organic . '</td>';  
    echo '</td>';

    echo '</table>';
   
  } else {
    print '<p>No results found.</p>';
  }
}

John

unread,
Aug 24, 2012, 2:29:02 PM8/24/12
to google-analytics...@googlegroups.com
If you need help with dimensions and metrics, go here. You can build a query and test them there so you can have a better understanding of dimensions and metrics.
Reply all
Reply to author
Forward
0 new messages