/** * Parses the overview data * * @return void */ private function parseOverviewData() { // get aggregates $results = BackendAnalyticsModel::getAggregates($this->startTimestamp, $this->endTimestamp); // get total aggregates $resultsTotal = BackendAnalyticsModel::getAggregatesTotal($this->startTimestamp, $this->endTimestamp); // are there some values? $dataAvailable = false; foreach ($resultsTotal as $data) { if ($data != 0) { $dataAvailable = true; } } // show message if there is no data $this->tpl->assign('dataAvailable', $dataAvailable); // there are some results if (!empty($results)) { // time on site values $timeOnSite = $results['entrances'] == 0 ? 0 : $results['timeOnSite'] / $results['entrances']; $timeOnSiteTotal = $resultsTotal['entrances'] == 0 ? 0 : $resultsTotal['timeOnSite'] / $resultsTotal['entrances']; $timeOnSiteDifference = $timeOnSiteTotal == 0 ? 0 : number_format(($timeOnSite - $timeOnSiteTotal) / $timeOnSiteTotal * 100, 0); if ($timeOnSiteDifference > 0) { $timeOnSiteDifference = '+' . $timeOnSiteDifference; } // pages / visit $pagesPerVisit = $results['visits'] == 0 ? 0 : number_format($results['pageviews'] / $results['visits'], 2); $pagesPerVisitTotal = $resultsTotal['visits'] == 0 ? 0 : number_format($resultsTotal['pageviews'] / $resultsTotal['visits'], 2); $pagesPerVisitDifference = $pagesPerVisitTotal == 0 ? 0 : number_format(($pagesPerVisit - $pagesPerVisitTotal) / $pagesPerVisitTotal * 100, 0); if ($pagesPerVisitDifference > 0) { $pagesPerVisitDifference = '+' . $pagesPerVisitDifference; } // new visits $newVisits = $results['entrances'] == 0 ? 0 : number_format($results['newVisits'] / $results['entrances'] * 100, 0); $newVisitsTotal = $resultsTotal['entrances'] == 0 ? 0 : number_format($resultsTotal['newVisits'] / $resultsTotal['entrances'] * 100, 0); $newVisitsDifference = $newVisitsTotal == 0 ? 0 : number_format(($newVisits - $newVisitsTotal) / $newVisitsTotal * 100, 0); if ($newVisitsDifference > 0) { $newVisitsDifference = '+' . $newVisitsDifference; } // bounces $bounces = $results['entrances'] == 0 ? 0 : number_format($results['bounces'] / $results['entrances'] * 100, 0); $bouncesTotal = $resultsTotal['entrances'] == 0 ? 0 : number_format($resultsTotal['bounces'] / $resultsTotal['entrances'] * 100, 0); $bouncesDifference = $bouncesTotal == 0 ? 0 : number_format(($bounces - $bouncesTotal) / $bouncesTotal * 100, 0); if ($bouncesDifference > 0) { $bouncesDifference = '+' . $bouncesDifference; } // parse data $this->tpl->assign('pageviews', $results['pageviews']); $this->tpl->assign('visitors', $results['visitors']); $this->tpl->assign('pageviews', $results['pageviews']); $this->tpl->assign('pageviewsTotal', $resultsTotal['pageviews']); $this->tpl->assign('pagesPerVisit', $pagesPerVisit); $this->tpl->assign('pagesPerVisitTotal', $pagesPerVisitTotal); $this->tpl->assign('pagesPerVisitDifference', $pagesPerVisitDifference); $this->tpl->assign('timeOnSite', BackendAnalyticsModel::getTimeFromSeconds($timeOnSite)); $this->tpl->assign('timeOnSiteTotal', BackendAnalyticsModel::getTimeFromSeconds($timeOnSiteTotal)); $this->tpl->assign('timeOnSiteDifference', $timeOnSiteDifference); $this->tpl->assign('newVisits', $newVisits); $this->tpl->assign('newVisitsTotal', $newVisitsTotal); $this->tpl->assign('newVisitsDifference', $newVisitsDifference); $this->tpl->assign('bounces', $bounces); $this->tpl->assign('bouncesTotal', $bouncesTotal); $this->tpl->assign('bouncesDifference', $bouncesDifference); } }
/** * Get all needed metrics for certain dates * * @return array * @param int $pageId The id of the page to collect data from. * @param int $startTimestamp The start timestamp for the google call. * @param int $endTimestamp The end timestamp for the google call. */ public static function getDataForPage($pageId, $startTimestamp, $endTimestamp) { // get page $page = BackendModel::getDB(false)->getVar('SELECT page FROM analytics_pages WHERE id = ?', array((int) $pageId)); // init vars $data = array(); $data['hostname'] = SITE_URL; $data['aggregates'] = array(); $data['metrics_per_day'] = array(); $data['sources'] = array(); $data['sources_grouped'] = array(); // get metrics and dimensions $metrics = 'ga:visits'; $dimensions = 'ga:hostname'; // get parameters $parameters = array(); $parameters['max-results'] = 1; $parameters['sort'] = '-ga:visits'; // get results $results = self::getGoogleAnalyticsInstance()->getAnalyticsResults($metrics, mktime(0, 0, 0, 1, 1, 2005), $endTimestamp, $dimensions, $parameters); // loop page results and add hostname to data array foreach ($results['entries'] as $result) { $data['hostname'] = $result['hostname']; } // get metrics $metrics = array('bounces', 'entrances', 'exits', 'newVisits', 'pageviews', 'timeOnPage', 'timeOnSite', 'visits'); $gaMetrics = array(); foreach ($metrics as $metric) { $gaMetrics[] = 'ga:' . $metric; } // get dimensions $dimensions = 'ga:date'; // get parameters $parameters = array(); $parameters['filters'] = 'ga:pagePath==' . $page; // get results $results = self::getGoogleAnalyticsInstance()->getAnalyticsResults($gaMetrics, $startTimestamp, $endTimestamp, $dimensions, $parameters); // get aggregates $data['aggregates'] = $results['aggregates']; // loop page results foreach ($results['entries'] as $result) { // get timestamp $timestamp = gmmktime(12, 0, 0, substr($result['date'], 4, 2), substr($result['date'], 6, 2), substr($result['date'], 0, 4)); // store metrics in correct format $entry = array(); $entry['timestamp'] = $timestamp; // loop metrics foreach ($metrics as $metric) { $entry[$metric] = (int) $result[$metric]; } // add to entries array $data['metrics_per_day'][] = $entry; } // get metrics $metrics = array('bounces', 'entrances', 'exits', 'newVisits', 'pageviews', 'timeOnPage', 'timeOnSite', 'visits'); $gaMetrics = array(); foreach ($metrics as $metric) { $gaMetrics[] = 'ga:' . $metric; } // get dimensions $dimensions = array('ga:source', 'ga:referralPath', 'ga:keyword'); // get parameters $parameters = array(); $parameters['max-results'] = 50; $parameters['filters'] = 'ga:pagePath==' . $page . ';ga:pageviews>0'; $parameters['sort'] = '-ga:pageviews'; // get results $results = self::getGoogleAnalyticsInstance()->getAnalyticsResults($gaMetrics, $startTimestamp, $endTimestamp, $dimensions, $parameters); // loop page results foreach ($results['entries'] as $result) { // store dimension in correct format $entry = array(); if ($result['keyword'] != '(not set)') { $entry['source'] = $result['keyword']; } elseif ($result['source'] == '(direct)') { $entry['source'] = BL::lbl('DirectTraffic'); } elseif ($result['referralPath'] != '(not set)') { $entry['source'] = $result['source'] . $result['referralPath']; } else { $entry['source'] = $result['source']; } // get metrics $entry['pageviews'] = (int) $result['pageviews']; $entry['pages_per_visit'] = $result['visits'] == 0 ? 0 : number_format((int) $result['pageviews'] / $result['visits'], 2); $entry['time_on_site'] = BackendAnalyticsModel::getTimeFromSeconds($result['entrances'] == 0 ? 0 : number_format((int) $result['timeOnSite'] / $result['entrances'], 2)); $entry['new_visits'] = ($result['visits'] == 0 ? 0 : number_format((int) $result['newVisits'] / $result['visits'] * 100, 2)) . '%'; $entry['bounce_rate'] = ($result['entrances'] == 0 ? 0 : number_format((int) $result['bounces'] / $result['entrances'] * 100, 2)) . '%'; // add to entries array $data['sources'][] = $entry; } // set parameters $parameters = array(); $parameters['filters'] = 'ga:pagePath==' . $page; $parameters['sort'] = '-ga:pageviews'; // get results for sources grouped $results = self::getGoogleAnalyticsInstance()->getAnalyticsResults('ga:pageviews', $startTimestamp, $endTimestamp, 'ga:medium', $parameters); // get total pageviews $totalPageviews = isset($results['aggregates']['pageviews']) ? (int) $results['aggregates']['pageviews'] : 0; // loop entries foreach ($results['entries'] as $i => $result) { // add to sources array $data['sources_grouped'][$i]['label'] = $result['medium']; $data['sources_grouped'][$i]['value'] = $result['pageviews']; $data['sources_grouped'][$i]['percentage'] = ($totalPageviews == 0 ? 0 : number_format((int) $result['pageviews'] / $totalPageviews * 100, 2)) . '%'; } // return metrics return $data; }
/** * Get pages * * @return array * @param int $startTimestamp The start timestamp for the cache file. * @param int $endTimestamp The end timestamp for the cache file. */ public static function getPages($startTimestamp, $endTimestamp) { // get data from cache $items = self::getDataFromCacheByType('pages', $startTimestamp, $endTimestamp); // get current action $action = Spoon::get('url')->getAction(); // nothing in cache if ($items === false) { self::redirectToLoadingPage($action); } // reset loop counter for the current action if we got data from cache SpoonSession::set($action . 'Loop', null); // init vars $results = array(); // build pages array foreach ($items as $i => $item) { // build array $results[$i] = array(); $results[$i]['page'] = $item['pagePath']; $results[$i]['page_encoded'] = urlencode($item['pagePath']); $results[$i]['pageviews'] = (int) $item['pageviews']; $results[$i]['pages_per_visit'] = $item['visits'] == 0 ? 0 : number_format((int) $item['pageviews'] / $item['visits'], 2); $results[$i]['time_on_site'] = BackendAnalyticsModel::getTimeFromSeconds($item['entrances'] == 0 ? 0 : number_format((int) $item['timeOnSite'] / $item['entrances'], 2)); $results[$i]['new_visits_percentage'] = ($item['visits'] == 0 ? 0 : number_format((int) $item['newVisits'] / $item['visits'] * 100, 2)) . '%'; $results[$i]['bounce_rate'] = ($item['entrances'] == 0 ? 0 : number_format((int) $item['bounces'] / $item['entrances'] * 100, 2)) . '%'; } // return results return $results; }
/** * Parses the overview data * * @return void */ private function parseOverviewData() { // get aggregates $results = BackendAnalyticsModel::getAggregates($this->startTimestamp, $this->endTimestamp); // get total aggregates $resultsTotal = BackendAnalyticsModel::getAggregatesTotal($this->startTimestamp, $this->endTimestamp); // are there some values? $dataAvailable = false; foreach ($resultsTotal as $data) { if ($data != 0) { $dataAvailable = true; } } // show message if there is no data $this->tpl->assign('dataAvailable', $dataAvailable); // there are some results if (!empty($results)) { // pageviews percentage of total $pageviewsPercentageOfTotal = $results['pageviews'] == 0 ? 0 : number_format($results['allPagesPageviews'] / $results['pageviews'] * 100, 0); // unique pageviews percentage of total $uniquePageviewsPercentageOfTotal = $results['uniquePageviews'] == 0 ? 0 : number_format($results['allPagesUniquePageviews'] / $results['uniquePageviews'] * 100, 0); // time on site values $timeOnSite = $results['entrances'] == 0 ? 0 : $results['timeOnSite'] / $results['entrances']; $timeOnSiteTotal = $resultsTotal['entrances'] == 0 ? 0 : $resultsTotal['timeOnSite'] / $resultsTotal['entrances']; $timeOnSiteDifference = $timeOnSiteTotal == 0 ? 0 : number_format(($timeOnSite - $timeOnSiteTotal) / $timeOnSiteTotal * 100, 0); if ($timeOnSiteDifference > 0) { $timeOnSiteDifference = '+' . $timeOnSiteDifference; } // bounces $bounces = $results['entrances'] == 0 ? 0 : number_format($results['bounces'] / $results['entrances'] * 100, 0); $bouncesTotal = $resultsTotal['entrances'] == 0 ? 0 : number_format($resultsTotal['bounces'] / $resultsTotal['entrances'] * 100, 0); $bouncesDifference = $bouncesTotal == 0 ? 0 : number_format(($bounces - $bouncesTotal) / $bouncesTotal * 100, 0); if ($bouncesDifference > 0) { $bouncesDifference = '+' . $bouncesDifference; } // exits percentage $exitsPercentage = $results['allPagesPageviews'] == 0 ? 0 : number_format($results['exits'] / $results['allPagesPageviews'] * 100, 0); $exitsPercentageTotal = $resultsTotal['pageviews'] == 0 ? 0 : number_format($resultsTotal['exits'] / $resultsTotal['pageviews'] * 100, 0); $exitsPercentageDifference = $exitsPercentageTotal == 0 ? 0 : number_format(($exitsPercentage - $exitsPercentageTotal) / $exitsPercentageTotal * 100, 0); if ($exitsPercentageDifference > 0) { $exitsPercentageDifference = '+' . $exitsPercentageDifference; } // parse data $this->tpl->assign('timeOnSite', BackendAnalyticsModel::getTimeFromSeconds($timeOnSite)); $this->tpl->assign('timeOnSiteTotal', BackendAnalyticsModel::getTimeFromSeconds($timeOnSiteTotal)); $this->tpl->assign('timeOnSiteDifference', $timeOnSiteDifference); $this->tpl->assign('pageviews', $results['pageviews']); $this->tpl->assign('pageviewsPercentageOfTotal', $pageviewsPercentageOfTotal); $this->tpl->assign('uniquePageviews', $results['uniquePageviews']); $this->tpl->assign('uniquePageviewsPercentageOfTotal', $uniquePageviewsPercentageOfTotal); $this->tpl->assign('bounces', $bounces); $this->tpl->assign('bouncesTotal', $bouncesTotal); $this->tpl->assign('bouncesDifference', $bouncesDifference); $this->tpl->assign('exitsPercentage', $exitsPercentage); $this->tpl->assign('exitsPercentageTotal', $exitsPercentageTotal); $this->tpl->assign('exitsPercentageDifference', $exitsPercentageDifference); } }