Skip to content

Commit fd5f4f3

Browse files
committed
First batch
1 parent c80b709 commit fd5f4f3

6 files changed

Lines changed: 251 additions & 158 deletions

classes/GoogleAnalyticsTools.php

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -139,45 +139,6 @@ function() {
139139
return $js;
140140
}
141141

142-
/**
143-
* addProductClickByHttpReferal
144-
*
145-
* @param array $products
146-
*
147-
* @return string|void
148-
*/
149-
public function addProductClickByHttpReferal($products, $currencyIsoCode)
150-
{
151-
if (!is_array($products)) {
152-
return;
153-
}
154-
155-
$js = '';
156-
foreach ($products as $key => $product) {
157-
$eventData = [
158-
'items' => [
159-
'item_id' => (int) $product['id'],
160-
'item_name' => $product['name'],
161-
'quantity' => (int) $product['quantity'],
162-
'price' => (float) $product['price'],
163-
'currency' => $currencyIsoCode,
164-
'index' => (int) $product['position'],
165-
'item_brand' => $product['brand'],
166-
'item_category' => $product['category'],
167-
'item_list_id' => $product['list'],
168-
'item_variant' => $product['variant'],
169-
],
170-
];
171-
172-
$js .= $this->renderEvent(
173-
'select_item',
174-
$eventData
175-
);
176-
}
177-
178-
return $js;
179-
}
180-
181142
/**
182143
* Encodes array of data into JSON, optionally ignoring some of the values
183144
*

classes/Hook/HookDisplayBeforeBodyClosingTag.php

Lines changed: 173 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class HookDisplayBeforeBodyClosingTag implements HookInterface
3535
{
3636
private $module;
3737
private $context;
38+
private $gaScripts = '';
3839

3940
public function __construct(Ps_Googleanalytics $module, Context $context)
4041
{
@@ -49,13 +50,35 @@ public function __construct(Ps_Googleanalytics $module, Context $context)
4950
*/
5051
public function run()
5152
{
53+
// Prepare our tag handler
5254
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
55+
56+
// Add events for item listing
57+
$this->renderProductListing();
58+
59+
// Add events for search
60+
$this->renderSearch();
61+
62+
// Add events for search
63+
$this->renderCartPage();
64+
65+
// Render begin checkout
66+
$this->renderBeginCheckout();
67+
68+
// TODO
69+
// Sign_up event after registration, we need to check if the register was submitted
70+
// Login event, we need to check if the login was done in this request
71+
// Cart actions adding/removing
72+
73+
return $gaTagHandler->generate($this->gaScripts);
74+
die;
75+
76+
5377
$ganalyticsDataHandler = new GanalyticsDataHandler(
5478
$this->context->cart->id,
5579
$this->context->shop->id
5680
);
5781

58-
$gaScripts = '';
5982
$gacarts = $ganalyticsDataHandler->manageData('', 'R');
6083
$controller_name = Tools::getValue('controller');
6184

@@ -125,24 +148,160 @@ public function run()
125148
$ganalyticsDataHandler->manageData('', 'D');
126149
}
127150

151+
return $gaTagHandler->generate($gaScripts);
152+
}
153+
154+
/**
155+
* This method renders tracking code for product listings, like category pages.
156+
*
157+
* @return string
158+
*/
159+
private function renderProductListing()
160+
{
161+
// Try to get product list variable
128162
$listing = $this->context->smarty->getTemplateVars('listing');
163+
if (empty($listing['products'])) {
164+
return;
165+
}
166+
167+
// Prepare items to our format
129168
$productWrapper = new ProductWrapper($this->context);
130-
$products = $productWrapper->wrapProductList(isset($listing['products']) ? $listing['products'] : []);
131-
132-
if ($controller_name == 'order' || $controller_name == 'orderopc') {
133-
$eventData = [
134-
'currency' => $this->context->currency->iso_code,
135-
];
136-
$gaScripts = $this->module->getTools()->renderEvent(
137-
'begin_checkout',
138-
$eventData
139-
);
169+
$items = [];
170+
$counter = 0;
171+
foreach ($listing['products'] as $product) {
172+
$product = $productWrapper->prepareItemFromProductLazyArray($product);
173+
$product['index'] = $counter;
174+
$items[] = $product;
175+
$counter++;
140176
}
141177

142-
if (isset($products) && count($products) && $controller_name != 'index') {
143-
$gaScripts .= $this->module->getTools()->addProductClick($products, $this->context->currency->iso_code);
178+
// Prepare info about the list
179+
$item_list_id = $this->context->controller->php_self;
180+
$item_list_name = $listing['label'];
181+
182+
// Render the listing event
183+
$eventData = [
184+
'item_list_id' => $item_list_id,
185+
'item_list_name' => $item_list_name,
186+
'items' => $items,
187+
];
188+
$this->gaScripts .= $this->module->getTools()->renderEvent(
189+
'view_item_list',
190+
$eventData
191+
);
192+
}
193+
194+
/**
195+
* This method renders tracking code when user searches on the shop.
196+
*/
197+
private function renderSearch()
198+
{
199+
// Check if we are on search page and we have a search string
200+
if ($this->context->controller->php_self != 'search' || empty($_GET['s'])) {
201+
return;
144202
}
145203

146-
return $gaTagHandler->generate($gaScripts);
204+
// Render the listing event
205+
$eventData = [
206+
'search_term' => (string) $_GET['s'],
207+
];
208+
$this->gaScripts .= $this->module->getTools()->renderEvent(
209+
'search',
210+
$eventData
211+
);
212+
}
213+
214+
/**
215+
* This method renders tracking code for product listings, like category pages.
216+
*
217+
* @return string
218+
*/
219+
private function renderCartpage()
220+
{
221+
// Check if we are on cart page
222+
if ($this->context->controller->php_self != 'cart') {
223+
return;
224+
}
225+
226+
// Try to get product list variable and check if it's not empty
227+
$cart = $this->context->smarty->getTemplateVars('cart');
228+
if (empty($cart['products'])) {
229+
return;
230+
}
231+
232+
// Prepare items to our format
233+
$productWrapper = new ProductWrapper($this->context);
234+
$items = [];
235+
$counter = 0;
236+
foreach ($cart['products'] as $product) {
237+
$product = $productWrapper->prepareItemFromProductLazyArray($product);
238+
$product['index'] = $counter;
239+
$items[] = $product;
240+
$counter++;
241+
}
242+
243+
// Render the listing event
244+
$eventData = [
245+
'currency' => $this->context->currency->iso_code,
246+
'value' => $cart['totals']['total']['amount'],
247+
'items' => $items,
248+
];
249+
$this->gaScripts .= $this->module->getTools()->renderEvent(
250+
'view_cart',
251+
$eventData
252+
);
253+
}
254+
255+
/**
256+
* This method renders tracking code for product listings, like category pages.
257+
*
258+
* @return string
259+
*/
260+
private function renderBeginCheckout()
261+
{
262+
// Check if we are on some supported order controller
263+
$allowed_controllers = ['order', 'orderopc', 'checkout'];
264+
if (!in_array($this->context->controller->php_self, $allowed_controllers)) {
265+
return;
266+
}
267+
268+
// If using default OrderController that comes with prestashop, we will check if we are
269+
// on step 1 of the checkout. Otherwise, we will flush the output anyway. It's probably OPC
270+
// handling everything with javascript, so our code will load only once.
271+
if (get_class($this->context->controller) == "OrderController") {
272+
// If we are not in the first step of checkout, we don't do anything
273+
// TODO test how it behaves with logged in customer
274+
if (!$this->context->controller->getCheckoutProcess()->getSteps()[0]->isCurrent()) {
275+
return;
276+
}
277+
}
278+
279+
// Try to get product list variable and check if it's not empty
280+
$cart = $this->context->smarty->getTemplateVars('cart');
281+
if (empty($cart['products'])) {
282+
return;
283+
}
284+
285+
// Prepare items to our format
286+
$productWrapper = new ProductWrapper($this->context);
287+
$items = [];
288+
$counter = 0;
289+
foreach ($cart['products'] as $product) {
290+
$product = $productWrapper->prepareItemFromProductLazyArray($product);
291+
$product['index'] = $counter;
292+
$items[] = $product;
293+
$counter++;
294+
}
295+
296+
// Render the listing event
297+
$eventData = [
298+
'currency' => $this->context->currency->iso_code,
299+
'value' => $cart['totals']['total']['amount'],
300+
'items' => $items,
301+
];
302+
$this->gaScripts .= $this->module->getTools()->renderEvent(
303+
'begin_checkout',
304+
$eventData
305+
);
147306
}
148307
}

classes/Hook/HookDisplayFooterProduct.php

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,12 @@
2323
use Context;
2424
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsJsHandler;
2525
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
26-
use Product;
2726
use Ps_Googleanalytics;
28-
use Tools;
2927

3028
class HookDisplayFooterProduct implements HookInterface
3129
{
3230
private $module;
3331
private $context;
34-
private $params;
3532

3633
public function __construct(Ps_Googleanalytics $module, Context $context)
3734
{
@@ -46,69 +43,59 @@ public function __construct(Ps_Googleanalytics $module, Context $context)
4643
*/
4744
public function run()
4845
{
49-
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
50-
$controllerName = Tools::getValue('controller');
51-
52-
if ('product' !== $controllerName) {
53-
return '';
46+
// Check we are really on product page
47+
if ($this->context->controller->php_self !== 'product') {
48+
return;
5449
}
5550

56-
if ($this->params['product'] instanceof Product) {
57-
$this->params['product'] = (array) $this->params['product'];
51+
// Get lazy array from context
52+
$product = $this->context->smarty->getTemplateVars('product');
53+
if (empty($product)) {
54+
return;
5855
}
59-
// Add product view
60-
$js = $this->getGoogleAnalytics4();
6156

62-
return $gaTagHandler->generate($js);
63-
}
57+
// Initialize tag handler
58+
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
6459

65-
/**
66-
* setParams
67-
*
68-
* @param array $params
69-
*/
70-
public function setParams($params)
71-
{
72-
$this->params = $params;
73-
}
60+
// Prepare it and format it for our purpose
61+
$productWrapper = new ProductWrapper($this->context);
62+
$item = $productWrapper->prepareItemFromProductLazyArray($product);
7463

75-
protected function getGoogleAnalytics4()
76-
{
77-
$gaProduct = $this->getProduct();
64+
$js = '';
65+
66+
// Prepare and render view_item event
7867
$eventData = [
7968
'currency' => $this->context->currency->iso_code,
80-
'value' => $this->params['product']['price_amount'],
81-
'items' => [
82-
[
83-
'item_id' => (int) $gaProduct['id'],
84-
'item_name' => $this->params['product']['name'],
85-
'currency' => $this->context->currency->iso_code,
86-
'item_brand' => $this->params['product']['manufacturer_name'],
87-
'item_category' => $this->params['product']['category_name'],
88-
'price' => (float) $this->params['product']['price_amount'],
89-
'quantity' => (int) $gaProduct['quantity'],
90-
],
91-
],
69+
'value' => $product['price_amount'],
70+
'items' => [$item],
9271
];
93-
$js = $this->module->getTools()->renderEvent(
72+
$js .= $this->module->getTools()->renderEvent(
9473
'view_item',
9574
$eventData
9675
);
9776

98-
if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) > 0) {
99-
$js .= $this->module->getTools()->addProductClickByHttpReferal([$gaProduct], $this->context->currency->iso_code);
77+
// If the user got to the product page from previous page on our shop,
78+
// we will also send select_item event
79+
if ($this->wasPreviousPageOurShop()) {
80+
$eventData = [
81+
'currency' => $this->context->currency->iso_code,
82+
'value' => $product['price_amount'],
83+
'items' => [$item],
84+
];
85+
$js .= $this->module->getTools()->renderEvent(
86+
'select_item',
87+
$eventData
88+
);
10089
}
10190

102-
return $js;
91+
return $gaTagHandler->generate($js);
10392
}
10493

105-
/**
106-
* @return array
107-
*/
108-
protected function getProduct()
109-
{
110-
$productWrapper = new ProductWrapper($this->context);
94+
private function wasPreviousPageOurShop() {
95+
if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) > 0) {
96+
return true;
97+
}
11198

112-
return $productWrapper->wrapProduct($this->params['product']);
99+
return false;
113100
}
114101
}

0 commit comments

Comments
 (0)