* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7307 $ * @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 SEKeywords extends ModuleGraph { private $_html = ''; private $_query = ''; private $_query2 = ''; public function __construct() { $this->name = 'sekeywords'; $this->tab = 'analytics_stats'; $this->version = 1.0; $this->author = 'PrestaShop'; $this->need_instance = 0; parent::__construct(); $this->_query = 'SELECT `keyword`, COUNT(TRIM(`keyword`)) as occurences FROM `'._DB_PREFIX_.'sekeyword` WHERE '.(Configuration::get('SEK_FILTER_KW') == '' ? '1' : '`keyword` REGEXP \''.pSQL(Configuration::get('SEK_FILTER_KW')).'\'') .$this->sqlShopRestriction(). ' AND `date_add` BETWEEN '; $this->_query2 = 'GROUP BY TRIM(`keyword`) HAVING occurences > '.(int)Configuration::get('SEK_MIN_OCCURENCES').' ORDER BY occurences DESC'; $this->displayName = $this->l('Search engine keywords'); $this->description = $this->l('Display which keywords have led visitors to your website.'); } public function install() { if (!parent::install() OR !$this->registerHook('top') OR !$this->registerHook('AdminStatsModules')) return false; Configuration::updateValue('SEK_MIN_OCCURENCES', 1); Configuration::updateValue('SEK_FILTER_KW', ''); return Db::getInstance()->execute(' CREATE TABLE `'._DB_PREFIX_.'sekeyword` ( id_sekeyword INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, id_shop INTEGER UNSIGNED NOT NULL DEFAULT \'1\', id_group_shop INTEGER UNSIGNED NOT NULL DEFAULT \'1\', keyword VARCHAR(256) NOT NULL, date_add DATETIME NOT NULL, PRIMARY KEY(id_sekeyword) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'); } public function uninstall() { if (!parent::uninstall()) return false; return (Db::getInstance()->execute('DROP TABLE `'._DB_PREFIX_.'sekeyword`')); } public function hookTop($params) { if (!isset($_SERVER['HTTP_REFERER']) OR strstr($_SERVER['HTTP_REFERER'], Tools::getHttpHost(false, false))) return; if ($keywords = $this->getKeywords($_SERVER['HTTP_REFERER'])) Db::getInstance()->execute('INSERT INTO `'._DB_PREFIX_.'sekeyword` (`keyword`, `date_add`, `id_shop`, `id_group_shop`) VALUES (\''.pSQL(Tools::strtolower(trim($keywords))).'\', NOW(), '.$this->context->shop->getID().', '.$this->context->shop->getGroupID().')'); } public function hookAdminStatsModules() { if (Tools::isSubmit('submitSEK')) { Configuration::updateValue('SEK_FILTER_KW', trim(Tools::getValue('SEK_FILTER_KW'))); Configuration::updateValue('SEK_MIN_OCCURENCES', (int)Tools::getValue('SEK_MIN_OCCURENCES')); Tools::redirectAdmin('index.php?tab=AdminStats&token='.Tools::getValue('token').'&module='.$this->name); } if (Tools::getValue('export')) $this->csvExport(array('type' => 'pie')); $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($this->_query.ModuleGraph::getDateBetween().$this->_query2); $total = count($result); $this->_html = '
'.$this->displayName.' '.$total.' '.($total == 1 ? $this->l('keyword matches your query.') : $this->l('keywords match your query.')).'
 
'; if ($result AND $total) { $table = '
'; foreach ($result as $index => $row) { $keyword =& $row['keyword']; $occurences =& $row['occurences']; $table .= ''; } $table .= '
'.$this->l('Keywords').' '.$this->l('Occurrences').'
'.$keyword.''.$occurences.'
'; $this->_html .= '
'.$this->engine(array('type' => 'pie')).'

'.$this->l('CSV Export').'


'.$this->l('Filter by keyword').' '.$this->l('and min occurrences').'

'.$table; } else $this->_html .= '

'.$this->l('No keywords').'

'; $this->_html .= '

'.$this->l('Guide').'

'.$this->l('Identify external search engines\' keywords').'

'.$this->l('One of the most common ways of finding a website through a search engine. Identifying the most popular keywords entered by your new visitors allows you to see which products you should put in front if you want to attract more visitors and potential customers.').'


'.$this->l('How does it work?').'

'.$this->l('When a visitor comes to your website, the server notes their previous location. This module parses the URL and finds the keywords in it. Currently, it manages the following search engines:').' Google, AOL, Yandex, Ask, NHL, Yahoo, Baidu, Lycos, Exalead, Live, Voila '.$this->l('and').' Altavista. '.$this->l('Soon it will be possible to dynamically add new search engines and contribute to this module.').'


'; return $this->_html; } public function getKeywords($url) { if (!Validate::isAbsoluteUrl($url)) return false; $parsedUrl = parse_url($url); if (!isset($parsedUrl['query']) && isset($parsedUrl['fragment'])) $parsedUrl['query'] = $parsedUrl['fragment']; if (!isset($parsedUrl['query'])) return false; $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('SELECT `server`, `getvar` FROM `'._DB_PREFIX_.'search_engine`'); foreach ($result as $index => $row) { $host =& $row['server']; $varname =& $row['getvar']; if (strstr($parsedUrl['host'], $host)) { $kArray = array(); preg_match('/[^a-z]'.$varname.'=.+\&'.'/U', $parsedUrl['query'], $kArray); if (!isset($kArray[0]) OR empty($kArray[0])) preg_match('/[^a-z]'.$varname.'=.+$'.'/', $parsedUrl['query'], $kArray); if (!isset($kArray[0]) OR empty($kArray[0])) return false; $kString = urldecode(str_replace('+', ' ', ltrim(substr(rtrim($kArray[0], '&'), strlen($varname) + 1), '='))); return $kString; } } } protected function getData($layers) { $this->_titles['main'] = $this->l('10 first keywords'); $totalResult = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($this->_query.$this->getDate().$this->_query2); $total = 0; $total2 = 0; foreach ($totalResult as $totalRow) $total += $totalRow['occurences']; $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($this->_query.$this->getDate().$this->_query2.' LIMIT 9'); foreach ($result as $row) { $this->_legend[] = $row['keyword']; $this->_values[] = $row['occurences']; $total2 += $row['occurences']; } if ($total >= $total2) { $this->_legend[] = $this->l('Others'); $this->_values[] = $total - $total2; } } }