visit.php
来自「一款可以和GOOGLE媲美的开源统计系统,运用AJAX.功能强大. 无色提示:」· PHP 代码 · 共 831 行 · 第 1/2 页
PHP
831 行
$timespentLastAction = $serverTime - $this->visitorInfo['visit_last_action_time']; $action->record( $this->visitorInfo['idvisit'], $this->visitorInfo['visit_exit_idaction'], $timespentLastAction ); /** * Cookie fields to be updated */ $this->visitorInfo['visit_last_action_time'] = $serverTime; $this->visitorInfo['visit_exit_idaction'] = $actionId; } /** * In the case of a new visit, we have to do the following actions: * * 1) Insert the new action * * 2) Insert the visit information */ protected function handleNewVisit() { printDebug("New Visit."); /** * Get the variables from the REQUEST */ $localTime = Piwik_Common::getRequestVar( 'h', $this->getCurrentDate("H"), 'numeric') .':'. Piwik_Common::getRequestVar( 'm', $this->getCurrentDate("i"), 'numeric') .':'. Piwik_Common::getRequestVar( 's', $this->getCurrentDate("s"), 'numeric'); $serverTime = $this->getCurrentTimestamp(); $serverDate = $this->getCurrentDate(); if($this->isVisitorKnown()) { $idcookie = $this->visitorInfo['visitor_idcookie']; $returningVisitor = 1; } else { $idcookie = $this->getVisitorUniqueId(); $returningVisitor = 0; } $defaultTimeOnePageVisit = Piwik_Tracker_Config::getInstance()->Tracker['default_time_one_page_visit']; $userInfo = $this->getUserSettingsInformation(); $country = Piwik_Common::getCountry($userInfo['location_browser_lang']); $continent = Piwik_Common::getContinent( $country ); $refererInfo = $this->getRefererInformation(); /** * Init the action */ $action = $this->getActionObject(); $actionId = $action->getActionId(); printDebug("idAction = $actionId"); /** * Save the visitor */ $informationToSave = array( 'idsite' => $this->idsite, 'visitor_localtime' => $localTime, 'visitor_idcookie' => $idcookie, 'visitor_returning' => $returningVisitor, 'visit_first_action_time' => $this->getDatetimeFromTimestamp($serverTime), 'visit_last_action_time' => $this->getDatetimeFromTimestamp($serverTime), 'visit_server_date' => $serverDate, 'visit_entry_idaction' => $actionId, 'visit_exit_idaction' => $actionId, 'visit_total_actions' => 1, 'visit_total_time' => $defaultTimeOnePageVisit, 'referer_type' => $refererInfo['referer_type'], 'referer_name' => $refererInfo['referer_name'], 'referer_url' => $refererInfo['referer_url'], 'referer_keyword' => $refererInfo['referer_keyword'], 'config_md5config' => $userInfo['config_md5config'], 'config_os' => $userInfo['config_os'], 'config_browser_name' => $userInfo['config_browser_name'], 'config_browser_version' => $userInfo['config_browser_version'], 'config_resolution' => $userInfo['config_resolution'], 'config_pdf' => $userInfo['config_pdf'], 'config_flash' => $userInfo['config_flash'], 'config_java' => $userInfo['config_java'], 'config_director' => $userInfo['config_director'], 'config_quicktime' => $userInfo['config_quicktime'], 'config_realplayer' => $userInfo['config_realplayer'], 'config_windowsmedia' => $userInfo['config_windowsmedia'], 'config_cookie' => $userInfo['config_cookie'], 'location_ip' => $userInfo['location_ip'], 'location_browser_lang' => $userInfo['location_browser_lang'], 'location_country' => $country, 'location_continent' => $continent, ); Piwik_PostEvent('Tracker.newVisitorInformation', $informationToSave); $fields = implode(", ", array_keys($informationToSave)); $values = substr(str_repeat( "?,",count($informationToSave)),0,-1); $this->db->query( "INSERT INTO ".$this->db->prefixTable('log_visit'). " ($fields) VALUES ($values)", array_values($informationToSave)); $idVisit = $this->db->lastInsertId(); // Update the visitor information attribute with this information array $this->visitorInfo = $informationToSave; $this->visitorInfo['idvisit'] = $idVisit; // we have to save timestamp in the object properties, whereas mysql eats some other datetime format $this->visitorInfo['visit_first_action_time'] = $serverTime; $this->visitorInfo['visit_last_action_time'] = $serverTime; // saves the action $action->record( $idVisit, 0, 0 ); } /** * Returns an object able to handle the current action * Plugins can return an override Action that for example, does not record the action in the DB * * @return Piwik_Tracker_Action child or fake but with same public interface */ protected function getActionObject() { $action = null; Piwik_PostEvent('Tracker.newAction', $action); if(is_null($action)) { $action = new Piwik_Tracker_Action( $this->db ); } elseif(!($action instanceof Piwik_Tracker_Action_Interface)) { throw new Exception("The Action object set in the plugin must implement the interface Piwik_Tracker_Action_Interface"); } $action->setIdSite($this->idsite); return $action; } /** * Returns an array containing the following information: * - referer_type * - direct -- absence of referer URL OR referer URL has the same host * - site -- based on the referer URL * - search_engine -- based on the referer URL * - campaign -- based on campaign URL parameter * - newsletter -- based on newsletter URL parameter * - partner -- based on partner URL parameter * * - referer_name * - () * - piwik.net -- site host name * - google.fr -- search engine host name * - adwords-search -- campaign name * - beta-release -- newsletter name * - my-nice-partner -- partner name * * - referer_keyword * - () * - () * - my keyword * - my paid keyword * - () * - () * * - referer_url : the same for all the referer types * */ protected function getRefererInformation() { // default values for the referer_* fields $this->typeRefererAnalyzed = Piwik_Common::REFERER_TYPE_DIRECT_ENTRY; $this->nameRefererAnalyzed = ''; $this->keywordRefererAnalyzed = ''; $this->refererHost = ''; // get the urls and parse them $refererUrl = Piwik_Common::getRequestVar( 'urlref', '', 'string'); $currentUrl = Piwik_Common::getRequestVar( 'url', '', 'string'); $this->refererUrlParse = @parse_url($refererUrl); $this->currentUrlParse = @parse_url($currentUrl); if(isset($this->refererUrlParse['host'])) { $this->refererHost = $this->refererUrlParse['host']; } $refererDetected = false; if( !empty($this->currentUrlParse['host'])) { if( $this->detectRefererNewsletter() || $this->detectRefererPartner() || $this->detectRefererCampaign() ) { $refererDetected = true; } } if(!$refererDetected && !empty($this->refererUrlParse['host']) ) { if( $this->detectRefererSearchEngine() || $this->detectRefererDirectEntry() ) { $refererDetected = true; } } if(!empty($this->refererHost) && !$refererDetected) { $this->typeRefererAnalyzed = Piwik_Common::REFERER_TYPE_WEBSITE; $this->nameRefererAnalyzed = $this->refererHost; } $refererInformation = array( 'referer_type' => $this->typeRefererAnalyzed, 'referer_name' => $this->nameRefererAnalyzed, 'referer_keyword' => $this->keywordRefererAnalyzed, 'referer_url' => $refererUrl, ); return $refererInformation; } /* * Search engine detection */ protected function detectRefererSearchEngine() { /* * A referer is a search engine if the URL's host is in the SearchEngines array * and if we found the keyword in the URL. * * For example if someone comes from http://www.google.com/partners.html this will not * be counted as a search engines, but as a website referer from google.com (because the * keyword couldn't be found in the URL) */ require "core/DataFiles/SearchEngines.php"; if(array_key_exists($this->refererHost, $GLOBALS['Piwik_SearchEngines'])) { $searchEngineName = $GLOBALS['Piwik_SearchEngines'][$this->refererHost][0]; $variableName = $GLOBALS['Piwik_SearchEngines'][$this->refererHost][1]; if(isset($this->refererUrlParse['query'])) { $query = $this->refererUrlParse['query']; if($searchEngineName == 'Google Images') { $query = urldecode(trim(strtolower(Piwik_Common::getParameterFromQueryString($query, 'prev')))); $query = str_replace('&', '&', strstr($query, '?')); } // search for keywords now &vname=keyword $key = trim(strtolower(Piwik_Common::getParameterFromQueryString($query, $variableName))); if(!empty($key) && function_exists('iconv') && isset($GLOBALS['Piwik_SearchEngines'][$this->refererHost][2])) { $charset = trim($GLOBALS['Piwik_SearchEngines'][$this->refererHost][2]); if(!empty($charset)) { $key = htmlspecialchars( @iconv( $charset, 'utf-8//TRANSLIT', htmlspecialchars_decode($key, Piwik_Common::HTML_ENCODING_QUOTE_STYLE)) , Piwik_Common::HTML_ENCODING_QUOTE_STYLE); } } if(!empty($key)) { $this->typeRefererAnalyzed = Piwik_Common::REFERER_TYPE_SEARCH_ENGINE; $this->nameRefererAnalyzed = $searchEngineName; $this->keywordRefererAnalyzed = $key; return true; } } } } /* * Newsletter analysis */ protected function detectRefererNewsletter() { if(isset($this->currentUrlParse['query'])) { $newsletterVariableName = Piwik_Tracker_Config::getInstance()->Tracker['newsletter_var_name']; $newsletterVar = Piwik_Common::getParameterFromQueryString( $this->currentUrlParse['query'], $newsletterVariableName); if(!empty($newsletterVar)) { $this->typeRefererAnalyzed = Piwik_Common::REFERER_TYPE_NEWSLETTER; $this->nameRefererAnalyzed = $newsletterVar; return true; } } } /* * Partner analysis */ protected function detectRefererPartner() { if(isset($this->currentUrlParse['query'])) { $partnerVariableName = Piwik_Tracker_Config::getInstance()->Tracker['partner_var_name']; $partnerVar = Piwik_Common::getParameterFromQueryString($this->currentUrlParse['query'], $partnerVariableName); if(!empty($partnerVar)) { $this->typeRefererAnalyzed = Piwik_Common::REFERER_TYPE_PARTNER; $this->nameRefererAnalyzed = $partnerVar; return true; } } } /* * Campaign analysis */ protected function detectRefererCampaign() { if(isset($this->currentUrlParse['query'])) { $campaignVariableName = Piwik_Tracker_Config::getInstance()->Tracker['campaign_var_name']; $campaignName = Piwik_Common::getParameterFromQueryString($this->currentUrlParse['query'], $campaignVariableName); if( !empty($campaignName)) { $campaignKeywordVariableName = Piwik_Tracker_Config::getInstance()->Tracker['campaign_keyword_var_name']; $campaignKeyword = Piwik_Common::getParameterFromQueryString($this->currentUrlParse['query'], $campaignKeywordVariableName); $this->typeRefererAnalyzed = Piwik_Common::REFERER_TYPE_CAMPAIGN; $this->nameRefererAnalyzed = $campaignName; if(!empty($campaignKeyword)) { $this->keywordRefererAnalyzed = $campaignKeyword; } return true; } } } /* * Direct entry (referer host is similar to current host) * And we have previously tried to detect the newsletter/partner/campaign variables in the URL * so it can only be a direct access */ protected function detectRefererDirectEntry() { if(isset($this->currentUrlParse['host'])) { $currentHost = $this->currentUrlParse['host']; if($currentHost == $this->refererHost) { $this->typeRefererAnalyzed = Piwik_Common::REFERER_TYPE_DIRECT_ENTRY; return true; } } } /** * Returns a MD5 of all the configuration settings * @return string */ protected function getConfigHash( $os, $browserName, $browserVersion, $resolution, $plugin_Flash, $plugin_Director, $plugin_RealPlayer, $plugin_Pdf, $plugin_WindowsMedia, $plugin_Java, $plugin_Cookie, $ip, $browserLang) { return md5( $os . $browserName . $browserVersion . $resolution . $plugin_Flash . $plugin_Director . $plugin_RealPlayer . $plugin_Pdf . $plugin_WindowsMedia . $plugin_Java . $plugin_Cookie . $ip . $browserLang ); } /** * Returns either * - "-1" for a known visitor * - a unique 32 char identifier @see Piwik_Common::generateUniqId() */ protected function getVisitorUniqueId() { if($this->isVisitorKnown()) { return -1; } else { return Piwik_Common::generateUniqId(); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?