loadXML($xml);
$entries = $doc->getElementsByTagName('entry');
$i = 0;
$profiles = array();
foreach($entries as $entry)
{
$profiles[$i] = array();
$title = $entry->getElementsByTagName('title');
$profiles[$i]["title"] = $title->item(0)->nodeValue;
$entryid = $entry->getElementsByTagName('id');
$profiles[$i]["entryid"] = $entryid->item(0)->nodeValue;
$properties = $entry->getElementsByTagName('property');
foreach($properties as $property)
{
if (strcmp($property->getAttribute('name'), 'ga:accountId') == 0)
$profiles[$i]["accountId"] = $property->getAttribute('value');
if (strcmp($property->getAttribute('name'), 'ga:accountName') == 0)
$profiles[$i]["accountName"] = $property->getAttribute('value');
if (strcmp($property->getAttribute('name'), 'ga:profileId') == 0)
$profiles[$i]["profileId"] = $property->getAttribute('value');
if (strcmp($property->getAttribute('name'), 'ga:webPropertyId') == 0)
$profiles[$i]["webPropertyId"] = $property->getAttribute('value');
}
$tableId = $entry->getElementsByTagName('tableId');
$profiles[$i]["tableId"] = $tableId->item(0)->nodeValue;
$i++;
}
return $profiles;
}
if ($_REQUEST['token'])
{
// Exchange token for session token
$sessiontoken = get_session_token($_REQUEST['token']);
// Get list of accounts
$accountxml = make_api_call("https://www.google.com/analytics/feeds/accounts/default", $sessiontoken);
// Get an array with the available accounts
$profiles = parse_account_list($accountxml);
$totalviews = 0;
foreach($profiles as $profile)
{
// For each profile, get number of pageviews
$requrl = sprintf("https://www.google.com/analytics/feeds/data?ids=%s&metrics=ga:pageviews&start-date=2007-06-01&end-date=2009-04-21", $profile["tableId"]);
$pagecountxml = make_api_call($requrl, $sessiontoken);
$doc = new DOMDocument();
$doc->loadXML($pagecountxml);
$metrics = $doc->getElementsByTagName("metric");
$views = $metrics->item(0)->getAttribute('value');
$totalviews = $totalviews + $views;
echo $profile["title"] . ": " . number_format($views) . "
";
// echo $output2."
";
}
echo "Total views: " . number_format($totalviews);
}
else // haven't authenticated yet, show authentication link
{
?>
This script shows individual page view counts for each profile in a Google Analytics account, and total number of page views, between June 1, 2007 and April 21, 2009.
Click here to authenticate through Google.