Example #1
0
 /**
  * Get data from analytics
  *
  * @return	void
  * @param	int $startTimestamp			The start timestamp for the data to collect.
  * @param	int $endTimestamp			The end timestamp for the data to collect.
  * @param	bool[optional] $force		Force getting data. Don't rely on cache.
  * @param	string[optional] $page		The page to get data for.
  * @param	string[optional] $pageId	The id of the page to get data for.
  * @param	string[optional] $filename	The name of the cache file.
  */
 private function getData($startTimestamp, $endTimestamp, $force = false, $page = 'all', $pageId = null, $filename = null)
 {
     // try
     try {
         // get data from cache
         $data = BackendAnalyticsModel::getDataFromCache($startTimestamp, $endTimestamp);
         // nothing in cache - fetch from google and set cache
         if (!isset($data['aggregates']) || $force) {
             $data['aggregates'] = BackendAnalyticsHelper::getAggregates($startTimestamp, $endTimestamp);
         }
         // nothing in cache - fetch from google and set cache
         if (!isset($data['aggregates_total']) || $force) {
             $data['aggregates_total'] = BackendAnalyticsHelper::getAggregates(mktime(0, 0, 0, 1, 1, 2005), mktime(0, 0, 0));
         }
         // nothing in cache - fetch from google and set cache
         if (!isset($data['metrics_per_day']) || $force) {
             $data['metrics_per_day']['entries'] = BackendAnalyticsHelper::getMetricsPerDay($startTimestamp, $endTimestamp);
         }
         // traffic sources, top keywords and top referrals on index page
         if ($page == 'all' || $page == 'index') {
             // nothing in cache - fetch from google and set cache
             if (!isset($data['traffic_sources']) || $force) {
                 $data['traffic_sources']['entries'] = BackendAnalyticsHelper::getTrafficSourcesGrouped(array('pageviews'), $startTimestamp, $endTimestamp, 'pageviews');
             }
             // nothing in cache
             if (!isset($data['top_keywords']) || $force) {
                 // fetch from google and use a safe limit
                 $gaResults = BackendAnalyticsHelper::getKeywords('pageviews', $startTimestamp, $endTimestamp, 'pageviews', 50);
                 // set cache
                 $data['top_keywords']['entries'] = $gaResults['entries'];
             }
             // nothing in cache
             if (!isset($data['top_referrals']) || $force) {
                 // fetch from google and use a safe limit
                 $gaResults = BackendAnalyticsHelper::getReferrals('pageviews', $startTimestamp, $endTimestamp, 'pageviews', 50);
                 // init vars
                 $topReferrals = array();
                 // add entries to items
                 foreach ($gaResults['entries'] as $entry) {
                     $topReferrals[] = array('referrer' => $entry['source'] . $entry['referralPath'], 'pageviews' => $entry['pageviews']);
                 }
                 // set cache
                 $data['top_referrals']['entries'] = $topReferrals;
             }
         }
         // top pages on index and content page
         if ($page == 'all' || $page == 'index' || $page == 'content') {
             // nothing in cache
             if (!isset($data['top_pages']) || $force) {
                 // fetch from google and use a safe limit
                 $gaResults = BackendAnalyticsHelper::getPages('pageviews', $startTimestamp, $endTimestamp, 'pageviews', 50);
                 // set cache
                 $data['top_pages']['entries'] = $gaResults['entries'];
             }
         }
         // top exit pages on content page
         if ($page == 'all' || $page == 'content') {
             // nothing in cache
             if (!isset($data['top_exit_pages']) || $force) {
                 // fetch from google
                 $gaResults = BackendAnalyticsHelper::getPages(array('exits', 'pageviews'), $startTimestamp, $endTimestamp, 'exits', 50);
                 // set cache
                 $data['top_exit_pages']['entries'] = $gaResults['entries'];
             }
         }
         // top exit pages on all pages page
         if ($page == 'all' || $page == 'all_pages') {
             // nothing in cache
             if (!isset($data['pages']) || $force) {
                 // fetch from google
                 $gaResults = BackendAnalyticsHelper::getPages(array('bounces', 'entrances', 'exits', 'newVisits', 'pageviews', 'timeOnSite', 'visits'), $startTimestamp, $endTimestamp, 'pageviews', 50);
                 // set cache
                 $data['pages']['entries'] = $gaResults['entries'];
                 $data['pages']['attributes'] = array('totalResults' => $gaResults['totalResults']);
             }
         }
         // exit pages on exit pages page
         if ($page == 'all' || $page == 'exit_pages') {
             // nothing in cache
             if (!isset($data['exit_pages']) || $force) {
                 // fetch from google
                 $gaResults = BackendAnalyticsHelper::getExitPages(array('bounces', 'entrances', 'exits', 'newVisits', 'pageviews', 'timeOnSite', 'visits'), $startTimestamp, $endTimestamp, 'exits', 50);
                 // set cache
                 $data['exit_pages']['entries'] = $gaResults['entries'];
             }
         }
         // detail page
         if ($page == 'detail_page') {
             // nothing in cache
             if (!isset($data['page' . $pageId]) || $force) {
                 // fetch from google
                 $gaResults = BackendAnalyticsHelper::getDataForPage($pageId, $startTimestamp, $endTimestamp);
                 // set cache
                 $data['page_' . $pageId] = $gaResults;
             }
         }
         // update cache file
         BackendAnalyticsModel::writeCacheFile($data, $startTimestamp, $endTimestamp);
     } catch (Exception $e) {
         // set file content to indicate something went wrong if needed
         if (isset($filename)) {
             SpoonFile::setContent($filename, 'error');
         } else {
             throw new SpoonException('Something went wrong while getting data.');
         }
     }
     // remove temporary file if needed
     if (isset($filename)) {
         SpoonFile::setContent($filename, 'done');
     }
 }