name = 'ganalyticscom';
$this->tab = 'analytics_stats';
$this->version = '1.6.1';
$this->author = 'Speedyweb';
$this->module_key = '90bbcd281e12778f64c01db88277a43c';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('Google Analytics E-Commerce PHP Ultimate');
$this->description = $this->l('Insertion des commandes dans Google Analytics en PHP');
/** Backward compatibility */
require(_PS_MODULE_DIR_.$this->name.'/backward_compatibility/backward.php');
if (!function_exists('curl_init'))
$this->warning = $this->l('The cURL extension must be enabled to use this module.');
}
public function install()
{
$languages = Language::getLanguages(false);
$aCodeGA = array();
foreach ($languages as $language) $aCodeGA[(int)($language['id_lang'])] = '';
if (!parent::install()
|| !$this->registerHook('top')
|| !$this->registerHook('header')
|| ($this->checkVersion('is_1.4') && !$this->registerHook('cart'))
|| ($this->checkVersion('is_1.5') && !$this->registerHook('actionCartSave'))
|| !$this->registerHook('postUpdateOrderStatus')
|| !$this->registerHook('newOrder')
|| !$this->registerHook('backOfficeHeader')
|| !Configuration::updateValue('codeGA', $aCodeGA)
|| !Configuration::updateValue('ga_insert_statuts', '2')
|| !Configuration::updateValue('ga_annul_statuts', '6')
|| !Configuration::updateValue('ga_logs', 0)
|| !Configuration::updateValue('cookie_min_time', 18000))
return false;
return Db::getInstance()->Execute('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'ganalyticscoms`(
`id` int(11) NOT NULL auto_increment,
`id_cart` int(11) DEFAULT \'0\' NOT NULL,
`commande` int(11) NOT NULL,
`referer` text NOT NULL,
`gclid` text NOT NULL,
`utm_params` VARCHAR( 255 ) NOT NULL,
`user_agent` TEXT NOT NULL,
`extra_params` VARCHAR( 255 ) NOT NULL,
`ga_statut` smallint(6) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8');
}
public function uninstall()
{
Configuration::deleteByName('codeGA');
Configuration::deleteByName('ga_insert_statuts');
Configuration::deleteByName('ga_annul_statuts');
Configuration::deleteByName('ga_logs');
Configuration::deleteByName('cookie_min_time');
Db::getInstance()->Execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'ganalyticscoms`');
return parent::uninstall();
}
/***************************************************************************************************************/
public function getContent()
{
if (Tools::isSubmit('submitAnalytics'))
{
$this->_postValidation();
if (!count($this->_postErrors)) $this->_postProcess();
else $this->displayErrors();
}
elseif (Tools::isSubmit('submitPurgeDB'))
{
$this->PurgeDB();
if (!count($this->_postErrors)) $this->displayPurgeConf();
else $this->displayErrors();
}
$this->_displayAnalytics();
$this->_displayForm();
return $this->_html;
}
private function _postProcess()
{
if (Tools::isSubmit('submitAnalytics'))
{
$languages = Language::getLanguages(false);
$aCodeGA = array();
foreach ($languages as $language) $aCodeGA[$language['id_lang']] = (string)Tools::getValue('codeGA_'.$language['id_lang']);
Configuration::updateValue('codeGA', $aCodeGA);
Configuration::updateValue('ga_logs', (Tools::isSubmit('ga_logs') && (int)Tools::getValue('ga_logs') != 0) ? 1 : 0);
Configuration::updateValue('ga_insert_statuts', implode(', ', Tools::getValue('ga_insert_statuts')));
Configuration::updateValue('ga_annul_statuts', implode(', ', Tools::getValue('ga_annul_statuts')));
$cookie_min_time = (int)Tools::getValue('cookie_min_time');
Configuration::updateValue('cookie_min_time', ($cookie_min_time > 18000) ? $cookie_min_time : 18000);
}
}
private function _postValidation()
{
// TO DO: Post Validation
}
/****** HOOKS *************************************************************************************************/
public function getDateCookieCreation($cookie)
{
$tab_cookie = explode('__datecookie:', $cookie);
return (isset($tab_cookie[1]) ? $tab_cookie[1] : time());
}
public function cleanRefererFromCookie($cookie)
{
$tab_cookie = explode('__datecookie:', $cookie);
return $tab_cookie[0];
}
public function hookTop($params)
{
include_once(realpath(dirname(__FILE__)).'/lib/X_Tools.php');
$referer = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
$query_string = (isset($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : '';
$user_agent = (isset($_SERVER['HTTP_USER_AGENT'])) ? $_SERVER['HTTP_USER_AGENT'] : '';
$cookie_min_time_before_overwrite = (int)Configuration::get('cookie_min_time'); // 5h par défaut - Donnée configurable dans l'admin
$cookie_expire_time = time() + 15778800; // 6 mois
if ((!isset($_COOKIE['ganalytics']) || (!empty($referer) && time() > ($this->getDateCookieCreation($_COOKIE['ganalytics']) + $cookie_min_time_before_overwrite)))
&& $this->GetDomain($referer) != str_replace('www.', '', $_SERVER['HTTP_HOST']))
{
setcookie('ganalytics', $referer.'__datecookie:'.time(), $cookie_expire_time, '/');
setcookie('query_string', $query_string, $cookie_expire_time, '/');
setcookie('user_agent', $user_agent, $cookie_expire_time, '/');
}
}
public function hookActionCartSave($params)
{
$this->hookCart($params);
}
public function hookCart($params)
{
include_once(realpath(dirname(__FILE__)).'/lib/X_Tools.php');
$referer = isset($_COOKIE['ganalytics']) ? $this->cleanRefererFromCookie($_COOKIE['ganalytics']) : '';
$query_string = isset($_COOKIE['query_string']) ? $_COOKIE['query_string'] : '';
$user_agent = isset($_COOKIE['user_agent']) ? $_COOKIE['user_agent'] : '';
$extra_params = isset($_COOKIE['ga_extra_params']) ? $_COOKIE['ga_extra_params'] : '';
$query_params = $this->query2tab($query_string);
$gclid = isset($query_params['gclid']) ? $query_params['gclid'] : '';
$utm_params = $this->getUtmParams($query_params);
if (isset($params['cart']) && (int)$params['cart']->id > 0)
{
$row = Db::getInstance()->getRow('SELECT * FROM `'._DB_PREFIX_.'ganalyticscoms` WHERE `id_cart` = '.$params['cart']->id);
if (!$row)
{
//if (Configuration::get('ga_logs')) $this->writeLogs($this->_logFile, "");
//if (Configuration::get('ga_logs')) $this->writeLogs($this->_logFile, "## ".date('d-m-Y H:i:s')." #############################################");
//if (Configuration::get('ga_logs')) $this->writeLogs($this->_logFile, "#Function - hookCart | query_string = ".$query_string." | referer = ".$referer." | user_agent = ".$user_agent." | extra params = ".$extra_params);
//if (Configuration::get('ga_logs')) $this->writeLogs($this->_logFile, "# INSERT | ID cart = ".$params['cart']->id);
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'ganalyticscoms` (`id_cart`, `commande`, `referer`, `gclid`, `utm_params`, `user_agent`, `extra_params`)
VALUES('.(int)$params['cart']->id.',0, "'.$referer.'", "'.$gclid.'", "'.$utm_params.'", "'.$user_agent.'", "'.$extra_params.'")');
}
}
}
public function hookHeader($params)
{
return $this->display(__FILE__, '/views/templates/hook/header.tpl');
}
public function hookBackOfficeHeader($params)
{
$return = '';
if (($this->checkVersion('is_1.5')
&& (!Tools::isSubmit('controller') || 'adminhome' == Tools::getValue('controller') || 'ganalyticscom' == Tools::getValue('module_name')
|| 'ganalyticscom' == Tools::getValue('configure')))
|| ($this->checkVersion('is_1.4') && !Tools::isSubmit('tab')))
$return = '';
return $return;
}
public function hookNewOrder($params)
{
$id_order = (isset($params['order'])) ? $params['order']->id : $params['id_order'];
$order = new Order((int)$id_order);
$row = Db::getInstance()->getRow('SELECT * FROM `'._DB_PREFIX_.'ganalyticscoms` WHERE `id_cart` = '.$order->id_cart);
if (!$row)
{
$referer = isset($_COOKIE['ganalytics']) ? $this->cleanRefererFromCookie($_COOKIE['ganalytics']) : '';
$query_string = isset($_COOKIE['query_string']) ? $_COOKIE['query_string'] : '';
$user_agent = isset($_COOKIE['user_agent']) ? $_COOKIE['user_agent'] : '';
$extra_params = isset($_COOKIE['ga_extra_params']) ? $_COOKIE['ga_extra_params'] : '';
$query_params = $this->query2tab($query_string);
$gclid = isset($query_params['gclid']) ? $query_params['gclid'] : '';
$utm_params = $this->getUtmParams($query_params);
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'ganalyticscoms` (`id_cart`, `commande`, `referer`, `gclid`, `utm_params`, `user_agent`, `extra_params`)
VALUES('.$order->id_cart.', '.(int)$id_order.', "'.$referer.'", "'.$gclid.'", "'.$utm_params.'", "'.$user_agent.'", "'.$extra_params.'")');
}
else Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.'ganalyticscoms` SET `commande` = '.(int)$id_order.' WHERE `id_cart` = '.$order->id_cart);
}
public function hookPostUpdateOrderStatus($params)
{
$this->statut($params);
}
/***************************************************************************************************************/
public function getArrayFromStringVar($nom, $sep = ', ')
{
return explode($sep, Configuration::get($nom));
}
public function getUrlSite()
{
return 'http://'.htmlspecialchars($_SERVER['HTTP_HOST'], ENT_COMPAT, 'UTF-8').__PS_BASE_URI__;
}
public function _displayAnalytics()
{
$this->_html .= '
'.$this->l('Module de statistiques Google Analytics E-commerce - Pilotez votre CA et votre taux de conversion !').'
'.
$this->l('Beware, this module does not insert the standard tracking code, but only allows you to insert transactions in Google Analytics.').' '.
$this->l('So, you must pass by another module to your standard tracking.').' '.
$this->l('If you want to use the "Enhanced Ecommerce" features, this is compatible but it\s the module with which you don the standard tracking that you need to configure.').' '.
$this->l('If you have the Google Analytics module developed by Prestashop, we recommend to disable the "OrderConfirmation" hook to not insert duplicate orders.').'
';
}
public function displayErrors()
{
foreach ($this->_postErrors as $err) $this->_html .= '