Google Analytics PHP示例
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Google Analytics PHP示例相关的知识,希望对你有一定的参考价值。
Example of using http://code.google.com/p/gapi-google-analytics-php-interface/ to graph google analytics data.
<?php // include the GAPI class from http://code.google.com/p/gapi-google-analytics-php-interface/ require_once 'gapi.class.php'; // login details for the account that has access to analytics reports. // use our caching function above to get some results in a nice array // change 123456789 to your google analytics id. // output the image: output_image($data); function get_analytics_data( $profile_id, $sort=null, $start_date=null, $end_date=null ){ $return = false; // check if the cache item exists. $temp_folder = '/tmp/ga/'; $filename = $temp_folder.$cache_id; } } if(!$return){ // no cache item found, so we grab it via gapi class. $ga = new gapi(ga_web_email,ga_web_pass); $ga->requestReportData($profile_id,$dimensions,$metrics,$sort,null,$start_date,$end_date,null,100); foreach($ga->getResults() as $result){ foreach($dimensions as $d){ $data[$d] = (string)$result; foreach($metrics as $m){ $data[$m] = $result->{'get'.$m}(); } } $return['data'][] = $data; } $return['total'] = $ga->getTotalResults(); foreach($metrics as $m){ $return[$m] = $ga->{'get'.$m}(); } $return['timestamp'] = $ga->getUpdated(); } // save cache item. return $return; } function output_image($data){ $max_y = 500; $width = 640; $height = 200; // work out the width of 1 day $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. // 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'; }else{ // we're starting graph on a saturday or sunday. $color='EFEFEF'; // then we do 5 days white. } // 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. // we're doing 5 days white. } $p = $start_day; $x=0; $points1 = $points2 = ''; foreach($data['data'] as $d){ if($p%7==1){ // every monday put a date. $attr['chxp'] .= ',' . $x; } $points1 .= $d['visits'].','; $points2 .= $d['newVisits'].','; $x++; $p++; } ?> <img src="http://chart.apis.google.com/chart?<?php foreach($attr as $k=>$v) echo $k .'='.urlencode($v).'& ';?>" width="<?php echo $width;?>" height="<?php echo $height;?>" alt="<?php echo $attr['chtt'];?>" /> <?php } ?>
以上是关于Google Analytics PHP示例的主要内容,如果未能解决你的问题,请参考以下文章
Google Analytics API 在示例代码中返回 401
php 将Google Analytics跟踪代码添加到WordPress的简单插件