/** * Fetches data from google analytics * * @param string $profileId * @param string $startDate * @param string $endDate * @return array */ private function requestReportData($profileId, $startDate, $endDate) { $data = array(); $query = $this->_analytics->newDataQuery(); $query->setProfileId($profileId)->addMetric(Zend_Gdata_Analytics_DataQuery::METRIC_PAGEVIEWS)->addMetric(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS)->setStartDate($startDate)->setEndDate($endDate)->setMaxResults(50); $results = $this->_analytics->getDataFeed($query); foreach ($results as $row) { $data['visits'] = $row->getMetric('ga:visits'); $data['page_views'] = $row->getMetric('ga:pageviews'); } return $data; }
public function indexAction() { $client = Zend_Gdata_ClientLogin::getHttpClient($this->_googleID, $this->_googlePassword, Zend_Gdata_Analytics::AUTH_SERVICE_NAME); $service = new Zend_Gdata_Analytics($client); $dimensions = array(Zend_Gdata_Analytics_DataQuery::DIMENSION_MEDIUM, Zend_Gdata_Analytics_DataQuery::DIMENSION_SOURCE, Zend_Gdata_Analytics_DataQuery::DIMENSION_BROWSER_VERSION, Zend_Gdata_Analytics_DataQuery::DIMENSION_MONTH); $query = $service->newDataQuery()->setProfileId('456871')->addMetric(Zend_Gdata_Analytics_DataQuery::METRIC_BOUNCES)->addMetric(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS)->setStartDate('2011-12-01')->setEndDate('2011-12-31')->addSort(Zend_Gdata_Analytics_DataQuery::METRIC_VISITS, true)->addSort(Zend_Gdata_Analytics_DataQuery::METRIC_BOUNCES, false)->setMaxResults(50); foreach ($dimensions as $dim) { $query->addDimension($dim); } $result = $service->getDataFeed($query); $results = array(); foreach ($result as $row) { $data = array('source' => $row->getDimension(Zend_Gdata_Analytics_DataQuery::DIMENSION_SOURCE), 'medium' => $row->getDimension('ga:medium'), 'visits' => $row->getMetric('ga:visits'), 'bounce' => $row->getValue('ga:bounces')); $results[] = $data; } $this->view->result = $results; }
/** * 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; } }