Skip to content

Commit 3ec09fe

Browse files
committed
GA Script generated with smarty
1 parent 5cdeace commit 3ec09fe

13 files changed

Lines changed: 142 additions & 78 deletions

classes/Form/ConfigurationForm.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function generate()
5353
$helper->module = $this->module;
5454
$helper->name_controller = $this->module->name;
5555
$helper->token = \Tools::getAdminTokenLite('AdminModules');
56-
$helper->currentIndex = \AdminController::$currentIndex.'&configure='.$this->module->name;
56+
$helper->currentIndex = \AdminController::$currentIndex.'&configure=' . $this->module->name;
5757

5858
// Language
5959
$helper->default_form_language = $default_lang;
@@ -63,12 +63,12 @@ public function generate()
6363
$helper->title = $this->module->displayName;
6464
$helper->show_toolbar = true; // false -> remove toolbar
6565
$helper->toolbar_scroll = true; // yes - > Toolbar is always visible on the top of the screen.
66-
$helper->submit_action = 'submit'.$this->module->name;
66+
$helper->submit_action = 'submit' . $this->module->name;
6767
$helper->toolbar_btn = array(
6868
'save' =>
6969
array(
7070
'desc' => $this->module->l('Save'),
71-
'href' => \AdminController::$currentIndex.'&configure='.$this->module->name.'&save'.$this->module->name.
71+
'href' => \AdminController::$currentIndex.'&configure=' . $this->module->name.'&save' . $this->module->name.
7272
'&token='.\Tools::getAdminTokenLite('AdminModules'),
7373
),
7474
'back' => array(

classes/GoogleAnalyticsTools.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -150,38 +150,4 @@ public function addProductFromCheckout($products)
150150

151151
return $js;
152152
}
153-
154-
/**
155-
* Generate Google Analytics js
156-
*
157-
* @param string $jsCode
158-
* @param int $backoffice
159-
*
160-
* @return string
161-
*/
162-
public function generateJs($jsState, $isoCode, $jsCode, $backoffice = 0)
163-
{
164-
if (\Configuration::get('GA_ACCOUNT_ID')) {
165-
$generateJsCode = '';
166-
if (!empty($jsCode)) {
167-
$generateJsCode .= '
168-
<script type="text/javascript">
169-
document.addEventListener(\'DOMContentLoaded\', function() {
170-
var MBG = GoogleAnalyticEnhancedECommerce;
171-
MBG.setCurrency(\''.\Tools::safeOutput($isoCode).'\');
172-
'.$jsCode.'
173-
});
174-
</script>';
175-
}
176-
177-
if (($jsState) != 1 && ($backoffice == 0)) {
178-
$generateJsCode .= '
179-
<script type="text/javascript">
180-
ga(\'send\', \'pageview\');
181-
</script>';
182-
}
183-
184-
return $generateJsCode;
185-
}
186-
}
187153
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* 2007-2020 PrestaShop.
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Academic Free License (AFL 3.0)
8+
* that is bundled with this package in the file LICENSE.txt.
9+
* It is also available through the world-wide-web at this URL:
10+
* http://opensource.org/licenses/afl-3.0.php
11+
* If you did not receive a copy of the license and are unable to
12+
* obtain it through the world-wide-web, please send an email
13+
* to license@prestashop.com so we can send you a copy immediately.
14+
*
15+
* DISCLAIMER
16+
*
17+
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
18+
* versions in the future. If you wish to customize PrestaShop for your
19+
* needs please refer to http://www.prestashop.com for more information.
20+
*
21+
* @author PrestaShop SA <contact@prestashop.com>
22+
* @copyright 2007-2020 PrestaShop SA
23+
* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
24+
* International Registered Trademark & Property of PrestaShop SA
25+
*/
26+
27+
namespace PrestaShop\Module\Ps_Googleanalytics\Handler;
28+
29+
class GanalyticsJsHandler
30+
{
31+
private $module;
32+
private $context;
33+
34+
public function __construct(\Ps_googleanalytics $module, \Context $context) {
35+
$this->module = $module;
36+
$this->context = $context;
37+
}
38+
39+
/**
40+
* Generate Google Analytics js
41+
*
42+
* @param string $jsCode
43+
* @param int $isBackoffice
44+
*
45+
* @return string
46+
*/
47+
public function generate($jsCode, $isBackoffice = 0)
48+
{
49+
if (\Configuration::get('GA_ACCOUNT_ID')) {
50+
$this->context->smarty->assign(
51+
array(
52+
'jsCode' => $jsCode,
53+
'isoCode' => \Tools::safeOutput($this->context->currency->iso_code),
54+
'jsState' => $this->module->js_state,
55+
'isBackoffice' => $isBackoffice,
56+
)
57+
);
58+
59+
return $this->module->display(
60+
$this->module->getLocalPath() . $this->module->name,
61+
'ga_tag.tpl'
62+
);
63+
}
64+
}
65+
66+
}

classes/Hook/HookActionCarrierProcess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function run()
5656
);
5757

5858
$carrierName = $carrierRepository->findByCarrierId((int) $this->params['cart']->id_carrier);
59-
$ganalyticsDataHandler->manageData('MBG.addCheckoutOption(2,\''.$carrierName.'\');', 'A');
59+
$ganalyticsDataHandler->manageData('MBG.addCheckoutOption(2,\'' . $carrierName.'\');', 'A');
6060
}
6161
}
6262

classes/Hook/HookActionProductCancel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function run()
5454
$orderDetail = new \OrderDetail($orderDetailId);
5555
$gaScripts .= 'MBG.add('.json_encode(
5656
array(
57-
'id' => empty($orderDetail->product_attribute_id)?$orderDetail->product_id:$orderDetail->product_id.'-'.$orderDetail->product_attribute_id,
57+
'id' => empty($orderDetail->product_attribute_id)?$orderDetail->product_id:$orderDetail->product_id.'-' . $orderDetail->product_attribute_id,
5858
'quantity' => $quantity)
5959
)
6060
.');';

classes/Hook/HookDisplayBackOfficeHeader.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
2828

2929
use PrestaShop\Module\Ps_Googleanalytics\Hooks\HookInterface;
30-
use PrestaShop\Module\Ps_Googleanalytics\GoogleAnalyticsTools;
3130
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\OrderWrapper;
31+
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
3232
use PrestaShop\Module\Ps_Googleanalytics\Repository\GanalyticsRepository;
3333

3434
class HookDisplayBackOfficeHeader implements HookInterface
@@ -56,7 +56,7 @@ public function run()
5656
$ga_account_id = \Configuration::get('GA_ACCOUNT_ID');
5757

5858
if (!empty($ga_account_id) && $this->module->active) {
59-
$gaTools = new GoogleAnalyticsTools();
59+
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
6060
$this->context->controller->addJs($this->module->getPathUri().'views/js/GoogleAnalyticActionLib.js');
6161

6262
$this->context->smarty->assign('GA_ACCOUNT_ID', $ga_account_id);
@@ -96,21 +96,14 @@ public function run()
9696
'id_order = ' . (int) $row['id_order'] . ' AND id_shop = ' . (int) $this->context->shop->id
9797
);
9898
$transaction = json_encode($transaction);
99-
$gaScripts .= 'MBG.addTransaction('.$transaction.');';
99+
$gaScripts .= 'MBG.addTransaction(' . $transaction.');';
100100
}
101101
}
102102
}
103103
}
104104
}
105105

106-
$generatedJs = $gaTools->generateJs(
107-
$this->module->js_state,
108-
$this->context->currency->iso_code,
109-
$gaScripts,
110-
1
111-
);
112-
113-
return $js.$this->module->hookdisplayHeader(null, true).$generatedJs;
106+
return $js . $this->module->hookdisplayHeader(null, true) . $gaTagHandler->generate($gaScripts, 1);
114107
}
115108

116109
return $js;

classes/Hook/HookDisplayFooter.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use PrestaShop\Module\Ps_Googleanalytics\Hooks\HookInterface;
3030
use PrestaShop\Module\Ps_Googleanalytics\GoogleAnalyticsTools;
3131
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
32+
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
3233
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsDataHandler;
3334

3435
class HookDisplayFooter implements HookInterface
@@ -49,6 +50,7 @@ public function __construct($module, $context) {
4950
public function run()
5051
{
5152
$gaTools = new GoogleAnalyticsTools();
53+
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
5254
$ganalyticsDataHandler = new GanalyticsDataHandler(
5355
$this->context->cart->id,
5456
$this->context->shop->id
@@ -106,10 +108,6 @@ public function run()
106108
$gaScripts .= $gaTools->addProductClick($products);
107109
}
108110

109-
return $gaTools->generateJs(
110-
$this->module->js_state,
111-
$this->context->currency->iso_code,
112-
$gaScripts
113-
);
111+
return $gaTagHandler->generate($gaScripts);
114112
}
115113
}

classes/Hook/HookDisplayFooterProduct.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use PrestaShop\Module\Ps_Googleanalytics\Hooks\HookInterface;
3030
use PrestaShop\Module\Ps_Googleanalytics\GoogleAnalyticsTools;
3131
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
32+
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
3233

3334
class HookDisplayFooterProduct implements HookInterface
3435
{
@@ -49,6 +50,7 @@ public function __construct($module, $context) {
4950
public function run()
5051
{
5152
$gaTools = new GoogleAnalyticsTools();
53+
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
5254
$controllerName = \Tools::getValue('controller');
5355

5456
if ('product' !== $controllerName) {
@@ -69,11 +71,7 @@ public function run()
6971

7072
$this->module->js_state = 1;
7173

72-
return $gaTools->generateJs(
73-
$this->module->js_state,
74-
$this->context->currency->iso_code,
75-
$js
76-
);
74+
return $gaTagHandler->generate($js);
7775
}
7876

7977
/**

classes/Hook/HookDisplayHeader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public function run()
8282
);
8383

8484
return $this->module->display(
85-
$this->module->getLocalPath().$this->module->name,
85+
$this->module->getLocalPath() . $this->module->name,
8686
'ps_googleanalytics.tpl'
8787
);
8888
}

classes/Hook/HookDisplayHome.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
use PrestaShop\Module\Ps_Googleanalytics\Hooks\HookInterface;
3030
use PrestaShop\Module\Ps_Googleanalytics\GoogleAnalyticsTools;
31+
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
3132
use PrestaShop\Module\Ps_Googleanalytics\Handler\ModuleHandler;
3233
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
3334

@@ -50,6 +51,7 @@ public function run()
5051
{
5152
$moduleHandler = new ModuleHandler();
5253
$gaTools = new GoogleAnalyticsTools();
54+
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
5355
$gaScripts = '';
5456

5557
// Home featured products
@@ -66,14 +68,12 @@ public function run()
6668
array(),
6769
true
6870
);
69-
$gaScripts .= $gaTools->addProductImpression($homeFeaturedProducts).$gaTools->addProductClick($homeFeaturedProducts);
71+
$gaScripts .= $gaTools->addProductImpression($homeFeaturedProducts) . $gaTools->addProductClick($homeFeaturedProducts);
7072
}
7173

7274
$this->js_state = 1;
7375

74-
return $gaTools->generateJs(
75-
$this->module->js_state,
76-
$this->context->currency->iso_code,
76+
return $gaTagHandler->generate(
7777
$gaTools->filter($gaScripts, $this->module->filterable)
7878
);
7979
}

0 commit comments

Comments
 (0)