* @copyright 2007-2014 PrestaShop SA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_PS_VERSION_')) exit; class StatsOrigin extends ModuleGraph { private $_html; public function __construct() { $this->name = 'statsorigin'; $this->tab = 'analytics_stats'; $this->version = '1.3.1'; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Visitors origin'); $this->description = $this->l('Adds a graph displaying the websites your visitors came from to the Stats dashboard.'); $this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_); } public function install() { return (parent::install() && $this->registerHook('AdminStatsModules')); } private function getOrigins($dateBetween) { $directLink = $this->l('Direct link'); $sql = 'SELECT http_referer FROM '._DB_PREFIX_.'connections WHERE 1 '.Shop::addSqlRestriction().' AND date_add BETWEEN '.$dateBetween; $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->query($sql); $websites = array($directLink => 0); while ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->nextRow($result)) { if (!isset($row['http_referer']) || empty($row['http_referer'])) ++$websites[$directLink]; else { $website = preg_replace('/^www./', '', parse_url($row['http_referer'], PHP_URL_HOST)); if (!isset($websites[$website])) $websites[$website] = 1; else ++$websites[$website]; } } arsort($websites); return $websites; } public function hookAdminStatsModules() { $websites = $this->getOrigins(ModuleGraph::getDateBetween()); if (Tools::getValue('export')) if (Tools::getValue('exportType') == 'top') $this->csvExport(array('type' => 'pie')); $this->_html = '
'.$this->l('The referrer is the URL of the previous webpage from which a link was followed by the visitor.').'
'.$this->l('A referrer also enables you to know which keywords visitors use in search engines when browsing for your online store.').'
'.$this->l('A referrer can be:').'
'.$this->l('Origin').' | '.$this->l('Total').' |
---|---|
'.(!strstr($website, ' ') ? '' : '').$website.(!strstr($website, ' ') ? '' : '').' | '.$total.' |
'.$this->l('Direct links only').'
'; return $this->_html; } protected function getData($layers) { $this->_titles['main'] = $this->l('Top ten referral websites'); $websites = $this->getOrigins($this->getDate()); $total = 0; $total2 = 0; $i = 0; foreach ($websites as $website => $totalRow) { if (!$totalRow) continue; $total += $totalRow; if ($i++ < 9) { $this->_legend[] = $website; $this->_values[] = $totalRow; $total2 += $totalRow; } } if ($total != $total2) { $this->_legend[] = $this->l('Others'); $this->_values[] = $total - $total2; } } }