pastebin

Paste Search Dynamic
Recent pastes
googleanalytics.php
  1. <?php
  2.  
  3. require_once(dirname(dirname(dirname(__file__))).'/lib.php');
  4. require_once(dirname(__file__).'/analytics_api.php');
  5.  
  6. initializePivotX(false);
  7.  
  8. // 14 dagen hits
  9. // totale stats.
  10. // populaire pages, deze week.
  11. // referers, deze week
  12.  
  13.  
  14. $PIVOTX['session']->minLevel(PIVOTX_UL_NORMAL);
  15.  
  16. echo "<h1>Testing Google Analytics Login</h1>";
  17.  
  18. $login = $PIVOTX['config']->get("ga_login");
  19. $password = $PIVOTX['config']->get("ga_password");
  20. $id = 'ga:'.$PIVOTX['config']->get("ga_profileid");
  21.  
  22. echo "<pre>$id: \n";
  23.  
  24. $statistics = array();
  25.  
  26. $api = new analytics_api();
  27. if($api->login($login, $password)) {
  28.  
  29.    
  30.     // totals for 'forever', this month, this week, today.
  31.     $statistics['totals'] = $api->data($id, '', 'ga:visits,ga:pageviews', '', "2005-01-01", false, 15, 1);
  32.    
  33.     $statistics['month'] = $api->data($id, '', 'ga:visits,ga:pageviews');
  34.    
  35.     $startdate = date("Y-m-d", mktime(1,1,1,date('m'),date('d')-7,date('Y')));        
  36.     $statistics['week'] = $api->data($id, '', 'ga:visits,ga:pageviews', '', $startdate, false, 15, 1);
  37.    
  38.     $startdate = date("Y-m-d", mktime(1,1,1,date('m'),date('d'),date('Y')));        
  39.     $statistics['today'] = $api->data($id, '', 'ga:visits,ga:pageviews', '', $startdate, $startdate, 15, 1);
  40.    
  41.     // Viewed pages, this week..
  42.     $startdate = date("Y-m-d", mktime(1,1,1,date('m'),date('d')-7,date('Y')));  
  43.     $data = $api->data($id, 'ga:pagePath', 'ga:pageviews', '', $startdate, false, 10, 1);
  44.    
  45.     foreach($data as $key=>$value) {
  46.         $statistics['pages'][$key] = $value['ga:pageviews'];
  47.     }
  48.    
  49.     // Referers, this week
  50.     $data = $api->data($id, 'ga:source,ga:referralpath', 'ga:pageviews', '', $startdate, false, 50, 1);
  51.    
  52.     foreach($data as $host=>$value) {
  53.         foreach($value as $path=>$value) {
  54.             if ($path=="(not set)") {
  55.                 $path="";
  56.             }
  57.            
  58.             if (strpos($host, 'google')!==false) {
  59.                 $statistics['referers']['google.com'] += $value['ga:pageviews'];            
  60.             } else {
  61.                 $statistics['referers'][$host.$path] = $value['ga:pageviews'];
  62.                
  63.             }
  64.            
  65.         }
  66.     }
  67.      
  68.     // Graph of the past two weeks..
  69.    
  70.     $startdate = date("Y-m-d", mktime(1,1,1,date('m'),date('d')-14,date('Y')));
  71.    
  72.     $data = $api->data($id, 'ga:date', 'ga:visits,ga:pageviews', 'ga:date', $startdate, false, 15, 1);
  73.     // print_r($data);
  74.    
  75.     $visits = array();
  76.     $pageviews = array();
  77.     $labels = array();
  78.    
  79.     $odd=false;
  80.    
  81.     foreach($data as $date=>$point) {
  82.        
  83.         $visits[] = $point['ga:visits'];
  84.         $pageviews[] = $point['ga:pageviews'];
  85.    
  86.         if($odd) {
  87.             $date = (0+substr($date, 4,2)) . "/" . (0+substr($date, 6,2));
  88.             $odd = false;            
  89.         } else {
  90.             $date = "";
  91.             $odd = true;
  92.         }
  93.        
  94.         $labels[] = urlencode($date);
  95.     }
  96.    
  97.     $max = round(max($pageviews) * 1.1);
  98.    
  99.     $labels = implode('|', $labels);
  100.    
  101.     $url = "http://chart.apis.google.com/chart".
  102.         "?cht=lc" .
  103.         "&chs=300x160" .
  104.         "&chxt=x,y" .
  105.         "&chxr=0,0," . count($visits). "|1,0,". $max .
  106.         "&chxl=0:|".$labels .
  107.         "&chdlp=b&" .
  108.         "&chls=2,1,0|2,1,0" .
  109.         "&chdl=Visits|Pageviews,+last+14+days".
  110.         "&chco=6F8082,404F52".
  111.         "&chm=b,9FB0B2,0,1,0|B,AFC0C2,0,1,0".
  112.         "&chds=0," . $max .
  113.         "&chxtc=0,3|1,3" .  // Tickmarks
  114.         "&chd=t:" . implode(",", $visits) . "|" . implode(",", $pageviews);
  115.    
  116.    
  117.     $statistics['chart'] = $url;
  118.    
  119.     save_serialize($PIVOTX['paths']['db_path'].'analytics.php', $statistics);
  120.  
  121. }
  122.  
  123. echo "<p>klaar</p>";
  124.  
  125. debug ("klaar!");
  126. ?>
Parsed in 0.186 seconds