Google Analytics: PHP API

Playing around with the Google Analytics data export API using the wonderful GAPI from: http://code.google.com/p/gapi-google-analytics-php-interface/ 

Here is a list of Dimensions and Metrics that are available: http://code.google.com/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html

 

($EXTENDED_MAP_LENGTH * $EXTENDED_MAP_LENGTH) – 1) {
$chartData .= “..”;
} else if ($scaledVal < 0) { $chartData .= '__'; } else { $quotient = floor($scaledVal / $EXTENDED_MAP_LENGTH); $remainder = $scaledVal - $EXTENDED_MAP_LENGTH * $quotient; $chartData .= $EXTENDED_MAP[$quotient] + $EXTENDED_MAP[$remainder]; } } return $chartData; } function output_image($data){ $max_y = 900; $width = 690; $height = 250; $attr=array(); $days = count($data['data'])-1; // work out the width of 1 day $day_width_percent = round((100 / $days)/100,4); // 15% will be 0.15 1% will be 0.01 $half_day_width = round($day_width_percent/2,4); $attr['chxl'] = '1:'; // the data point label text. $attr['chxp'] = '1'; // the data point label positions. $attr['chxr'] = '0,0,'.$max_y.'|1,0,'.$days; // the min/max data range to graph over the space of the image. $attr['chxt'] = 'y,x'; // ? $attr['chs'] = $width.'x'.$height; // width x height of the image. $attr['cht'] = 'lc'; // type of graph $attr['chco'] = '3D7930,FF9900'; // colors of the graph lines. $attr['chds'] = '0,'.$max_y.',0,'.$max_y.''; // data range of each data set (0 to 500 each) $attr['chdl'] = 'Visits|New Visits'; // labels for each data set $attr['chg'] = (100/$days) . ',' . ((100/$max_y)*100) . ',4,1,-'.($half_day_width*100).',0'; // (vert,horiz) how many step lines. $attr['chls'] = '2,4,0|1'; // ?something about size of lines maybe? $attr['chtt'] = 'Visits vs New Visits over past '.$data['total'].' days.'; // graph label at the top. // graph stripes. every saturday and sunday ? // work out what day the graph starts on. $first = current($data['data']); $start_day = date('N',strtotime($first['date'])); // how big is the first bar? // highlight white from first bar to first Saturday (6) $attr['chf'] = 'c,ls,0'; $number_of_days = 6 - $start_day; if($number_of_days>0){
// we start with a white bar, because we’re starting graph during the week.
$color=’FFFFFF’;
$attr[‘chf’] .= ‘,’.$color.’,’.(round($number_of_days*$day_width_percent,5)-$half_day_width);
}else{
// we’re starting graph on a saturday or sunday.
$color=’EFEFEF’;
$attr[‘chf’] .= ‘,’.$color.’,’.(round((8-6)*$day_width_percent,5)-$half_day_width);
// then we do 5 days white.
$attr[‘chf’] .= ‘,FFFFFF,’.round(5*$day_width_percent,5);
}
// loop over for the remaining days, every 7 days different colour.
for($x=0;$x<=$days;$x+=7){ // we're doing 2 days dark for weekend. $attr['chf'] .= ',EFEFEF,'.round(2*$day_width_percent,5); // we're doing 5 days white. $attr['chf'] .= ',FFFFFF,'.round(5*$day_width_percent,5); } $p = $start_day; $x=0; $label_int=7; $label_day=1; if($data['total'] < 13){ $label_int = 1; $label_day = 0; } $points1 = $points2 = array(); foreach($data['data'] as $d){ if($p%$label_int==$label_day){ // every monday put a date. $attr['chxl'] .= '|' . date('DjM',strtotime($d['date'])); $attr['chxp'] .= ',' . $x; } $points1[]= $d['visits']; $points2[]= $d['newVisits']; $x++; $p++; } $attr['chd'] = 't:' . implode($points1,',') . '|' . implode($points2,','); ?>
Show Data & Example PHP Code »

 

Operating Systems of Visitors over past 30 days:


URLVisitors

30)break;
?>

BrowserPage ViewsUnique Page ViewsVisits

Total:

Data updated:

Leave a Reply

Your email address will not be published. Required fields are marked *