The intention is, to filter the top internal searches for my Site using the Google Analytics API and Zend Framework , so i don’t have to track it myself (so i could maybe use them to Build a Search-Cloud – SEO ..woohoo). But this can basically be modified to fit A LOT of needs.
Well first, unfortunately the Zend Framework Zend_Gdata_Analytics Component is just a proposal at the moment, but it works anyhow using Zend_Gdata. First we need to open an connection aka get a client:
$email = 'user@googlemail.de'; $pass = 'pwpwpwpwpw'; $client = Zend_Gdata_ClientLogin::getHttpClient($email, $pass, "analytics"); $gdClient = new Zend_Gdata($client);
Then we build our Report, if you worked with custom reports this you will be kindaaa familiar with this, basically you work with dimensions and metrics, which represents different kind of values that you can request, combine, filter and search for. The basic Data Operation Stuff, you can find all the Information about the Dimensions and Metrics here and how to work with them (especially how to handle the filter) here. The last parameter (&ids=ga:xxxxxxxx) is the Table ID, you can (for example) se it when you are logged in to analytics ‘https://www.google.com/analytics/reporting/custom?id=xxxxxxx’.
$reportURL = 'https://www.google.com/analytics/feeds/data' .
'?start-date=2010-01-01' .
'&end-date=2010-01-31' .
'&dimensions=ga:pageTitle,ga:pagePath' .
'&metrics=ga:pageviews' .
'&sort=-ga:pageviews' .
'&filters=ga:pagePath%3D~'.urlencode("/?s=").
'&max-results=10' .
'&ids=ga:xxxxxxxx';
$results = $gdClient->getFeed($reportURL);
… and there you go, here is also an short example for a basic xml Output:
$content = new XMLWriter;
$content->openMemory();
$content->startDocument( '1.0" encoding="ISO-8859-1" standalone="yes');
$highest = $results[0]->extensionElements[1]->extensionAttributes["value"]["value"] ;
$content->startElement('searches');
foreach ($results as $rep) {
$content->startElement('search');
$content->writeAttribute('hits',$rep->extensionElements[1]->extensionAttributes["value"]["value"]);
$content->writeAttribute('rank',round($rep->extensionElements[1]->extensionAttributes["value"]["value"]/($highest/7)));
$content->writeCData(str_replace('/finder/1/','',$rep->extensionElements[0]->extensionAttributes["value"]["value"]));
$content->endElement();//e o Search
}
$content->endElement();//e o Searches
}
$content->endDocument();
header('Content-Type: text/xml; charset=iso-8859-1');
print $content->outputMemory();