first commit
This commit is contained in:
113
modules/ps_googleanalytics/classes/Database/Install.php
Normal file
113
modules/ps_googleanalytics/classes/Database/Install.php
Normal file
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Database;
|
||||
|
||||
use Configuration;
|
||||
use Db;
|
||||
use Ps_Googleanalytics;
|
||||
use Shop;
|
||||
|
||||
class Install
|
||||
{
|
||||
/**
|
||||
* @var Ps_Googleanalytics
|
||||
*/
|
||||
private $module;
|
||||
|
||||
public function __construct(Ps_Googleanalytics $module)
|
||||
{
|
||||
if (Shop::isFeatureActive()) {
|
||||
Shop::setContext(Shop::CONTEXT_ALL);
|
||||
}
|
||||
|
||||
$this->module = $module;
|
||||
}
|
||||
|
||||
/**
|
||||
* installTables
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function installTables()
|
||||
{
|
||||
$sql = [];
|
||||
|
||||
$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'ganalytics` (
|
||||
`id_google_analytics` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`id_order` int(11) NOT NULL,
|
||||
`id_customer` int(10) NOT NULL,
|
||||
`id_shop` int(11) NOT NULL,
|
||||
`sent` tinyint(1) DEFAULT NULL,
|
||||
`refund_sent` tinyint(1) DEFAULT NULL,
|
||||
`date_add` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id_google_analytics`),
|
||||
KEY `id_order` (`id_order`),
|
||||
KEY `sent` (`sent`)
|
||||
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8 AUTO_INCREMENT=1';
|
||||
|
||||
$sql[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'ganalytics_data` (
|
||||
`id_cart` int(11) NOT NULL,
|
||||
`id_shop` int(11) NOT NULL,
|
||||
`data` TEXT DEFAULT NULL,
|
||||
PRIMARY KEY (`id_cart`)
|
||||
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
|
||||
|
||||
foreach ($sql as $query) {
|
||||
if (!Db::getInstance()->execute($query)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert default data to database
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function setDefaultConfiguration()
|
||||
{
|
||||
Configuration::updateValue('GA_CANCELLED_STATES', json_encode([Configuration::get('PS_OS_CANCELED')]));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register Module hooks
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function registerHooks()
|
||||
{
|
||||
return $this->module->registerHook('displayHeader') &&
|
||||
$this->module->registerHook('displayAdminOrder') &&
|
||||
$this->module->registerHook('displayFooter') &&
|
||||
$this->module->registerHook('displayHome') &&
|
||||
$this->module->registerHook('displayFooterProduct') &&
|
||||
$this->module->registerHook('displayOrderConfirmation') &&
|
||||
$this->module->registerHook('actionProductCancel') &&
|
||||
$this->module->registerHook('actionOrderStatusPostUpdate') &&
|
||||
$this->module->registerHook('actionCartSave') &&
|
||||
$this->module->registerHook('displayBackOfficeHeader') &&
|
||||
$this->module->registerHook('actionCarrierProcess');
|
||||
}
|
||||
}
|
||||
45
modules/ps_googleanalytics/classes/Database/Uninstall.php
Normal file
45
modules/ps_googleanalytics/classes/Database/Uninstall.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Database;
|
||||
|
||||
use Db;
|
||||
|
||||
class Uninstall
|
||||
{
|
||||
/**
|
||||
* uninstallTables
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uninstallTables()
|
||||
{
|
||||
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'ganalytics`';
|
||||
$sql[] = 'DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'ganalytics_data`';
|
||||
|
||||
foreach ($sql as $query) {
|
||||
if (!Db::getInstance()->execute($query)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
28
modules/ps_googleanalytics/classes/Database/index.php
Normal file
28
modules/ps_googleanalytics/classes/Database/index.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
header('Expires: Mon, 26 Jul 1998 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
250
modules/ps_googleanalytics/classes/Form/ConfigurationForm.php
Normal file
250
modules/ps_googleanalytics/classes/Form/ConfigurationForm.php
Normal file
@@ -0,0 +1,250 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Form;
|
||||
|
||||
use AdminController;
|
||||
use Configuration;
|
||||
use Context;
|
||||
use HelperForm;
|
||||
use OrderState;
|
||||
use Ps_Googleanalytics;
|
||||
use Shop;
|
||||
use Tools;
|
||||
|
||||
class ConfigurationForm
|
||||
{
|
||||
private $module;
|
||||
|
||||
public function __construct(Ps_Googleanalytics $module)
|
||||
{
|
||||
$this->module = $module;
|
||||
}
|
||||
|
||||
/**
|
||||
* generate
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function generate()
|
||||
{
|
||||
// Check if multistore is active
|
||||
$is_multistore_active = Shop::isFeatureActive();
|
||||
|
||||
// Get default language
|
||||
$default_lang = (int) Configuration::get('PS_LANG_DEFAULT');
|
||||
|
||||
$helper = new HelperForm();
|
||||
|
||||
// Module, token and currentIndex
|
||||
$helper->module = $this->module;
|
||||
$helper->name_controller = $this->module->name;
|
||||
$helper->token = Tools::getAdminTokenLite('AdminModules');
|
||||
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->module->name;
|
||||
|
||||
// Language
|
||||
$helper->default_form_language = $default_lang;
|
||||
$helper->allow_employee_form_lang = $default_lang;
|
||||
|
||||
// Title and toolbar
|
||||
$helper->title = $this->module->displayName;
|
||||
$helper->show_toolbar = true; // false -> remove toolbar
|
||||
$helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen.
|
||||
$helper->submit_action = 'submit' . $this->module->name;
|
||||
$helper->toolbar_btn = [
|
||||
'save' => [
|
||||
'desc' => $this->module->l('Save'),
|
||||
'href' => AdminController::$currentIndex . '&configure=' . $this->module->name . '&save=' . $this->module->name .
|
||||
'&token=' . Tools::getAdminTokenLite('AdminModules'),
|
||||
],
|
||||
'back' => [
|
||||
'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminModules'),
|
||||
'desc' => $this->module->l('Back to list'),
|
||||
],
|
||||
];
|
||||
|
||||
$fields_form = [];
|
||||
// Init Fields form array
|
||||
$fields_form[0]['form'] = [
|
||||
'legend' => [
|
||||
'title' => $this->module->l('Settings'),
|
||||
],
|
||||
'input' => [
|
||||
[
|
||||
'type' => 'text',
|
||||
'label' => $this->module->l('Google Analytics Tracking ID'),
|
||||
'name' => 'GA_ACCOUNT_ID',
|
||||
'size' => 20,
|
||||
'required' => true,
|
||||
'hint' => $this->module->l('This information is available in your Google Analytics account'),
|
||||
],
|
||||
[
|
||||
'type' => 'switch',
|
||||
'label' => $this->module->l('Enable User ID tracking'),
|
||||
'name' => 'GA_USERID_ENABLED',
|
||||
'values' => [
|
||||
[
|
||||
'id' => 'ga_userid_enabled',
|
||||
'value' => 1,
|
||||
'label' => $this->module->l('Yes'),
|
||||
],
|
||||
[
|
||||
'id' => 'ga_userid_disabled',
|
||||
'value' => 0,
|
||||
'label' => $this->module->l('No'),
|
||||
], ],
|
||||
],
|
||||
[
|
||||
'type' => 'switch',
|
||||
'label' => $this->module->l('Anonymize IP'),
|
||||
'name' => 'GA_ANONYMIZE_ENABLED',
|
||||
'hint' => $this->module->l('Use this option to anonymize the visitor’s IP to comply with data privacy laws in some countries'),
|
||||
'values' => [
|
||||
[
|
||||
'id' => 'ga_anonymize_enabled',
|
||||
'value' => 1,
|
||||
'label' => $this->module->l('Yes'),
|
||||
],
|
||||
[
|
||||
'id' => 'ga_anonymize_disabled',
|
||||
'value' => 0,
|
||||
'label' => $this->module->l('No'),
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'type' => 'switch',
|
||||
'label' => $this->module->l('Enable Back Office Tracking'),
|
||||
'name' => 'GA_TRACK_BACKOFFICE_ENABLED',
|
||||
'hint' => $this->module->l('Use this option to enable the tracking inside the Back Office'),
|
||||
'values' => [
|
||||
[
|
||||
'id' => 'ga_track_backoffice',
|
||||
'value' => 1,
|
||||
'label' => $this->module->l('Yes'),
|
||||
],
|
||||
[
|
||||
'id' => 'ga_do_not_track_backoffice',
|
||||
'value' => 0,
|
||||
'label' => $this->module->l('No'),
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'type' => 'select',
|
||||
'label' => $this->module->l('Cancelled order states'),
|
||||
'name' => 'GA_CANCELLED_STATES',
|
||||
'desc' => $this->module->l('Choose order states, in which you consider the given order cancelled. This will be usually only the default "Cancelled" state, but some shops may have extra states like "Returned" etc.'),
|
||||
'class' => 'chosen',
|
||||
'multiple' => true,
|
||||
'options' => [
|
||||
'query' => OrderState::getOrderStates((int) Context::getContext()->language->id),
|
||||
'id' => 'id_order_state',
|
||||
'name' => 'name',
|
||||
],
|
||||
],
|
||||
],
|
||||
'submit' => [
|
||||
'title' => $this->module->l('Save'),
|
||||
],
|
||||
];
|
||||
|
||||
if ($is_multistore_active) {
|
||||
$fields_form[0]['form']['input'][] = [
|
||||
'type' => 'switch',
|
||||
'label' => $this->module->l('Enable Cross-Domain tracking'),
|
||||
'name' => 'GA_CROSSDOMAIN_ENABLED',
|
||||
'values' => [
|
||||
[
|
||||
'id' => 'ga_crossdomain_enabled',
|
||||
'value' => 1,
|
||||
'label' => $this->module->l('Yes'),
|
||||
],
|
||||
[
|
||||
'id' => 'ga_crossdomain_disabled',
|
||||
'value' => 0,
|
||||
'label' => $this->module->l('No'),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
// Load current value
|
||||
$helper->fields_value['GA_ACCOUNT_ID'] = Configuration::get('GA_ACCOUNT_ID');
|
||||
$helper->fields_value['GA_USERID_ENABLED'] = Configuration::get('GA_USERID_ENABLED');
|
||||
$helper->fields_value['GA_CROSSDOMAIN_ENABLED'] = Configuration::get('GA_CROSSDOMAIN_ENABLED');
|
||||
$helper->fields_value['GA_ANONYMIZE_ENABLED'] = Configuration::get('GA_ANONYMIZE_ENABLED');
|
||||
$helper->fields_value['GA_TRACK_BACKOFFICE_ENABLED'] = Configuration::get('GA_TRACK_BACKOFFICE_ENABLED');
|
||||
$helper->fields_value['GA_CANCELLED_STATES[]'] = json_decode(Configuration::get('GA_CANCELLED_STATES'), true);
|
||||
|
||||
return $helper->generateForm($fields_form);
|
||||
}
|
||||
|
||||
/**
|
||||
* treat the form datas if submited
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function treat()
|
||||
{
|
||||
$treatmentResult = '';
|
||||
$gaAccountId = Tools::getValue('GA_ACCOUNT_ID');
|
||||
$gaUserIdEnabled = Tools::getValue('GA_USERID_ENABLED');
|
||||
$gaCrossdomainEnabled = Tools::getValue('GA_CROSSDOMAIN_ENABLED');
|
||||
$gaAnonymizeEnabled = Tools::getValue('GA_ANONYMIZE_ENABLED');
|
||||
$gaTrackBackOffice = Tools::getValue('GA_TRACK_BACKOFFICE_ENABLED');
|
||||
$gaCancelledStates = Tools::getValue('GA_CANCELLED_STATES');
|
||||
|
||||
if (!empty($gaAccountId)) {
|
||||
Configuration::updateValue('GA_ACCOUNT_ID', $gaAccountId);
|
||||
Configuration::updateValue('GANALYTICS_CONFIGURATION_OK', true);
|
||||
$treatmentResult .= $this->module->displayConfirmation($this->module->l('Account ID updated successfully'));
|
||||
}
|
||||
|
||||
if (null !== $gaUserIdEnabled) {
|
||||
Configuration::updateValue('GA_USERID_ENABLED', (bool) $gaUserIdEnabled);
|
||||
$treatmentResult .= $this->module->displayConfirmation($this->module->l('Settings for User ID updated successfully'));
|
||||
}
|
||||
|
||||
if (null !== $gaCrossdomainEnabled) {
|
||||
Configuration::updateValue('GA_CROSSDOMAIN_ENABLED', (bool) $gaCrossdomainEnabled);
|
||||
$treatmentResult .= $this->module->displayConfirmation($this->module->l('Settings for User ID updated successfully'));
|
||||
}
|
||||
|
||||
if (null !== $gaAnonymizeEnabled) {
|
||||
Configuration::updateValue('GA_ANONYMIZE_ENABLED', (bool) $gaAnonymizeEnabled);
|
||||
$treatmentResult .= $this->module->displayConfirmation($this->module->l('Settings for Anonymize IP updated successfully'));
|
||||
}
|
||||
|
||||
if (null !== $gaTrackBackOffice) {
|
||||
Configuration::updateValue('GA_TRACK_BACKOFFICE_ENABLED', (bool) $gaTrackBackOffice);
|
||||
$treatmentResult .= $this->module->displayConfirmation($this->module->l('Settings for Enable Back Office tracking updated successfully'));
|
||||
}
|
||||
|
||||
if ($gaCancelledStates === false) {
|
||||
Configuration::updateValue('GA_CANCELLED_STATES', '');
|
||||
} else {
|
||||
Configuration::updateValue('GA_CANCELLED_STATES', json_encode($gaCancelledStates));
|
||||
}
|
||||
$treatmentResult .= $this->module->displayConfirmation($this->module->l('Settings for cancelled order states updated successfully'));
|
||||
|
||||
return $treatmentResult;
|
||||
}
|
||||
}
|
||||
28
modules/ps_googleanalytics/classes/Form/index.php
Normal file
28
modules/ps_googleanalytics/classes/Form/index.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
header('Expires: Mon, 26 Jul 1998 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
147
modules/ps_googleanalytics/classes/GoogleAnalyticsTools.php
Normal file
147
modules/ps_googleanalytics/classes/GoogleAnalyticsTools.php
Normal file
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics;
|
||||
|
||||
class GoogleAnalyticsTools
|
||||
{
|
||||
/**
|
||||
* filter
|
||||
*
|
||||
* @param string $gaScripts
|
||||
* @param int $filterable
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function filter($gaScripts, $filterable)
|
||||
{
|
||||
if (1 == $filterable) {
|
||||
return implode(';', array_unique(explode(';', $gaScripts)));
|
||||
}
|
||||
|
||||
return $gaScripts;
|
||||
}
|
||||
|
||||
/**
|
||||
* add order transaction
|
||||
*
|
||||
* @param array $products
|
||||
* @param array $order
|
||||
*
|
||||
* @return string|void
|
||||
*/
|
||||
public function addTransaction($products, $order)
|
||||
{
|
||||
if (!is_array($products)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$js = '';
|
||||
foreach ($products as $product) {
|
||||
$js .= 'MBG.add(' . json_encode($product) . ');';
|
||||
}
|
||||
|
||||
return $js . 'MBG.addTransaction(' . json_encode($order) . ');';
|
||||
}
|
||||
|
||||
/**
|
||||
* add product impression js and product click js
|
||||
*
|
||||
* @param array $products
|
||||
*
|
||||
* @return string|void
|
||||
*/
|
||||
public function addProductImpression($products)
|
||||
{
|
||||
if (!is_array($products)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$js = '';
|
||||
foreach ($products as $product) {
|
||||
$js .= 'MBG.add(' . json_encode($product) . ",'',true);";
|
||||
}
|
||||
|
||||
return $js;
|
||||
}
|
||||
|
||||
/**
|
||||
* addProductClick
|
||||
*
|
||||
* @param array $products
|
||||
*
|
||||
* @return string|void
|
||||
*/
|
||||
public function addProductClick($products)
|
||||
{
|
||||
if (!is_array($products)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$js = '';
|
||||
foreach ($products as $product) {
|
||||
$js .= 'MBG.addProductClick(' . json_encode($product) . ');';
|
||||
}
|
||||
|
||||
return $js;
|
||||
}
|
||||
|
||||
/**
|
||||
* addProductClickByHttpReferal
|
||||
*
|
||||
* @param array $products
|
||||
*
|
||||
* @return string|void
|
||||
*/
|
||||
public function addProductClickByHttpReferal($products)
|
||||
{
|
||||
if (!is_array($products)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$js = '';
|
||||
foreach ($products as $product) {
|
||||
$js .= 'MBG.addProductClickByHttpReferal(' . json_encode($product) . ');';
|
||||
}
|
||||
|
||||
return $js;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add product checkout info
|
||||
*
|
||||
* @param array $products
|
||||
*
|
||||
* @return string|void
|
||||
*/
|
||||
public function addProductFromCheckout($products)
|
||||
{
|
||||
if (!is_array($products)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$js = '';
|
||||
foreach ($products as $product) {
|
||||
$js .= 'MBG.add(' . json_encode($product) . ');';
|
||||
}
|
||||
|
||||
return $js;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Handler;
|
||||
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Repository\GanalyticsDataRepository;
|
||||
|
||||
class GanalyticsDataHandler
|
||||
{
|
||||
private $ganalyticsDataRepository;
|
||||
private $cartId;
|
||||
private $shopId;
|
||||
|
||||
/**
|
||||
* __construct
|
||||
*
|
||||
* @param int $cartId
|
||||
* @param int $shopId
|
||||
*/
|
||||
public function __construct($cartId, $shopId)
|
||||
{
|
||||
$this->ganalyticsDataRepository = new GanalyticsDataRepository();
|
||||
$this->cartId = (int) $cartId;
|
||||
$this->shopId = (int) $shopId;
|
||||
}
|
||||
|
||||
/**
|
||||
* manageData
|
||||
*
|
||||
* @param string|array $data
|
||||
* @param string $action
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function manageData($data, $action)
|
||||
{
|
||||
if ('R' === $action) {
|
||||
return $this->readData();
|
||||
}
|
||||
|
||||
if ('W' === $action) {
|
||||
return $this->ganalyticsDataRepository->addNewRow(
|
||||
(int) $this->cartId,
|
||||
(int) $this->shopId,
|
||||
json_encode($data)
|
||||
);
|
||||
}
|
||||
|
||||
if ('A' === $action) {
|
||||
return $this->appendData($data);
|
||||
}
|
||||
|
||||
if ('D' === $action) {
|
||||
return $this->ganalyticsDataRepository->deleteRow(
|
||||
$this->cartId,
|
||||
$this->shopId
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* readData
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function readData()
|
||||
{
|
||||
$dataReturned = $this->ganalyticsDataRepository->findDataByCartIdAndShopId(
|
||||
$this->cartId,
|
||||
$this->shopId
|
||||
);
|
||||
|
||||
if (false === $dataReturned) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->jsonDecodeValidJson($dataReturned);
|
||||
}
|
||||
|
||||
/**
|
||||
* appendData
|
||||
*
|
||||
* @param string $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function appendData($data)
|
||||
{
|
||||
$dataReturned = $this->ganalyticsDataRepository->findDataByCartIdAndShopId(
|
||||
$this->cartId,
|
||||
$this->shopId
|
||||
);
|
||||
|
||||
if (false === $dataReturned) {
|
||||
$newData = [$data];
|
||||
} else {
|
||||
$newData[] = $this->jsonDecodeValidJson($dataReturned);
|
||||
}
|
||||
|
||||
return $this->ganalyticsDataRepository->addNewRow(
|
||||
(int) $this->cartId,
|
||||
(int) $this->shopId,
|
||||
json_encode($newData)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the json is valid and returns an empty array if not
|
||||
*
|
||||
* @param string $json
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function jsonDecodeValidJson($json)
|
||||
{
|
||||
$array = json_decode($json, true);
|
||||
|
||||
if (JSON_ERROR_NONE === json_last_error()) {
|
||||
return $array;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Handler;
|
||||
|
||||
use Configuration;
|
||||
use Context;
|
||||
use Ps_Googleanalytics;
|
||||
use Tools;
|
||||
|
||||
class GanalyticsJsHandler
|
||||
{
|
||||
private $module;
|
||||
private $context;
|
||||
|
||||
public function __construct(Ps_Googleanalytics $module, Context $context)
|
||||
{
|
||||
$this->module = $module;
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Google Analytics js
|
||||
*
|
||||
* @param string $jsCode
|
||||
* @param bool $isBackoffice
|
||||
*
|
||||
* @return void|string
|
||||
*/
|
||||
public function generate($jsCode, $isBackoffice = false)
|
||||
{
|
||||
if (Configuration::get('GA_ACCOUNT_ID')) {
|
||||
$this->context->smarty->assign(
|
||||
[
|
||||
'jsCode' => $jsCode,
|
||||
'isoCode' => Tools::safeOutput($this->context->currency->iso_code),
|
||||
'jsState' => $this->module->js_state,
|
||||
'isBackoffice' => $isBackoffice,
|
||||
]
|
||||
);
|
||||
|
||||
return $this->module->display(
|
||||
$this->module->getLocalPath() . $this->module->name,
|
||||
'ga_tag.tpl'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
98
modules/ps_googleanalytics/classes/Handler/ModuleHandler.php
Normal file
98
modules/ps_googleanalytics/classes/Handler/ModuleHandler.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Handler;
|
||||
|
||||
use Module;
|
||||
|
||||
class ModuleHandler
|
||||
{
|
||||
/**
|
||||
* @param string $moduleName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isModuleEnabled($moduleName)
|
||||
{
|
||||
$module = Module::getInstanceByName($moduleName);
|
||||
|
||||
if (!($module instanceof Module)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (false === Module::isInstalled($moduleName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (false === $module->active) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $moduleName
|
||||
* @param string $hookName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isModuleEnabledAndHookedOn($moduleName, $hookName)
|
||||
{
|
||||
$module = Module::getInstanceByName($moduleName);
|
||||
|
||||
if (false === $this->isModuleEnabled($moduleName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $module->isRegisteredInHook($hookName);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $moduleName
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function uninstallModule($moduleName)
|
||||
{
|
||||
if (false === Module::isInstalled($moduleName)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$oldModule = Module::getInstanceByName($moduleName);
|
||||
|
||||
if (!($oldModule instanceof Module)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (method_exists($oldModule, 'uninstallTab')) {
|
||||
$oldModule->uninstallTab();
|
||||
}
|
||||
|
||||
// This closure calls the parent class to prevent data to be erased
|
||||
$parentUninstallClosure = function () {
|
||||
return parent::uninstall();
|
||||
};
|
||||
|
||||
$parentUninstallClosure = $parentUninstallClosure->bindTo($oldModule, get_class($oldModule));
|
||||
|
||||
return $parentUninstallClosure();
|
||||
}
|
||||
}
|
||||
28
modules/ps_googleanalytics/classes/Handler/index.php
Normal file
28
modules/ps_googleanalytics/classes/Handler/index.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
header('Expires: Mon, 26 Jul 1998 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
|
||||
|
||||
use Context;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsDataHandler;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Repository\CarrierRepository;
|
||||
use Ps_Googleanalytics;
|
||||
|
||||
class HookActionCarrierProcess implements HookInterface
|
||||
{
|
||||
private $module;
|
||||
private $context;
|
||||
private $params;
|
||||
|
||||
public function __construct(Ps_Googleanalytics $module, Context $context)
|
||||
{
|
||||
$this->module = $module;
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* run
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
if (isset($this->params['cart']->id_carrier)) {
|
||||
$carrierRepository = new CarrierRepository();
|
||||
$ganalyticsDataHandler = new GanalyticsDataHandler(
|
||||
$this->context->cart->id,
|
||||
$this->context->shop->id
|
||||
);
|
||||
|
||||
$carrierName = $carrierRepository->findByCarrierId((int) $this->params['cart']->id_carrier);
|
||||
$ganalyticsDataHandler->manageData('MBG.addCheckoutOption(2,\'' . $carrierName . '\');', 'A');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* setParams
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function setParams($params)
|
||||
{
|
||||
$this->params = $params;
|
||||
}
|
||||
}
|
||||
136
modules/ps_googleanalytics/classes/Hook/HookActionCartSave.php
Normal file
136
modules/ps_googleanalytics/classes/Hook/HookActionCartSave.php
Normal file
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
|
||||
|
||||
use Configuration;
|
||||
use Context;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsDataHandler;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
|
||||
use Product;
|
||||
use Ps_Googleanalytics;
|
||||
use Tools;
|
||||
use Validate;
|
||||
|
||||
class HookActionCartSave implements HookInterface
|
||||
{
|
||||
private $module;
|
||||
|
||||
/**
|
||||
* @var Context
|
||||
*/
|
||||
private $context;
|
||||
|
||||
public function __construct(Ps_Googleanalytics $module, Context $context)
|
||||
{
|
||||
$this->module = $module;
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* run
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
if (!isset($this->context->cart)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Tools::getIsset('id_product')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$cart = [
|
||||
'controller' => Tools::getValue('controller'),
|
||||
'addAction' => Tools::getValue('add') ? 'add' : '',
|
||||
'removeAction' => Tools::getValue('delete') ? 'delete' : '',
|
||||
'extraAction' => Tools::getValue('op'),
|
||||
'qty' => (int) Tools::getValue('qty', 1),
|
||||
];
|
||||
|
||||
$cartProducts = $this->context->cart->getProducts();
|
||||
if (!empty($cartProducts)) {
|
||||
foreach ($cartProducts as $cartProduct) {
|
||||
if ($cartProduct['id_product'] == Tools::getValue('id_product')) {
|
||||
$addProduct = $cartProduct;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($cart['removeAction'] == 'delete') {
|
||||
$addProductObject = new Product((int) Tools::getValue('id_product'), true, (int) Configuration::get('PS_LANG_DEFAULT'));
|
||||
if (Validate::isLoadedObject($addProductObject)) {
|
||||
$addProduct['name'] = $addProductObject->name;
|
||||
$addProduct['manufacturer_name'] = $addProductObject->manufacturer_name;
|
||||
$addProduct['category'] = $addProductObject->category;
|
||||
$addProduct['reference'] = $addProductObject->reference;
|
||||
$addProduct['link_rewrite'] = $addProductObject->link_rewrite;
|
||||
$addProduct['link'] = $addProductObject->link_rewrite;
|
||||
$addProduct['price'] = $addProductObject->price;
|
||||
$addProduct['ean13'] = $addProductObject->ean13;
|
||||
$addProduct['id_product'] = Tools::getValue('id_product');
|
||||
$addProduct['id_category_default'] = $addProductObject->id_category_default;
|
||||
$addProduct['out_of_stock'] = $addProductObject->out_of_stock;
|
||||
$addProduct['minimal_quantity'] = 1;
|
||||
$addProduct['unit_price_ratio'] = 0;
|
||||
$addProduct = Product::getProductProperties((int) Configuration::get('PS_LANG_DEFAULT'), $addProduct);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($addProduct) && !in_array((int) Tools::getValue('id_product'), $this->module->products)) {
|
||||
$ganalyticsDataHandler = new GanalyticsDataHandler(
|
||||
$this->context->cart->id,
|
||||
$this->context->shop->id
|
||||
);
|
||||
|
||||
$this->module->products[] = (int) Tools::getValue('id_product');
|
||||
$productWrapper = new ProductWrapper($this->context);
|
||||
$gaProducts = $productWrapper->wrapProduct($addProduct, $cart, 0, true);
|
||||
|
||||
if (array_key_exists('id_product_attribute', $gaProducts) && $gaProducts['id_product_attribute'] != '' && $gaProducts['id_product_attribute'] != 0) {
|
||||
$productId = $gaProducts['id_product_attribute'];
|
||||
} else {
|
||||
$productId = Tools::getValue('id_product');
|
||||
}
|
||||
|
||||
$gaCart = $ganalyticsDataHandler->manageData('', 'R');
|
||||
|
||||
if ($cart['removeAction'] == 'delete') {
|
||||
$gaProducts['quantity'] = -1;
|
||||
} elseif ($cart['extraAction'] == 'down') {
|
||||
if (array_key_exists($productId, $gaCart)) {
|
||||
$gaProducts['quantity'] = $gaCart[$productId]['quantity'] - $cart['qty'];
|
||||
} else {
|
||||
$gaProducts['quantity'] = $cart['qty'] * -1;
|
||||
}
|
||||
} elseif (Tools::getValue('step') <= 0) { // Sometimes cartsave is called in checkout
|
||||
if (array_key_exists($productId, $gaCart)) {
|
||||
$gaProducts['quantity'] = $gaCart[$productId]['quantity'] + $cart['qty'];
|
||||
}
|
||||
}
|
||||
|
||||
$gaCart[$productId] = $gaProducts;
|
||||
$ganalyticsDataHandler->manageData($gaCart, 'W');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
|
||||
|
||||
use Configuration;
|
||||
use Context;
|
||||
use Db;
|
||||
use Ps_Googleanalytics;
|
||||
|
||||
class HookActionOrderStatusPostUpdate implements HookInterface
|
||||
{
|
||||
/**
|
||||
* @var Ps_Googleanalytics
|
||||
*/
|
||||
private $module;
|
||||
|
||||
/**
|
||||
* @var Context
|
||||
*/
|
||||
private $context;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $params;
|
||||
|
||||
public function __construct(Ps_Googleanalytics $module, Context $context)
|
||||
{
|
||||
$this->module = $module;
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* run
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
// If we do not have an order or a new order status, we return
|
||||
if (empty($this->params['id_order']) || empty($this->params['newOrderStatus']->id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// We get all states in which the merchant want to have refund sent and check if the new state being set belongs there
|
||||
$gaCancelledStates = json_decode(Configuration::get('GA_CANCELLED_STATES'), true);
|
||||
if (empty($gaCancelledStates) || !in_array($this->params['newOrderStatus']->id, $gaCancelledStates)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// We check if the refund was already sent to Google Analytics
|
||||
$gaRefundSent = Db::getInstance()->getValue(
|
||||
'SELECT id_order FROM `' . _DB_PREFIX_ . 'ganalytics` WHERE id_order = ' . (int) $this->params['id_order'] . ' AND refund_sent = 1'
|
||||
);
|
||||
|
||||
// If it was not already refunded
|
||||
if ($gaRefundSent === false) {
|
||||
// We refund it and set the "sent" flag to true
|
||||
$this->context->cookie->__set('ga_admin_refund', 'MBG.refundByOrderId(' . json_encode(['id' => $this->params['id_order']]) . ');');
|
||||
$this->context->cookie->write();
|
||||
|
||||
// We save this information to database
|
||||
Db::getInstance()->execute(
|
||||
'UPDATE `' . _DB_PREFIX_ . 'ganalytics` SET refund_sent = 1 WHERE id_order = ' . (int) $this->params['id_order']
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* setParams
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function setParams($params)
|
||||
{
|
||||
$this->params = $params;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
|
||||
|
||||
use Context;
|
||||
use OrderDetail;
|
||||
use Ps_Googleanalytics;
|
||||
|
||||
class HookActionProductCancel implements HookInterface
|
||||
{
|
||||
private $module;
|
||||
private $context;
|
||||
private $params;
|
||||
|
||||
public function __construct(Ps_Googleanalytics $module, Context $context)
|
||||
{
|
||||
$this->module = $module;
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* run
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
if (!isset($this->params['id_order_detail']) || !isset($this->params['cancel_quantity'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Display GA refund product
|
||||
$orderDetail = new OrderDetail($this->params['id_order_detail']);
|
||||
$gaScripts = 'MBG.add(' . json_encode(
|
||||
[
|
||||
'id' => empty($orderDetail->product_attribute_id) ? $orderDetail->product_id : $orderDetail->product_id . '-' . $orderDetail->product_attribute_id,
|
||||
'quantity' => $this->params['cancel_quantity'],
|
||||
])
|
||||
. ');';
|
||||
|
||||
$this->context->cookie->__set(
|
||||
'ga_admin_refund',
|
||||
$gaScripts . 'MBG.refundByProduct(' . json_encode(['id' => $this->params['order']->id]) . ');'
|
||||
);
|
||||
$this->context->cookie->write();
|
||||
}
|
||||
|
||||
/**
|
||||
* setParams
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function setParams($params)
|
||||
{
|
||||
$this->params = $params;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
|
||||
|
||||
use Configuration;
|
||||
use Context;
|
||||
use Order;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Repository\GanalyticsRepository;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\OrderWrapper;
|
||||
use Ps_Googleanalytics;
|
||||
use Tools;
|
||||
use Validate;
|
||||
|
||||
class HookDisplayBackOfficeHeader implements HookInterface
|
||||
{
|
||||
private $module;
|
||||
private $context;
|
||||
|
||||
public function __construct(Ps_Googleanalytics $module, Context $context)
|
||||
{
|
||||
$this->module = $module;
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* run
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$js = '';
|
||||
if (strcmp(Tools::getValue('configure'), $this->module->name) === 0) {
|
||||
$this->context->controller->addCSS($this->module->getPathUri() . 'views/css/ganalytics.css');
|
||||
}
|
||||
|
||||
$ga_account_id = Configuration::get('GA_ACCOUNT_ID');
|
||||
|
||||
if (!empty($ga_account_id) && $this->module->active) {
|
||||
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
|
||||
$this->context->controller->addJs($this->module->getPathUri() . 'views/js/GoogleAnalyticActionLib.js');
|
||||
|
||||
$this->context->smarty->assign('GA_ACCOUNT_ID', $ga_account_id);
|
||||
|
||||
$gaScripts = '';
|
||||
if ($this->context->controller->controller_name == 'AdminOrders') {
|
||||
$ganalyticsRepository = new GanalyticsRepository();
|
||||
|
||||
if (Tools::getValue('id_order')) {
|
||||
$order = new Order((int) Tools::getValue('id_order'));
|
||||
if (Validate::isLoadedObject($order) && strtotime('+1 day', strtotime($order->date_add)) > time()) {
|
||||
$gaOrderSent = $ganalyticsRepository->findGaOrderByOrderId((int) Tools::getValue('id_order'));
|
||||
if ($gaOrderSent === false) {
|
||||
$ganalyticsRepository->addNewRow(
|
||||
[
|
||||
'id_order' => (int) Tools::getValue('id_order'),
|
||||
'id_shop' => (int) $this->context->shop->id,
|
||||
'sent' => 0,
|
||||
'date_add' => ['value' => 'NOW()', 'type' => 'sql'],
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$gaOrderRecords = $ganalyticsRepository->findAllByShopIdAndDateAdd((int) $this->context->shop->id);
|
||||
|
||||
if ($gaOrderRecords) {
|
||||
$orderWrapper = new OrderWrapper($this->context);
|
||||
foreach ($gaOrderRecords as $row) {
|
||||
$transaction = $orderWrapper->wrapOrder($row['id_order']);
|
||||
if (!empty($transaction)) {
|
||||
$ganalyticsRepository->updateData(
|
||||
[
|
||||
'date_add' => ['value' => 'NOW()', 'type' => 'sql'],
|
||||
'sent' => 1,
|
||||
],
|
||||
'id_order = ' . (int) $row['id_order'] . ' AND id_shop = ' . (int) $this->context->shop->id
|
||||
);
|
||||
$transaction = json_encode($transaction);
|
||||
$gaScripts .= 'MBG.addTransaction(' . $transaction . ');';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $js . $this->module->hookdisplayHeader(null, true) . $gaTagHandler->generate($gaScripts, true);
|
||||
}
|
||||
|
||||
return $js;
|
||||
}
|
||||
}
|
||||
117
modules/ps_googleanalytics/classes/Hook/HookDisplayFooter.php
Normal file
117
modules/ps_googleanalytics/classes/Hook/HookDisplayFooter.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
|
||||
|
||||
use Context;
|
||||
use Hook;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\GoogleAnalyticsTools;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsDataHandler;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
|
||||
use Ps_Googleanalytics;
|
||||
use RecursiveArrayIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
use Tools;
|
||||
|
||||
class HookDisplayFooter implements HookInterface
|
||||
{
|
||||
private $module;
|
||||
private $context;
|
||||
|
||||
public function __construct(Ps_Googleanalytics $module, Context $context)
|
||||
{
|
||||
$this->module = $module;
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* run
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$gaTools = new GoogleAnalyticsTools();
|
||||
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
|
||||
$ganalyticsDataHandler = new GanalyticsDataHandler(
|
||||
$this->context->cart->id,
|
||||
$this->context->shop->id
|
||||
);
|
||||
|
||||
$gaScripts = '';
|
||||
$this->module->js_state = 0;
|
||||
$gacarts = $ganalyticsDataHandler->manageData('', 'R');
|
||||
$controller_name = Tools::getValue('controller');
|
||||
|
||||
if (count($gacarts) > 0 && $controller_name != 'product') {
|
||||
$this->module->filterable = 0;
|
||||
|
||||
foreach ($gacarts as $gacart) {
|
||||
if (isset($gacart['quantity'])) {
|
||||
if ($gacart['quantity'] > 0) {
|
||||
$gaScripts .= 'MBG.addToCart(' . json_encode($gacart) . ');';
|
||||
} elseif ($gacart['quantity'] < 0) {
|
||||
$gacart['quantity'] = abs($gacart['quantity']);
|
||||
$gaScripts .= 'MBG.removeFromCart(' . json_encode($gacart) . ');';
|
||||
}
|
||||
} elseif (is_array($gacart)) {
|
||||
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($gacart));
|
||||
foreach ($it as $v) {
|
||||
$gaScripts .= $v;
|
||||
}
|
||||
} else {
|
||||
$gaScripts .= $gacart;
|
||||
}
|
||||
}
|
||||
|
||||
$ganalyticsDataHandler->manageData('', 'D');
|
||||
}
|
||||
|
||||
$listing = $this->context->smarty->getTemplateVars('listing');
|
||||
$productWrapper = new ProductWrapper($this->context);
|
||||
$products = $productWrapper->wrapProductList(isset($listing['products']) ? $listing['products'] : [], [], true);
|
||||
|
||||
if ($controller_name == 'order' || $controller_name == 'orderopc') {
|
||||
$this->module->js_state = 1;
|
||||
$this->module->eligible = 1;
|
||||
$step = Tools::getValue('step');
|
||||
if (empty($step)) {
|
||||
$step = 0;
|
||||
}
|
||||
$gaScripts .= $gaTools->addProductFromCheckout($products);
|
||||
$gaScripts .= 'MBG.addCheckout(\'' . (int) $step . '\');';
|
||||
}
|
||||
|
||||
$confirmation_hook_id = (int) Hook::getIdByName('displayOrderConfirmation');
|
||||
if (isset(Hook::$executed_hooks[$confirmation_hook_id])) {
|
||||
$this->module->eligible = 1;
|
||||
}
|
||||
|
||||
if (isset($products) && count($products) && $controller_name != 'index') {
|
||||
if ($this->module->eligible == 0) {
|
||||
$gaScripts .= $gaTools->addProductImpression($products);
|
||||
}
|
||||
$gaScripts .= $gaTools->addProductClick($products);
|
||||
}
|
||||
|
||||
return $gaTagHandler->generate($gaScripts);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
|
||||
|
||||
use Context;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\GoogleAnalyticsTools;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
|
||||
use Product;
|
||||
use Ps_Googleanalytics;
|
||||
use Tools;
|
||||
|
||||
class HookDisplayFooterProduct implements HookInterface
|
||||
{
|
||||
private $module;
|
||||
private $context;
|
||||
private $params;
|
||||
|
||||
public function __construct(Ps_Googleanalytics $module, Context $context)
|
||||
{
|
||||
$this->module = $module;
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* run
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$gaTools = new GoogleAnalyticsTools();
|
||||
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
|
||||
$controllerName = Tools::getValue('controller');
|
||||
|
||||
if ('product' !== $controllerName) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($this->params['product'] instanceof Product) {
|
||||
$this->params['product'] = (array) $this->params['product'];
|
||||
}
|
||||
// Add product view
|
||||
$productWrapper = new ProductWrapper($this->context);
|
||||
$gaProduct = $productWrapper->wrapProduct($this->params['product'], null, 0, true);
|
||||
$js = 'MBG.addProductDetailView(' . json_encode($gaProduct) . ');';
|
||||
|
||||
if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) > 0) {
|
||||
$js .= $gaTools->addProductClickByHttpReferal([$gaProduct]);
|
||||
}
|
||||
|
||||
$this->module->js_state = 1;
|
||||
|
||||
return $gaTagHandler->generate($js);
|
||||
}
|
||||
|
||||
/**
|
||||
* setParams
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function setParams($params)
|
||||
{
|
||||
$this->params = $params;
|
||||
}
|
||||
}
|
||||
122
modules/ps_googleanalytics/classes/Hook/HookDisplayHeader.php
Normal file
122
modules/ps_googleanalytics/classes/Hook/HookDisplayHeader.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
|
||||
|
||||
use Configuration;
|
||||
use Context;
|
||||
use Ps_Googleanalytics;
|
||||
use Shop;
|
||||
use Tools;
|
||||
|
||||
class HookDisplayHeader implements HookInterface
|
||||
{
|
||||
private $module;
|
||||
private $context;
|
||||
private $params;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $backOffice;
|
||||
|
||||
public function __construct(Ps_Googleanalytics $module, Context $context)
|
||||
{
|
||||
$this->module = $module;
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* run
|
||||
*
|
||||
* @return void|string
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
if (!Configuration::get('GA_ACCOUNT_ID')) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$this->context->controller->addJs($this->module->getPathUri() . 'views/js/GoogleAnalyticActionLib.js');
|
||||
|
||||
$shops = Shop::getShops();
|
||||
$isMultistoreActive = Shop::isFeatureActive();
|
||||
$currentShopId = (int) Context::getContext()->shop->id;
|
||||
$userId = null;
|
||||
$gaCrossdomainEnabled = false;
|
||||
|
||||
if (Configuration::get('GA_USERID_ENABLED') &&
|
||||
$this->context->customer && $this->context->customer->isLogged()
|
||||
) {
|
||||
$userId = (int) $this->context->customer->id;
|
||||
}
|
||||
|
||||
$gaAnonymizeEnabled = Configuration::get('GA_ANONYMIZE_ENABLED');
|
||||
|
||||
if ((int) Configuration::get('GA_CROSSDOMAIN_ENABLED') && $isMultistoreActive && count($shops) > 1) {
|
||||
$gaCrossdomainEnabled = true;
|
||||
}
|
||||
|
||||
$this->context->smarty->assign(
|
||||
[
|
||||
'backOffice' => $this->backOffice,
|
||||
'trackBackOffice' => Configuration::get('GA_TRACK_BACKOFFICE_ENABLED'),
|
||||
'currentShopId' => $currentShopId,
|
||||
'userId' => $userId,
|
||||
'gaAccountId' => Tools::safeOutput(Configuration::get('GA_ACCOUNT_ID')),
|
||||
'shops' => $shops,
|
||||
'gaCrossdomainEnabled' => $gaCrossdomainEnabled,
|
||||
'gaAnonymizeEnabled' => $gaAnonymizeEnabled,
|
||||
'useSecureMode' => Configuration::get('PS_SSL_ENABLED'),
|
||||
]
|
||||
);
|
||||
|
||||
return $this->module->display(
|
||||
$this->module->getLocalPath() . $this->module->name,
|
||||
'ps_googleanalytics.tpl'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* setParams
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function setParams($params)
|
||||
{
|
||||
$this->module->params = $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $backOffice
|
||||
*/
|
||||
public function setBackOffice($backOffice)
|
||||
{
|
||||
$this->acknowledgeBackOfficeContext($backOffice);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $isBackOffice
|
||||
*/
|
||||
public function acknowledgeBackOfficeContext($isBackOffice)
|
||||
{
|
||||
$this->backOffice = $isBackOffice;
|
||||
}
|
||||
}
|
||||
78
modules/ps_googleanalytics/classes/Hook/HookDisplayHome.php
Normal file
78
modules/ps_googleanalytics/classes/Hook/HookDisplayHome.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
|
||||
|
||||
use Category;
|
||||
use Configuration;
|
||||
use Context;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\GoogleAnalyticsTools;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Handler\ModuleHandler;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
|
||||
use Ps_Googleanalytics;
|
||||
|
||||
class HookDisplayHome implements HookInterface
|
||||
{
|
||||
private $module;
|
||||
private $context;
|
||||
|
||||
public function __construct(Ps_Googleanalytics $module, Context $context)
|
||||
{
|
||||
$this->module = $module;
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* run
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$moduleHandler = new ModuleHandler();
|
||||
$gaTools = new GoogleAnalyticsTools();
|
||||
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
|
||||
$gaScripts = '';
|
||||
|
||||
// Home featured products
|
||||
if ($moduleHandler->isModuleEnabledAndHookedOn('ps_featuredproducts', 'displayHome')) {
|
||||
$category = new Category($this->context->shop->getCategory(), $this->context->language->id);
|
||||
$productWrapper = new ProductWrapper($this->context);
|
||||
$homeFeaturedProducts = $productWrapper->wrapProductList(
|
||||
$category->getProducts(
|
||||
(int) $this->context->language->id,
|
||||
1,
|
||||
(Configuration::get('HOME_FEATURED_NBR') ? (int) Configuration::get('HOME_FEATURED_NBR') : 8),
|
||||
'position'
|
||||
),
|
||||
[],
|
||||
true
|
||||
);
|
||||
$gaScripts .= $gaTools->addProductImpression($homeFeaturedProducts) . $gaTools->addProductClick($homeFeaturedProducts);
|
||||
}
|
||||
|
||||
$this->module->js_state = 1;
|
||||
|
||||
return $gaTagHandler->generate(
|
||||
$gaTools->filter($gaScripts, $this->module->filterable)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
|
||||
|
||||
use Cart;
|
||||
use Configuration;
|
||||
use Context;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\GoogleAnalyticsTools;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Repository\GanalyticsRepository;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
|
||||
use Ps_Googleanalytics;
|
||||
use Shop;
|
||||
use Validate;
|
||||
|
||||
class HookDisplayOrderConfirmation implements HookInterface
|
||||
{
|
||||
private $module;
|
||||
private $context;
|
||||
private $params;
|
||||
|
||||
public function __construct(Ps_Googleanalytics $module, Context $context)
|
||||
{
|
||||
$this->module = $module;
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* run
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
if (true === $this->module->psVersionIs17) {
|
||||
$order = $this->params['order'];
|
||||
} else {
|
||||
$order = $this->params['objOrder'];
|
||||
}
|
||||
|
||||
if (Validate::isLoadedObject($order) && $order->getCurrentState() != (int) Configuration::get('PS_OS_ERROR')) {
|
||||
$ganalyticsRepository = new GanalyticsRepository();
|
||||
$gaOrderSent = $ganalyticsRepository->findGaOrderByOrderId((int) $order->id);
|
||||
|
||||
if (false === $gaOrderSent) {
|
||||
$ganalyticsRepository->addNewRow(
|
||||
[
|
||||
'id_order' => (int) $order->id,
|
||||
'id_shop' => (int) $this->context->shop->id,
|
||||
'sent' => 0,
|
||||
'date_add' => ['value' => 'NOW()', 'type' => 'sql'],
|
||||
]
|
||||
);
|
||||
|
||||
if ($order->id_customer == $this->context->cookie->id_customer) {
|
||||
$orderProducts = [];
|
||||
$cart = new Cart($order->id_cart);
|
||||
$gaTools = new GoogleAnalyticsTools();
|
||||
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
|
||||
$productWrapper = new ProductWrapper($this->context);
|
||||
|
||||
foreach ($cart->getProducts() as $order_product) {
|
||||
$orderProducts[] = $productWrapper->wrapProduct($order_product, [], 0, true);
|
||||
}
|
||||
|
||||
$gaScripts = 'MBG.addCheckoutOption(3,\'' . $order->payment . '\');';
|
||||
$transaction = [
|
||||
'id' => $order->id,
|
||||
'affiliation' => (Shop::isFeatureActive()) ? $this->context->shop->name : Configuration::get('PS_SHOP_NAME'),
|
||||
'revenue' => $order->total_paid,
|
||||
'shipping' => $order->total_shipping,
|
||||
'tax' => $order->total_paid_tax_incl - $order->total_paid_tax_excl,
|
||||
'url' => $this->context->link->getModuleLink('ps_googleanalytics', 'ajax', [], true),
|
||||
'customer' => $order->id_customer, ];
|
||||
$gaScripts .= $gaTools->addTransaction($orderProducts, $transaction);
|
||||
|
||||
$this->module->js_state = 1;
|
||||
|
||||
return $gaTagHandler->generate($gaScripts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* setParams
|
||||
*
|
||||
* @param array $params
|
||||
*/
|
||||
public function setParams($params)
|
||||
{
|
||||
$this->params = $params;
|
||||
}
|
||||
}
|
||||
35
modules/ps_googleanalytics/classes/Hook/HookInterface.php
Normal file
35
modules/ps_googleanalytics/classes/Hook/HookInterface.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
|
||||
|
||||
use Context;
|
||||
use Ps_Googleanalytics;
|
||||
|
||||
interface HookInterface
|
||||
{
|
||||
/**
|
||||
* @param Ps_Googleanalytics $module
|
||||
* @param Context $context
|
||||
*/
|
||||
public function __construct(Ps_Googleanalytics $module, Context $context);
|
||||
|
||||
public function run();
|
||||
}
|
||||
28
modules/ps_googleanalytics/classes/Hook/index.php
Normal file
28
modules/ps_googleanalytics/classes/Hook/index.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
header('Expires: Mon, 26 Jul 1998 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Repository;
|
||||
|
||||
use Db;
|
||||
|
||||
class CarrierRepository
|
||||
{
|
||||
const TABLE_NAME = 'carrier';
|
||||
|
||||
/**
|
||||
* findByCarrierId
|
||||
*
|
||||
* @param int $carrierId
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function findByCarrierId($carrierId)
|
||||
{
|
||||
return Db::getInstance()->getValue(
|
||||
'SELECT name
|
||||
FROM `' . _DB_PREFIX_ . self::TABLE_NAME . '`
|
||||
WHERE id_carrier = ' . (int) $carrierId
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Repository;
|
||||
|
||||
use Db;
|
||||
|
||||
class GanalyticsDataRepository
|
||||
{
|
||||
const TABLE_NAME = 'ganalytics_data';
|
||||
|
||||
/**
|
||||
* findByCartId
|
||||
*
|
||||
* @param int $cartId
|
||||
* @param int $shopId
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function findDataByCartIdAndShopId($cartId, $shopId)
|
||||
{
|
||||
return Db::getInstance()->getValue(
|
||||
'SELECT data
|
||||
FROM `' . _DB_PREFIX_ . self::TABLE_NAME . '`
|
||||
WHERE id_cart = ' . (int) $cartId . '
|
||||
AND id_shop = ' . (int) $shopId
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* addNewRow
|
||||
*
|
||||
* @param int $cartId
|
||||
* @param int $shopId
|
||||
* @param string $data
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function addNewRow($cartId, $shopId, $data)
|
||||
{
|
||||
return Db::getInstance()->Execute(
|
||||
'INSERT INTO `' . _DB_PREFIX_ . self::TABLE_NAME . '` (id_cart, id_shop, data)
|
||||
VALUES(\'' . (int) $cartId . '\',\'' . (int) $shopId . '\',\'' . pSQL($data) . '\')
|
||||
ON DUPLICATE KEY UPDATE data = \'' . pSQL($data) . '\';'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* deleteRow
|
||||
*
|
||||
* @param int $cartId
|
||||
* @param int $shopId
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteRow($cartId, $shopId)
|
||||
{
|
||||
return Db::getInstance()->delete(
|
||||
self::TABLE_NAME,
|
||||
'id_cart = ' . (int) $cartId . ' AND id_shop = ' . (int) $shopId
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Repository;
|
||||
|
||||
use Db;
|
||||
|
||||
class GanalyticsRepository
|
||||
{
|
||||
const TABLE_NAME = 'ganalytics';
|
||||
|
||||
/**
|
||||
* findGaOrderByOrderId
|
||||
*
|
||||
* @param int $orderId
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function findGaOrderByOrderId($orderId)
|
||||
{
|
||||
return Db::getInstance()->getValue(
|
||||
'SELECT id_order
|
||||
FROM `' . _DB_PREFIX_ . self::TABLE_NAME . '`
|
||||
WHERE id_order = ' . (int) $orderId);
|
||||
}
|
||||
|
||||
/**
|
||||
* findAllByShopIdAndDateAdd
|
||||
*
|
||||
* @param int $shopId
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function findAllByShopIdAndDateAdd($shopId)
|
||||
{
|
||||
return Db::getInstance()->ExecuteS(
|
||||
'SELECT *
|
||||
FROM `' . _DB_PREFIX_ . self::TABLE_NAME . '`
|
||||
WHERE sent = 0
|
||||
AND id_shop = ' . (int) $shopId . '
|
||||
AND DATE_ADD(date_add, INTERVAL 30 minute) < NOW()'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* addNewRow
|
||||
*
|
||||
* @param array $data
|
||||
* @param int $type
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function addNewRow(array $data, $type = Db::INSERT_IGNORE)
|
||||
{
|
||||
return Db::getInstance()->insert(
|
||||
self::TABLE_NAME,
|
||||
$data,
|
||||
false,
|
||||
true,
|
||||
$type
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* updateData
|
||||
*
|
||||
* @param array $data
|
||||
* @param string $where
|
||||
* @param int $limit
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function updateData($data, $where, $limit = 0)
|
||||
{
|
||||
return Db::getInstance()->update(
|
||||
self::TABLE_NAME,
|
||||
$data,
|
||||
$where,
|
||||
$limit
|
||||
);
|
||||
}
|
||||
}
|
||||
28
modules/ps_googleanalytics/classes/Repository/index.php
Normal file
28
modules/ps_googleanalytics/classes/Repository/index.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
header('Expires: Mon, 26 Jul 1998 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
58
modules/ps_googleanalytics/classes/Wrapper/OrderWrapper.php
Normal file
58
modules/ps_googleanalytics/classes/Wrapper/OrderWrapper.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Wrapper;
|
||||
|
||||
use Configuration;
|
||||
use Context;
|
||||
use Order;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Hooks\WrapperInterface;
|
||||
use Shop;
|
||||
use Validate;
|
||||
|
||||
class OrderWrapper implements WrapperInterface
|
||||
{
|
||||
private $context;
|
||||
|
||||
public function __construct(Context $context)
|
||||
{
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a detailed transaction for Google Analytics
|
||||
*/
|
||||
public function wrapOrder($id_order)
|
||||
{
|
||||
$order = new Order((int) $id_order);
|
||||
|
||||
if (Validate::isLoadedObject($order)) {
|
||||
return [
|
||||
'id' => $id_order,
|
||||
'affiliation' => Shop::isFeatureActive() ? $this->context->shop->name : Configuration::get('PS_SHOP_NAME'),
|
||||
'revenue' => $order->total_paid,
|
||||
'shipping' => $order->total_shipping,
|
||||
'tax' => $order->total_paid_tax_incl - $order->total_paid_tax_excl,
|
||||
'url' => $this->context->link->getAdminLink('AdminGanalyticsAjax'),
|
||||
'customer' => $order->id_customer,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
133
modules/ps_googleanalytics/classes/Wrapper/ProductWrapper.php
Normal file
133
modules/ps_googleanalytics/classes/Wrapper/ProductWrapper.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Wrapper;
|
||||
|
||||
use Context;
|
||||
use Currency;
|
||||
use PrestaShop\Module\Ps_Googleanalytics\Hooks\WrapperInterface;
|
||||
use Product;
|
||||
use Tools;
|
||||
|
||||
class ProductWrapper implements WrapperInterface
|
||||
{
|
||||
private $context;
|
||||
|
||||
public function __construct(Context $context)
|
||||
{
|
||||
$this->context = $context;
|
||||
}
|
||||
|
||||
/**
|
||||
* wrap products to provide a standard products information for google analytics script
|
||||
*/
|
||||
public function wrapProductList($products, $extras = [], $full = false)
|
||||
{
|
||||
$result_products = [];
|
||||
if (!is_array($products)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$currency = new Currency($this->context->currency->id);
|
||||
$usetax = (Product::getTaxCalculationMethod((int) $this->context->customer->id) != PS_TAX_EXC);
|
||||
|
||||
if (count($products) > 20) {
|
||||
$full = false;
|
||||
} else {
|
||||
$full = true;
|
||||
}
|
||||
|
||||
foreach ($products as $index => $product) {
|
||||
if ($product instanceof Product) {
|
||||
$product = (array) $product;
|
||||
}
|
||||
|
||||
if (!isset($product['price'])) {
|
||||
$product['price'] = (float) Tools::displayPrice(Product::getPriceStatic((int) $product['id_product'], $usetax), $currency);
|
||||
}
|
||||
$result_products[] = $this->wrapProduct($product, $extras, $index, $full);
|
||||
}
|
||||
|
||||
return $result_products;
|
||||
}
|
||||
|
||||
/**
|
||||
* wrap product to provide a standard product information for google analytics script
|
||||
*/
|
||||
public function wrapProduct($product, $extras, $index = 0, $full = false)
|
||||
{
|
||||
$ga_product = '';
|
||||
|
||||
$variant = null;
|
||||
if (isset($product['attributes_small'])) {
|
||||
$variant = $product['attributes_small'];
|
||||
} elseif (isset($extras['attributes_small'])) {
|
||||
$variant = $extras['attributes_small'];
|
||||
}
|
||||
|
||||
$product_qty = 1;
|
||||
if (isset($extras['qty'])) {
|
||||
$product_qty = $extras['qty'];
|
||||
} elseif (isset($product['cart_quantity'])) {
|
||||
$product_qty = $product['cart_quantity'];
|
||||
}
|
||||
|
||||
$product_id = 0;
|
||||
if (!empty($product['id_product'])) {
|
||||
$product_id = $product['id_product'];
|
||||
} elseif (!empty($product['id'])) {
|
||||
$product_id = $product['id'];
|
||||
}
|
||||
|
||||
if (!empty($product['id_product_attribute'])) {
|
||||
$product_id .= '-' . $product['id_product_attribute'];
|
||||
}
|
||||
|
||||
$product_type = 'typical';
|
||||
if (isset($product['pack']) && $product['pack'] == 1) {
|
||||
$product_type = 'pack';
|
||||
} elseif (isset($product['virtual']) && $product['virtual'] == 1) {
|
||||
$product_type = 'virtual';
|
||||
}
|
||||
|
||||
if ($full) {
|
||||
$ga_product = [
|
||||
'id' => $product_id,
|
||||
'name' => Tools::str2url($product['name']),
|
||||
'category' => Tools::str2url($product['category']),
|
||||
'brand' => isset($product['manufacturer_name']) ? Tools::str2url($product['manufacturer_name']) : '',
|
||||
'variant' => Tools::str2url($variant),
|
||||
'type' => $product_type,
|
||||
'position' => $index ? $index : '0',
|
||||
'quantity' => $product_qty,
|
||||
'list' => Tools::getValue('controller'),
|
||||
'url' => isset($product['link']) ? urlencode($product['link']) : '',
|
||||
'price' => $product['price'],
|
||||
];
|
||||
} else {
|
||||
$ga_product = [
|
||||
'id' => $product_id,
|
||||
'name' => Tools::str2url($product['name']),
|
||||
];
|
||||
}
|
||||
|
||||
return $ga_product;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
|
||||
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
|
||||
|
||||
use Context;
|
||||
|
||||
interface WrapperInterface
|
||||
{
|
||||
/**
|
||||
* @param Context $context
|
||||
*/
|
||||
public function __construct(Context $context);
|
||||
}
|
||||
28
modules/ps_googleanalytics/classes/Wrapper/index.php
Normal file
28
modules/ps_googleanalytics/classes/Wrapper/index.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
header('Expires: Mon, 26 Jul 1998 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
28
modules/ps_googleanalytics/classes/index.php
Normal file
28
modules/ps_googleanalytics/classes/index.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* 2007-2020 PrestaShop and Contributors
|
||||
*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
|
||||
* that is bundled with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/AFL-3.0
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@prestashop.com so we can send you a copy immediately.
|
||||
*
|
||||
* @author PrestaShop SA <contact@prestashop.com>
|
||||
* @copyright 2007-2020 PrestaShop SA and Contributors
|
||||
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
|
||||
* International Registered Trademark & Property of PrestaShop SA
|
||||
*/
|
||||
header('Expires: Mon, 26 Jul 1998 05:00:00 GMT');
|
||||
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
|
||||
|
||||
header('Cache-Control: no-store, no-cache, must-revalidate');
|
||||
header('Cache-Control: post-check=0, pre-check=0', false);
|
||||
header('Pragma: no-cache');
|
||||
|
||||
header('Location: ../');
|
||||
exit;
|
||||
Reference in New Issue
Block a user