Пример #1
0
 /**
  * Set start and end timestamp needed to collect analytics data
  *
  * @return	void
  */
 private function setDates()
 {
     // process
     BackendAnalyticsHelper::setDates();
     // get timestamps from session and set
     $this->startTimestamp = SpoonSession::get('analytics_start_timestamp');
     $this->endTimestamp = SpoonSession::get('analytics_end_timestamp');
 }
 /**
  * Get data
  */
 private function getData()
 {
     try {
         BackendAnalyticsHelper::getRecentReferrers();
         BackendAnalyticsHelper::getRecentKeywords();
     } catch (Exception $e) {
         $this->output(self::OK, array('status' => 'error'), 'Something went wrong while getting traffic sources.');
     }
 }
 /**
  * Get data
  */
 private function getData()
 {
     try {
         BackendAnalyticsHelper::getRecentReferrers();
         BackendAnalyticsHelper::getRecentKeywords();
     } catch (Exception $e) {
         throw new SpoonException('Something went wrong while getting dashboard data.');
     }
 }
 /**
  * Get data
  *
  * @return	void
  */
 private function getData()
 {
     // try
     try {
         // fetch from google and save in db
         BackendAnalyticsHelper::getRecentReferrers();
         // fetch from google and save in db
         BackendAnalyticsHelper::getRecentKeywords();
     } catch (Exception $e) {
         // throw exception
         throw new SpoonException('Something went wrong while getting dashboard data.');
     }
 }
 /**
  * Get data
  *
  * @return	void
  */
 private function getData()
 {
     // try
     try {
         // fetch from google and save in db
         BackendAnalyticsHelper::getRecentReferrers();
         // fetch from google and save in db
         BackendAnalyticsHelper::getRecentKeywords();
     } catch (Exception $e) {
         // return status
         $this->output(self::OK, array('status' => 'error'), 'Something went wrong while getting the traffic sources.');
     }
 }
Пример #6
0
 /**
  * Validate the form
  *
  * @return	void
  */
 private function validateForm()
 {
     // is the form submitted?
     if ($this->frm->isSubmitted()) {
         // cleanup the submitted fields, ignore fields that were added by hackers
         $this->frm->cleanupFields();
         // shorten values
         $pagePath = $this->frm->getField('page_path')->getValue();
         if (count($this->linkList) > 1) {
             $pageList = $this->frm->getField('page_list')->getSelected();
         }
         // get the target
         if ($this->frm->getfield('page_path')->isFilled()) {
             $page = $pagePath;
         } elseif ($pageList == '0') {
             $page = null;
         } else {
             $page = SITE_MULTILANGUAGE ? substr($pageList, strpos($pageList, '/', 1)) : $pageList;
         }
         // validate fields
         if (isset($page) && !SpoonFilter::isURL(SITE_URL . $page)) {
             $this->frm->getField('page_path')->addError(BL::err('InvalidURL'));
         }
         if (!isset($page)) {
             $this->frm->getField('page_path')->addError(BL::err('FieldIsRequired'));
         }
         if (!$this->frm->getField('page_path')->isFilled() && !$this->frm->getfield('page_list')->isFilled()) {
             $this->frm->getField('page_path')->addError(BL::err('FieldIsRequired'));
         }
         // no errors?
         if ($this->frm->isCorrect()) {
             // get metrics
             $metrics = BackendAnalyticsHelper::getMetricsForPage($page, $this->startTimestamp, $this->endTimestamp);
             // build item
             $item['page_path'] = $page;
             $item['entrances'] = isset($metrics['entrances']) ? $metrics['entrances'] : 0;
             $item['bounces'] = isset($metrics['bounces']) ? $metrics['bounces'] : 0;
             $item['bounce_rate'] = ($metrics['entrances'] == 0 ? 0 : number_format((int) $metrics['bounces'] / $metrics['entrances'] * 100, 2)) . '%';
             $item['start_date'] = date('Y-m-d', $this->startTimestamp) . ' 00:00:00';
             $item['end_date'] = date('Y-m-d', $this->endTimestamp) . ' 00:00:00';
             $item['updated_on'] = date('Y-m-d H:i:s');
             // insert the item
             $item['id'] = (int) BackendAnalyticsModel::insertLandingPage($item);
             // trigger event
             BackendModel::triggerEvent($this->getModule(), 'after_add_landing_page', array('item' => $item));
             // everything is saved, so redirect to the overview
             $this->redirect(BackendModel::createURLForAction('landing_pages') . '&report=saved&var=' . urlencode($item['page_path']));
         }
     }
 }
Пример #7
0
 /**
  * Get the selected table id
  *
  * @return	string
  */
 public static function getTableId()
 {
     return (string) BackendAnalyticsHelper::getGoogleAnalyticsInstance()->getTableId();
 }
Пример #8
0
 /**
  * Set start and end timestamp needed to collect analytics data
  */
 private function setDates()
 {
     BackendAnalyticsHelper::setDates();
     $this->startTimestamp = SpoonSession::get('analytics_start_timestamp');
     $this->endTimestamp = SpoonSession::get('analytics_end_timestamp');
 }
Пример #9
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');
     }
 }
Пример #10
0
 /**
  * Gets all the needed parameters to link a google analytics account to fork
  */
 private function getAnalyticsParameters()
 {
     $remove = SpoonFilter::getGetValue('remove', array('session_token', 'table_id'), null);
     // something has to be removed before proceeding
     if (!empty($remove)) {
         // the session token has te be removed
         if ($remove == 'session_token') {
             // remove all parameters from the module settings
             BackendModel::setModuleSetting($this->getModule(), 'session_token', null);
         }
         // remove all profile parameters from the module settings
         BackendModel::setModuleSetting($this->getModule(), 'account_name', null);
         BackendModel::setModuleSetting($this->getModule(), 'table_id', null);
         BackendModel::setModuleSetting($this->getModule(), 'profile_title', null);
         // remove cache files
         BackendAnalyticsModel::removeCacheFiles();
         // clear tables
         BackendAnalyticsModel::clearTables();
     }
     // get session token, account name, the profile's table id, the profile's title
     $this->sessionToken = BackendModel::getModuleSetting($this->getModule(), 'session_token', null);
     $this->accountName = BackendModel::getModuleSetting($this->getModule(), 'account_name', null);
     $this->tableId = BackendModel::getModuleSetting($this->getModule(), 'table_id', null);
     $this->profileTitle = BackendModel::getModuleSetting($this->getModule(), 'profile_title', null);
     // no session token
     if (!isset($this->sessionToken)) {
         $token = SpoonFilter::getGetValue('token', null, null);
         // a one time token is given in the get parameters
         if (!empty($token) && $token !== 'true') {
             // get google analytics instance
             $ga = BackendAnalyticsHelper::getGoogleAnalyticsInstance();
             // get a session token
             $this->sessionToken = $ga->getSessionToken($token);
             // store the session token in the settings
             BackendModel::setModuleSetting($this->getModule(), 'session_token', $this->sessionToken);
         }
     }
     // session id is present but there is no table_id
     if (isset($this->sessionToken) && !isset($this->tableId)) {
         // get google analytics instance
         $ga = BackendAnalyticsHelper::getGoogleAnalyticsInstance();
         // get all possible profiles in this account
         $this->profiles = $ga->getAnalyticsAccountList($this->sessionToken);
         // not authorized
         if ($this->profiles == 'UNAUTHORIZED') {
             // remove invalid session token
             BackendModel::setModuleSetting($this->getModule(), 'session_token', null);
             // redirect to the settings page without parameters
             $this->redirect(BackendModel::createURLForAction('settings'));
         } elseif (is_array($this->profiles)) {
             $tableId = SpoonFilter::getGetValue('table_id', null, null);
             // a table id is given in the get parameters
             if (!empty($tableId)) {
                 $profiles = array();
                 // set the table ids as keys
                 foreach ($this->profiles as $profile) {
                     $profiles[$profile['tableId']] = $profile;
                 }
                 // correct table id
                 if (isset($profiles[$tableId])) {
                     // save table id and account title
                     $this->tableId = $tableId;
                     $this->accountName = $profiles[$this->tableId]['accountName'];
                     $this->profileTitle = $profiles[$this->tableId]['title'];
                     $webPropertyId = $profiles[$this->tableId]['webPropertyId'];
                     // store the table id and account title in the settings
                     BackendModel::setModuleSetting($this->getModule(), 'account_name', $this->accountName);
                     BackendModel::setModuleSetting($this->getModule(), 'table_id', $this->tableId);
                     BackendModel::setModuleSetting($this->getModule(), 'profile_title', $this->profileTitle);
                     BackendModel::setModuleSetting($this->getModule(), 'web_property_id', $webPropertyId);
                 }
             }
         }
     }
 }