public function admin_index()
 {
     $this->set('title', __('Dashboard'));
     //$this->set('description', __('Manage Quiz'));
     $practiceTests = $this->PracticeTest->find('all', array('order' => array('PracticeTest.created' => 'DESC'), 'limit' => 20));
     $this->set('practiceTests', $practiceTests);
     $FavouritePracticeTests = $this->PracticeTest->find('all', array('order' => array('PracticeTest.avg' => 'DESC'), 'limit' => 10));
     $this->set('FavouritePracticeTests', $FavouritePracticeTests);
     $users = $this->User->find('all', array('order' => array('User.created' => 'DESC'), 'limit' => 10));
     $this->set('users', $users);
     //google analytics
     if (Configure::read('GA.email') != '' && Configure::read('GA.password') != '') {
         try {
             // construct the class
             $oAnalytics = new analytics(Configure::read('GA.email'), Configure::read('GA.password'));
             // set it up to use caching
             $oAnalytics->useCache();
             //$oAnalytics->setProfileByName(Configure::read('GA.domain'));
             $oAnalytics->setProfileById(Configure::read('GA.profile_id'));
             // set the date range
             //$oAnalytics->setMonth(date('n'), date('Y'));
             $oAnalytics->setDateRange(date('Y-m-d', strtotime('-1 week')), date('Y-m-d'));
             //echo '<pre>';
             // print out visitors for given period
             //print_r($oAnalytics->getVisitors());
             // print out pageviews for given period
             //print_r($oAnalytics->getPageviews());
             // use dimensions and metrics for output
             // see: http://code.google.com/intl/nl/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html
             // print_r($oAnalytics->getData(array(   'dimensions' => 'ga:keyword',
             // 'metrics'    => 'ga:visits',
             // 'sort'       => 'ga:keyword')));
             //print_r($oAnalytics->getData(array('dimensions' => 'ga:visitorType','metrics'    => 'ga:newVisits')));
             $visits = $oAnalytics->getVisitors();
             $views = $oAnalytics->getPageviews();
             /* build tables */
             if (count($visits)) {
                 $visits = $this->array_filter_recursive($visits);
                 $views = $this->array_filter_recursive($views);
                 foreach ($visits as $day => $visit) {
                     $flot_datas_visits[] = '[' . $day . ',' . $visit . ']';
                     $flot_datas_views[] = '[' . $day . ',' . $views[$day] . ']';
                 }
                 $flot_data_visits = '[' . implode(',', $flot_datas_visits) . ']';
                 $flot_data_views = '[' . implode(',', $flot_datas_views) . ']';
             }
             $this->set(compact('flot_data_visits', 'flot_data_views'));
         } catch (Exception $e) {
             //echo 'Caught exception: ' . $e->getMessage();
         }
     }
 }
Example #2
0
<?php

// session_start for caching
session_start();
require 'analytics.class.php';
try {
    // construct the class
    $oAnalytics = new analytics('[username]', '[password]');
    // set it up to use caching
    $oAnalytics->useCache();
    $oAnalytics->setProfileByName('[Google analytics accountname]');
    // or $oAnalytics->setProfileById('ga:123456');
    // set the date range
    $oAnalytics->setMonth(date('n'), date('Y'));
    // or $oAnalytics->setDateRange('YYYY-MM-DD', 'YYYY-MM-DD');
    echo '<pre>';
    // print out visitors for given period
    print_r($oAnalytics->getVisitors());
    // print out pageviews for given period
    print_r($oAnalytics->getPageviews());
    // use dimensions and metrics for output
    // see: http://code.google.com/intl/nl/apis/analytics/docs/gdata/gdataReferenceDimensionsMetrics.html
    print_r($oAnalytics->getData(array('dimensions' => 'ga:keyword', 'metrics' => 'ga:visits', 'sort' => 'ga:keyword')));
} catch (Exception $e) {
    echo 'Caught exception: ' . $e->getMessage();
}
		public function display_results($email, $password, $profile_id) {
		
			
			require_once 'lib/analytics.class.php';
			
			// session_start for caching
			session_start();
			
			try {
			    
			    // construct the class
			    $oAnalytics = new analytics($email, $password);
			    
			    // set it up to use caching
			    $oAnalytics->useCache();
			    
			    //$oAnalytics->setProfileByName('[Google analytics accountname]');
			    $oAnalytics->setProfileById('ga:'.$profile_id);
			    
			    // set the date range
			    $last_month = date("Y-m-d", strtotime('today - 30 days'));
			    $today = date("Y-m-d", strtotime('today'));
			    //$oAnalytics->setMonth(date('n'), date('Y'));
			    $oAnalytics->setDateRange($last_month, $today);
			    
			    $wrapper = new XMLElement('div');
			    
			    $graph = extension_dashboard_analytics::buildChart($oAnalytics);
				$wrapper->appendChild($graph);
			    
			    $info = new XMLElement('div');
			    $info->setAttribute('class', 'info');
			    $info_header = new XMLElement('h4', 'Quick Information');
			    $dl_results = new XMLElement('dl');
			    
			    //Total Pageviews
			    $dt_pageviews = new XMLElement('dt', 'Pageviews');
			    $dd_pageviews = new XMLElement('dd', array_sum($oAnalytics->getPageviews()));
			    
			    $dl_results->appendChild($dt_pageviews);
			    $dl_results->appendChild($dd_pageviews);
			    
			    //Total Visits
			    $dt_visits = new XMLElement('dt', 'Visits');
			    $dd_visits = new XMLElement('dd', array_sum($oAnalytics->getVisitors()));
			    
			    $dl_results->appendChild($dt_visits);
			    $dl_results->appendChild($dd_visits);
			    
			    //Pages/Visit
			    $pages_visits = $oAnalytics->getData(
			     	array('metrics'=> urlencode('ga:pageviewsPerVisit'))
			    );
		     	$dt_pages_visits = new XMLElement('dt', 'Pages per Visit');
		     	$dd_pages_visits = new XMLElement('dd',  round(array_sum($pages_visits),2));
		     	$dl_results->appendChild($dt_pages_visits);
		     	$dl_results->appendChild($dd_pages_visits);
			    
			    $bounce_rate = $oAnalytics->getData(
			    	array('metrics'=> urlencode('ga:visitBounceRate'))
			    );
			    $dt_bounce_rate = new XMLElement('dt', 'Bounce Rate');
			    $dd_bounce_rate = new XMLElement('dd', round(array_sum($bounce_rate),2).'%');
			    $dl_results->appendChild($dt_bounce_rate );
			    $dl_results->appendChild($dd_bounce_rate);
			    
			    //% New Visits
			    $new_visits = $oAnalytics->getData(
			    	array('metrics'=> urlencode('ga:percentNewVisits'))
			    );
			  	$dt_new_visits = new XMLElement('dt', '% New Visits');
			  	$dd_new_visits = new XMLElement('dd',  round(array_sum($new_visits),2).'%');
			  	$dl_results->appendChild($dt_new_visits);
			  	$dl_results->appendChild($dd_new_visits);
			  	
			  	
			  	//Avg Time on Site
			  	$average_time = $oAnalytics->getData(
			  		array('metrics'=> urlencode('ga:avgTimeOnSite'))
			  	);
		  		$dt_average_time = new XMLElement('dt', 'Avg. Time on Site');
		  		$dd_average_time = new XMLElement('dd',  extension_dashboard_analytics::sec2hms(round(array_sum($average_time),0)));
		  		$dl_results->appendChild($dt_average_time);
		  		$dl_results->appendChild($dd_average_time);
			  	

				$search_terms = new XMLElement('div');
			    $search_terms->setAttribute('class', 'terms');
			    
			    //Search Terms
			    $terms_head = new XMLElement('h4', 'Top Keywords');
			    $terms = new XMLElement('ol');
			    $keywords = array_keys($oAnalytics->getSearchWords());

			    $count = 0;
			    foreach($keywords as $keyword) {	
			    	$item = new XMLElement('li', $keyword);
			    	$terms->appendChild($item);
			    	$count++;
			    	if ($count == 10) break;
			    }
			
			    $info->appendChild($info_header);
			    $info->appendChild($dl_results);
			
			    $search_terms->appendChild($terms_head);
			    $search_terms->appendChild($terms);
			
			    $wrapper->appendChild($info);
			$wrapper->appendChild($search_terms);

			    return $wrapper;

			    
			} catch (Exception $e) { 
			   
			   $info = new XMLElement('div');
			   $info->setAttribute('class', 'info');
			   $info_header = new XMLElement('h4', 'No data found. Check your account details.');
			   $info->appendChild($info_header);
			   $info_header->appendChild(new XMLElement('p', '<code>'.(string)$e->getMessage().'</code>'));
			   return $info;

			   
			} 
				
		}