Skip to content

Instantly share code, notes, and snippets.

@pezholio
Created August 11, 2010 15:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pezholio/519215 to your computer and use it in GitHub Desktop.
Save pezholio/519215 to your computer and use it in GitHub Desktop.
A (slightly hacky) script to return the page title, url and number of pageviews from the Google Analytics API as a tab-separated CSV file. No guarantee that it'll work, but worked for me!
<?php
/* A (slightly hacky) script to return the page title, url and number of pageviews from the Google Analytics API as a tab-separated CSV file. No guarantee that it'll work, but worked for me! */
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"analytics.csv\"");
function xml2array($contents, $get_attributes=1) {
if(!$contents) return array();
if(!function_exists('xml_parser_create')) {
//print "'xml_parser_create()' function not found!";
return array();
}
//Get the XML parser of PHP - PHP must have this module for the parser to work
$parser = xml_parser_create();
xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
xml_parse_into_struct( $parser, $contents, $xml_values );
xml_parser_free( $parser );
if(!$xml_values) return;//Hmm...
//Initializations
$xml_array = array();
$parents = array();
$opened_tags = array();
$arr = array();
$current = &$xml_array;
//Go through the tags.
foreach($xml_values as $data) {
unset($attributes,$value);//Remove existing values, or there will be trouble
//This command will extract these variables into the foreach scope
// tag(string), type(string), level(int), attributes(array).
extract($data);//We could use the array by itself, but this cooler.
$result = '';
if($get_attributes) {//The second argument of the function decides this.
$result = array();
if(isset($value)) $result['value'] = $value;
//Set the attributes too.
if(isset($attributes)) {
foreach($attributes as $attr => $val) {
if($get_attributes == 1) $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
/** :TODO: should we change the key name to '_attr'? Someone may use the tagname 'attr'. Same goes for 'value' too */
}
}
} elseif(isset($value)) {
$result = $value;
}
//See tag status and do the needed.
if($type == "open") {//The starting of the tag '<tag>'
$parent[$level-1] = &$current;
if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
$current = &$current[$tag];
} else { //There was another element with the same tag name
if(isset($current[$tag][0])) {
array_push($current[$tag], $result);
} else {
$current[$tag] = array($current[$tag],$result);
}
$last = count($current[$tag]) - 1;
$current = &$current[$tag][$last];
}
} elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
//See if the key is already taken.
if(!isset($current[$tag])) { //New Key
$current[$tag] = $result;
} else { //If taken, put all things inside a list(array)
if((is_array($current[$tag]) and $get_attributes == 0)//If it is already an array...
or (isset($current[$tag][0]) and is_array($current[$tag][0]) and $get_attributes == 1)) {
array_push($current[$tag],$result); // ...push the new element into that array.
} else { //If it is not an array...
$current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
}
}
} elseif($type == 'close') { //End of tag '</tag>'
$current = &$parent[$level-1];
}
}
return($xml_array);
}
$email = ""; // Your analytics username
$password = ""; // Your analytics password
$url = ""; // The URL of your site (Just makes readability easier)
$id = ""; // Your Google Analytics ID (e.g. ga:1234)
$start = ""; // Start date in yyyy-mm-dd format
$end = ""; // End date in yyyy-mm-dd format
$max = ""; // Maximum results to show
$data = array(
'accountType' => 'GOOGLE',
'Email' => $email,
'Passwd' => $password,
'service' => 'analytics',
'source' => ''
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
$auth = '';
if($info['http_code'] == 200) {
preg_match('/Auth=(.*)/', $output, $matches);
if(isset($matches[1])) {
$auth = $matches[1];
}
}
$ch = curl_init();
$headers = array("Authorization: GoogleLogin auth=$auth");
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/analytics/feeds/data?ids=". urlencode($id) ."&dimensions=ga%3ApageTitle%2Cga%3ApagePath&metrics=ga%3Apageviews&segment=gaid%3A%3A-1&sort=-ga%3Apageviews&start-date=". $start ."&end-date=". $end ."&max-results=". $max);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$xml = xml2array($output);
echo "Page Title URL Number of Pageviews\r\n";
foreach ($xml['feed']['entry'] as $entry) {
echo $entry['dxp:dimension'][0]['attr']['value'] ." ". $url . $entry['dxp:dimension'][1]['attr']['value'] ." ". $entry['dxp:metric']['attr']['value'] ."\r\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment