Converting Google Analytics API XML data into an array with PHPConverting Google Analytics API XML data into an array with PHP

Posted April 28th, 2009 in PHP

This post updates my Google Analytics API PHP class again with a new method which can be used to get data from any dimension and metric and return it as an associative array. The full class, which I will continue to update over the next few days, can be downloaded here.

The new method

The method accepts parameters for the profile id, the dimension(s) to use, the metric(s) to use and so on. The downloadable version of the full class has this documented inline using phpdoc style documentation. After the code example below is some example calls and associated output.

public function data($id, $dimension, $metric, $sort = false, $start = false, $end = false, $max_results = 10, $start_index = 1) {
   
    if(!$sort) $sort = "-$metric";
    if(!$start) $start = date('Y-m-d', strtotime('1 month ago'));
    if(!$end) $end = date('Y-m-d', strtotime('yesterday'));
           
    $xml = $this->call("https://www.google.com/analytics/feeds/data?ids=$id&dimensions=$dimension&metrics=$metric&sort=$sort&start-date=$start&end-date=$end&max-results=$max_results&start-index=$start_index");
   
    if(!$xml) {
        return false;
    }
   
    $dom = new DOMDocument();
    $dom->loadXML($xml);

    $entries = $dom->getElementsByTagName('entry');
    $data = array();
    foreach($entries as $entry) {
   
        $index = array();
        foreach($entry->getElementsByTagName('dimension') as $dimension) {
            $index[] = $dimension->getAttribute('value');
        }
   
        switch(count($index)) {
   
            case 1:
                foreach($entry->getElementsByTagName('metric') as $metric) {
                    $data[$index[0]][$metric->getAttribute('name')] = $metric->getAttribute('value');
                }
            break;
       
            case 2:
                foreach($entry->getElementsByTagName('metric') as $metric) {
                    $data[$index[0]][$index[1]][$metric->getAttribute('name')] = $metric->getAttribute('value');
                }
            break;
       
            case 3:
                foreach($entry->getElementsByTagName('metric') as $metric) {
                    $data[$index[0]][$index[1]][$index[2]][$metric->getAttribute('name')] = $metric->getAttribute('value');
                }
            break;
   
        }
           
    }
   
    return $data;
   
}

Getting pagePath by pageviews and uniquePageviews

To get the pagePath (e.g. /article/mysql/delete-all-data-mysql/) and show the pageviews and uniquePageviews metrics, make the following call:

$api = new analytics_api();
if($api->login($login, $password)) {
    $data = $api->data($id, 'ga:pagePath', 'ga:pageviews,ga:uniquePageviews');
}

Doing print_r() on $data will show this (the example output below just shows the first three results):

Array
(
    [/article/apache/restart-apache/] => Array
        (
            [ga:pageviews] => 2561
            [ga:uniquePageviews] => 2455
        )

    [/article/mysql/delete-all-data-mysql/] => Array
        (
            [ga:pageviews] => 2512
            [ga:uniquePageviews] => 2377
        )

    [/jquery-get-set-form-values/] => Array
        (
            [ga:pageviews] => 2437
            [ga:uniquePageviews] => 2260
        )

)

Getting browsers and versions by visits and pageviews

Example code:

$api = new analytics_api();
if($api->login($login, $password)) {
    $data = $api->data($id, 'ga:browser,ga:browserVersion', 'ga:visits,ga:pageviews', false, false, false, 10);
}

Again, just a selection of the actual results from print_r:

Array
(
    [Internet Explorer] => Array
        (
            [7.0] => Array
                (
                    [ga:visits] => 14202
                    [ga:pageviews] => 17157
                )

            [6.0] => Array
                (
                    [ga:visits] => 5063
                    [ga:pageviews] => 6273
                )

            [8.0] => Array
                (
                    [ga:visits] => 2734
                    [ga:pageviews] => 3457
                )

        )

)

Download the code

The full code for the class including further documentation about the parameters etc can be downloaded here. I will be making further updates to that class over the coming week and that location will always contain the most up to date version.

Related posts:

Comments

blog comments powered by Disqus