Skip to content

Commit 2fc509f

Browse files
committed
Implement more events
1 parent fd5f4f3 commit 2fc509f

9 files changed

Lines changed: 193 additions & 134 deletions

classes/GoogleAnalyticsTools.php

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,6 @@
2424

2525
class GoogleAnalyticsTools
2626
{
27-
/**
28-
* filter
29-
*
30-
* @param string $gaScripts
31-
* @param int $filterable
32-
*
33-
* @return string
34-
*/
35-
public function filter($gaScripts, $filterable)
36-
{
37-
if (1 == $filterable) {
38-
return implode(';', array_unique(explode(';', $gaScripts)));
39-
}
40-
41-
return $gaScripts;
42-
}
43-
4427
/**
4528
* Renders purchase event for order
4629
*
@@ -68,21 +51,12 @@ public function renderPurchaseEvent($orderProducts, $orderData, $callbackUrl)
6851
'tax' => (float) $orderData['tax'],
6952
'shipping' => (float) $orderData['shipping'],
7053
'currency' => $orderData['currency'],
71-
'items' => [],
54+
'items' => $orderProducts,
7255
'event_callback' => "function() {
7356
$.get('" . $callbackUrl . "', " . json_encode($callbackData, JSON_UNESCAPED_UNICODE) . ');
7457
}',
7558
];
7659

77-
foreach ($orderProducts as $product) {
78-
$eventData['items'][] = [
79-
'item_id' => (int) $product['id'],
80-
'item_name' => $product['name'],
81-
'quantity' => (int) $product['quantity'],
82-
'price' => (float) $product['price'],
83-
];
84-
}
85-
8660
return $this->renderEvent(
8761
'purchase',
8862
$eventData,

classes/Hook/HookDisplayBackOfficeHeader.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,7 @@ public function processOrder($idOrder)
167167
$orderProducts = [];
168168
$cart = new Cart($order->id_cart);
169169
if (Validate::isLoadedObject($cart)) {
170-
foreach ($cart->getProducts() as $order_product) {
171-
$orderProducts[] = $productWrapper->wrapProduct($order_product);
172-
}
170+
$orderProducts = $productWrapper->prepareItemListFromProductList($cart->getProducts(), true);
173171
}
174172

175173
// Render transaction code

classes/Hook/HookDisplayBeforeBodyClosingTag.php

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,21 @@ public function run()
5353
// Prepare our tag handler
5454
$gaTagHandler = new GanalyticsJsHandler($this->module, $this->context);
5555

56-
// Add events for item listing
57-
$this->renderProductListing();
56+
// Log information about product listing
57+
$this->saveInformationAboutListing();
5858

59-
// Add events for search
59+
// Add events
60+
$this->renderProductListing();
6061
$this->renderSearch();
61-
62-
// Add events for search
6362
$this->renderCartPage();
64-
65-
// Render begin checkout
6663
$this->renderBeginCheckout();
64+
$this->renderLogin();
65+
$this->renderRegistration();
6766

6867
// 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
68+
// Unify wrappers - add proper quantities for cart items
7169
// Cart actions adding/removing
70+
// Shipping info
7271

7372
return $gaTagHandler->generate($this->gaScripts);
7473
die;
@@ -83,8 +82,6 @@ public function run()
8382
$controller_name = Tools::getValue('controller');
8483

8584
if (count($gacarts) > 0 && $controller_name != 'product') {
86-
$this->module->filterable = 0;
87-
8885
foreach ($gacarts as $key => $gacart) {
8986
if (isset($gacart['quantity'])) {
9087
if ($gacart['quantity'] > 0) {
@@ -153,8 +150,6 @@ public function run()
153150

154151
/**
155152
* This method renders tracking code for product listings, like category pages.
156-
*
157-
* @return string
158153
*/
159154
private function renderProductListing()
160155
{
@@ -166,20 +161,13 @@ private function renderProductListing()
166161

167162
// Prepare items to our format
168163
$productWrapper = new ProductWrapper($this->context);
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++;
176-
}
177-
164+
$items = $productWrapper->prepareItemListFromProductList($listing['products']);
165+
178166
// Prepare info about the list
179167
$item_list_id = $this->context->controller->php_self;
180168
$item_list_name = $listing['label'];
181169

182-
// Render the listing event
170+
// Render the event
183171
$eventData = [
184172
'item_list_id' => $item_list_id,
185173
'item_list_name' => $item_list_name,
@@ -201,7 +189,7 @@ private function renderSearch()
201189
return;
202190
}
203191

204-
// Render the listing event
192+
// Render the event
205193
$eventData = [
206194
'search_term' => (string) $_GET['s'],
207195
];
@@ -213,8 +201,6 @@ private function renderSearch()
213201

214202
/**
215203
* This method renders tracking code for product listings, like category pages.
216-
*
217-
* @return string
218204
*/
219205
private function renderCartpage()
220206
{
@@ -231,16 +217,9 @@ private function renderCartpage()
231217

232218
// Prepare items to our format
233219
$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
220+
$items = $productWrapper->prepareItemListFromProductList($cart['products'], true);
221+
222+
// Render the event
244223
$eventData = [
245224
'currency' => $this->context->currency->iso_code,
246225
'value' => $cart['totals']['total']['amount'],
@@ -254,8 +233,6 @@ private function renderCartpage()
254233

255234
/**
256235
* This method renders tracking code for product listings, like category pages.
257-
*
258-
* @return string
259236
*/
260237
private function renderBeginCheckout()
261238
{
@@ -284,16 +261,9 @@ private function renderBeginCheckout()
284261

285262
// Prepare items to our format
286263
$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-
}
264+
$items = $productWrapper->prepareItemListFromProductList($cart['products'], true);
295265

296-
// Render the listing event
266+
// Render the event
297267
$eventData = [
298268
'currency' => $this->context->currency->iso_code,
299269
'value' => $cart['totals']['total']['amount'],
@@ -304,4 +274,53 @@ private function renderBeginCheckout()
304274
$eventData
305275
);
306276
}
277+
278+
/**
279+
* This method renders tracking code after user logs in.
280+
*/
281+
private function renderLogin()
282+
{
283+
// Render it only on login page AND if we are not creating a new account in older PS versions
284+
// For newer versions, registrations are handled with standalone registration controller.
285+
if ($this->context->controller->php_self != 'authentication' || isset($_GET['create_account'])) {
286+
return;
287+
}
288+
289+
// Render the event
290+
$this->gaScripts .= $this->module->getTools()->renderEvent('login', []);
291+
}
292+
293+
/**
294+
* This method renders tracking code after user registers.
295+
*/
296+
private function renderRegistration()
297+
{
298+
if ($this->context->controller->php_self != 'registration' &&
299+
($this->context->controller->php_self != 'authentication' || !isset($_GET['create_account']))
300+
) {
301+
return;
302+
}
303+
304+
// Render the event
305+
$this->gaScripts .= $this->module->getTools()->renderEvent('sign_up', []);
306+
}
307+
308+
/**
309+
* Saves information about last visited product listing, so we can later use it for select_item event.
310+
*/
311+
private function saveInformationAboutListing()
312+
{
313+
// Try to get product list variable
314+
$listing = $this->context->smarty->getTemplateVars('listing');
315+
if (empty($listing['products']) || empty($listing['label'])) {
316+
return;
317+
}
318+
319+
// Save this information to a cookie
320+
$this->context->cookie->ga_last_listing = json_encode([
321+
'item_list_url' => $_SERVER['REQUEST_URI'],
322+
'item_list_id' => $this->context->controller->php_self,
323+
'item_list_name' => $listing['label'],
324+
]);
325+
}
307326
}

classes/Hook/HookDisplayFooterProduct.php

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ public function run()
5959

6060
// Prepare it and format it for our purpose
6161
$productWrapper = new ProductWrapper($this->context);
62-
$item = $productWrapper->prepareItemFromProductLazyArray($product);
62+
$item = $productWrapper->prepareItemFromProduct($product);
6363

6464
$js = '';
6565

66-
// Prepare and render view_item event
66+
// Prepare and render event
6767
$eventData = [
6868
'currency' => $this->context->currency->iso_code,
6969
'value' => $product['price_amount'],
@@ -75,13 +75,21 @@ public function run()
7575
);
7676

7777
// If the user got to the product page from previous page on our shop,
78-
// we will also send select_item event
78+
// we will also send select_item event.
7979
if ($this->wasPreviousPageOurShop()) {
8080
$eventData = [
81-
'currency' => $this->context->currency->iso_code,
82-
'value' => $product['price_amount'],
8381
'items' => [$item],
8482
];
83+
84+
// We will also try to get the information about the last visited listing.
85+
// We save this information into a cookie. If it's the page that got the user here,
86+
// we will use it.
87+
$previousListingData = $this->getLastVisitedListing();
88+
if (!empty($previousListingData)) {
89+
$eventData = array_merge($previousListingData, $eventData);
90+
}
91+
92+
// Render the event
8593
$js .= $this->module->getTools()->renderEvent(
8694
'select_item',
8795
$eventData
@@ -91,11 +99,45 @@ public function run()
9199
return $gaTagHandler->generate($js);
92100
}
93101

102+
/**
103+
* Checks HTTP_REFERER to see if the previous page that got user to this product
104+
* was our shop.
105+
*
106+
* @return bool
107+
*/
94108
private function wasPreviousPageOurShop() {
95-
if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) > 0) {
109+
if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) !== false) {
96110
return true;
97111
}
98112

99113
return false;
100114
}
115+
116+
/**
117+
* Tries to get details of previous listing from the cookie.
118+
*
119+
* @return bool|array
120+
*/
121+
private function getLastVisitedListing()
122+
{
123+
// Fetch it from the cookie
124+
$last_listing = $this->context->cookie->ga_last_listing;
125+
if (empty($last_listing)) {
126+
return false;
127+
}
128+
129+
// Decode the data and check if it contains something sensible
130+
$last_listing = json_decode($last_listing, true);
131+
if (empty($last_listing['item_list_id'])) {
132+
return false;
133+
}
134+
135+
// Check if the last listing is the page the user came from
136+
if (isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], $last_listing['item_list_url']) !== false) {
137+
unset($last_listing['item_list_url']);
138+
return $last_listing;
139+
}
140+
141+
return false;
142+
}
101143
}

classes/Hook/HookDisplayOrderConfirmation.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function run()
6969

7070
// If the customer is revisiting confirmation screen and the order was already sent, we don't do anything
7171
if ($ganalyticsRepository->hasOrderBeenAlreadySent((int) $order->id)) {
72-
return $gaScripts;
72+
// return $gaScripts;
7373
}
7474

7575
// Prepare transaction data
@@ -88,9 +88,7 @@ public function run()
8888
$orderProducts = [];
8989
$cart = new Cart($order->id_cart);
9090
if (Validate::isLoadedObject($cart)) {
91-
foreach ($cart->getProducts() as $order_product) {
92-
$orderProducts[] = $productWrapper->wrapProduct($order_product);
93-
}
91+
$orderProducts = $productWrapper->prepareItemListFromProductList($cart->getProducts(), true);
9492
}
9593

9694
// Render transaction code

classes/Wrapper/OrderWrapper.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323
use Configuration;
2424
use Context;
2525
use Currency;
26-
use PrestaShop\Module\Ps_Googleanalytics\Hooks\WrapperInterface;
2726
use Shop;
2827

29-
class OrderWrapper implements WrapperInterface
28+
class OrderWrapper
3029
{
3130
private $context;
3231

0 commit comments

Comments
 (0)