/**
  * Generate the dashboard with the google analytics graphs
  *
  * @return void
  */
 public function indexAction()
 {
     $params = Zend_Registry::get('params');
     $this->view->currentPage = 'dashboard';
     try {
         $email = $params->googleAnalytics->username;
         $password = $params->googleAnalytics->password;
         $profileID = $params->googleAnalytics->homeletProfileID;
         $client = Zend_Gdata_ClientLogin::getHttpClient($email, $password, Zend_Gdata_Analytics::AUTH_SERVICE_NAME);
         $service = new Zend_Gdata_Analytics($client);
         $firstOfMonth = date("Y-m-d", strtotime(date('m') . '/01/' . date('Y') . ' 00:00:00'));
         $today = date("Y-m-d");
         $this->view->startDate = date("d/m/Y", strtotime($firstOfMonth));
         $this->view->today = date("d/m/Y", strtotime($today));
         // LINE CHART for visitors this month
         $query = $service->newDataQuery()->setProfileId($profileID)->addDimension(Zend_Gdata_Analytics_DataQuery::DIMENSION_DAY)->addMetric(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS)->setStartDate($firstOfMonth)->setEndDate($today)->setSort(Zend_Gdata_Analytics_DataQuery::DIMENSION_DAY, false)->setMaxResults(25);
         $result = $service->getDataFeed($query);
         $chartArray = array();
         foreach ($result as $row) {
             $chartArray[] = (int) $row->getValue('ga:visits')->getValue();
         }
         $visitorsChart = new gLineChart(800, 200);
         $visitorsChart->addDataSet($chartArray);
         $visitorsChart->setLegend(array('Site Visitors'));
         $visitorsChart->setColors(array('FF6D1B'));
         $visitorsChart->setVisibleAxes(array('x', 'y'));
         $visitorsChart->setDataRange(0, 7000);
         $visitorsChart->setLegendPosition('t');
         $visitorsChart->addAxisRange(0, 0, count($chartArray), 2);
         $visitorsChart->addAxisRange(1, 0, 7000);
         $visitorsChart->setGridLines(7, 1000);
         $this->view->visitorGraphUrl = $visitorsChart->getUrl();
         // PIE CHART for sources
         $query = $service->newDataQuery()->setProfileId($profileID)->addDimension(Zend_Gdata_Analytics_DataQuery::DIMENSION_MEDIUM)->addMetric(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS)->setStartDate($firstOfMonth)->setEndDate($today)->setSort(Zend_Gdata_Analytics_DataQuery::DIMENSION_MEDIUM, false)->setMaxResults(500);
         $result = $service->getDataFeed($query);
         $chartArray = array();
         $colorArray = array();
         $labelArray = array();
         foreach ($result as $row) {
             $chartArray[] = (int) $row->getValue('ga:visits')->getValue();
             switch ((string) $row->getValue('ga:medium')->getValue()) {
                 case '(none)':
                     $medium = 'Direct';
                     $color = 'FF6F1C';
                     break;
                 case 'email':
                     $medium = 'Email Campaign';
                     $color = 'FDBB30';
                     break;
                 case 'organic':
                     $medium = 'Search Engines';
                     $color = '7AC142';
                     break;
                 case 'referral':
                     $medium = 'Referring Site';
                     $color = 'E60E64';
                     break;
                 default:
                     $medium = (string) $row->getValue('ga:medium')->getValue();
             }
             $labelArray[] = $medium;
             $colorArray[] = $color;
         }
         $sourcesChart = new gPieChart(380, 200);
         $sourcesChart->addDataSet($chartArray);
         $sourcesChart->setLegend($labelArray);
         $sourcesChart->setColors($colorArray);
         $this->view->sourcesGraphUrl = $sourcesChart->getUrl();
         // Table of top 20 keywords
         $query = $service->newDataQuery()->setProfileId($profileID)->addDimension(Zend_Gdata_Analytics_DataQuery::DIMENSION_KEYWORD)->addMetric(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS)->setStartDate($firstOfMonth)->setEndDate($today)->setSort(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS, true)->setMaxResults(20);
         $result = $service->getDataFeed($query);
         $keywordArray = array();
         foreach ($result as $row) {
             $keyword = (string) $row->getValue('ga:keyword')->getValue();
             if ($keyword == '(not set)') {
                 $keyword = '-';
             }
             $keywordArray[$keyword] = (int) $row->getValue('ga:visits')->getValue();
         }
         $this->view->keywordList = $keywordArray;
         // PIE CHART for new/returning visitors
         $query = $service->newDataQuery()->setProfileId($profileID)->addDimension(Zend_Gdata_Analytics_DataQuery::DIMENSION_VISITOR_TYPE)->addMetric(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS)->setStartDate($firstOfMonth)->setEndDate($today)->setSort(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS, true)->setMaxResults(2);
         $result = $service->getDataFeed($query);
         $chartArray = array();
         $colorArray = array();
         $labelArray = array();
         foreach ($result as $row) {
             $chartArray[] = (int) $row->getValue('ga:visits')->getValue();
             $labelArray[] = (string) $row->getValue('ga:visitorType')->getValue();
             switch ((string) $row->getValue('ga:visitorType')->getValue()) {
                 case 'Returning Visitor':
                     $color = 'FF6F1C';
                     break;
                 case 'New Visitor':
                     $color = '7AC142';
                     break;
                 default:
             }
             $colorArray[] = $color;
         }
         $newReturningChart = new gPieChart(380, 200);
         $newReturningChart->addDataSet($chartArray);
         $newReturningChart->setLegend($labelArray);
         $newReturningChart->setColors($colorArray);
         $this->view->newReturningUrl = $newReturningChart->getUrl();
     } catch (Exception $e) {
         $this->view->statsUnavailable = true;
     }
 }
Exemplo n.º 2
0
<?php

require_once "gChart.php";
$lineChart = new gLineChart($_GET['width'], $_GET['height']);
$lineChart->addDataSet(array(120, 131, 135, 160, 129, 22, 190));
$lineChart->setLegend(array('Nice figures'));
$lineChart->setColors(array('ED237A'));
$lineChart->setVisibleAxes(array('x', 'y'));
$lineChart->setDataRange(0, 200);
$lineChart->setLegendPosition('t');
// axisnr, from, to, step
$lineChart->addAxisRange(0, 0, 28, 7);
$lineChart->addAxisRange(1, 0, 200);
$lineChart->setGridLines(25, 10);
$lineChart->renderImage($_GET['post']);