Skip to content

Commit 5e03dea

Browse files
committed
Add cart events
1 parent bc84529 commit 5e03dea

8 files changed

Lines changed: 153 additions & 412 deletions

classes/GoogleAnalyticsTools.php

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -64,55 +64,6 @@ public function renderPurchaseEvent($orderProducts, $orderData, $callbackUrl)
6464
);
6565
}
6666

67-
/**
68-
* addProductClick
69-
*
70-
* @param array $products
71-
* @param string $currencyIsoCode
72-
*
73-
* @return string|void
74-
*/
75-
public function addProductClick($products, $currencyIsoCode)
76-
{
77-
if (!is_array($products)) {
78-
return;
79-
}
80-
81-
$js = '';
82-
foreach ($products as $key => $product) {
83-
$eventData = [
84-
'items' => [
85-
'item_id' => (int) $product['id'],
86-
'item_name' => $product['name'],
87-
'quantity' => (int) $product['quantity'],
88-
'price' => (float) $product['price'],
89-
'currency' => $currencyIsoCode,
90-
'index' => (int) $product['position'],
91-
'item_brand' => $product['brand'],
92-
'item_category' => $product['category'],
93-
'item_list_id' => $product['list'],
94-
'item_variant' => $product['variant'],
95-
],
96-
];
97-
98-
// Add send_to parameter to avoid sending extra events
99-
// to other gtag configs (Ads for example).
100-
$eventData = array_merge(
101-
['send_to' => Configuration::get('GA_ACCOUNT_ID')],
102-
$eventData
103-
);
104-
105-
$productId = explode('-', $product['id']);
106-
$js .= '$(\'article[data-id-product="' . $productId[0] . '"] a.quick-view\').on(
107-
"click",
108-
function() {
109-
gtag("event", "select_item", ' . json_encode($eventData, JSON_UNESCAPED_UNICODE) . ')
110-
});';
111-
}
112-
113-
return $js;
114-
}
115-
11667
/**
11768
* Encodes array of data into JSON, optionally ignoring some of the values
11869
*

classes/Hook/HookActionCarrierProcess.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public function run()
5454
// Load carrier name
5555
$carrierName = (string) $carrierRepository->findByCarrierId((int) $this->params['cart']->id_carrier);
5656

57+
// Check if we actually have some name
58+
if (empty($carrierName)) {
59+
return;
60+
}
61+
5762
// Prepare and render the event
5863
$eventData = [
5964
'currency' => $this->context->currency->iso_code,

classes/Hook/HookActionCartSave.php

Lines changed: 0 additions & 205 deletions
This file was deleted.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Copyright since 2007 PrestaShop SA and Contributors
4+
*
5+
* NOTICE OF LICENSE
6+
*
7+
* This source file is subject to the Academic Free License 3.0 (AFL-3.0)
8+
* that is bundled with this package in the file LICENSE.md.
9+
* It is also available through the world-wide-web at this URL:
10+
* https://opensource.org/licenses/AFL-3.0
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+
* @author PrestaShop SA <contact@prestashop.com>
16+
* @copyright Since 2007 PrestaShop SA and Contributors
17+
* @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
18+
* International Registered Trademark & Property of PrestaShop SA
19+
*/
20+
21+
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
22+
23+
use Context;
24+
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsDataHandler;
25+
use PrestaShop\Module\Ps_Googleanalytics\Wrapper\ProductWrapper;
26+
use Product;
27+
use Ps_Googleanalytics;
28+
29+
class HookActionCartUpdateQuantityBefore implements HookInterface
30+
{
31+
private $module;
32+
33+
/**
34+
* @var Context
35+
*/
36+
private $context;
37+
private $params;
38+
39+
public function __construct(Ps_Googleanalytics $module, Context $context)
40+
{
41+
$this->module = $module;
42+
$this->context = $context;
43+
}
44+
45+
/**
46+
* run
47+
*
48+
* @return void
49+
*/
50+
public function run()
51+
{
52+
// Get our tag handler
53+
$ganalyticsDataHandler = new GanalyticsDataHandler(
54+
$this->context->cart->id,
55+
$this->context->shop->id
56+
);
57+
58+
/*
59+
* The hook passes a legacy Product object to add, but no attribute information.
60+
* But thankfully, we can use id_product_attribute for this.
61+
*
62+
* Other info is fairly standard:
63+
*
64+
* Add to cart from product page + up from cart page
65+
* $this->params['operator'] == up
66+
* $this->params['quantity'] to determine quantity
67+
*
68+
* Down from cart page
69+
* $this->params['operator'] == down
70+
* $this->params['quantity'] to determine quantity
71+
*/
72+
73+
// Format product and standardize ID
74+
$product = (array) $this->params['product'];
75+
$product['id_product'] = $product['id'];
76+
77+
// Get some basic information
78+
$product = Product::getProductProperties($this->context->language->id, $product);
79+
80+
// Add information about attribute
81+
if (!empty($this->params['id_product_attribute'])) {
82+
$product['id_product_attribute'] = (int) $this->params['id_product_attribute'];
83+
}
84+
$product['price_amount'] = $product['price'];
85+
86+
// Add informationa about quantity difference
87+
$product['quantity'] = (int) $this->params['quantity'];
88+
89+
// Prepare it and format it for our purpose
90+
$productWrapper = new ProductWrapper($this->context);
91+
$item = $productWrapper->prepareItemFromProduct($product, true);
92+
93+
// Prepare and render event
94+
$eventData = [
95+
'currency' => $this->context->currency->iso_code,
96+
'value' => $product['price_amount'] * $product['quantity'],
97+
'items' => [$item],
98+
];
99+
$jsCode = $this->module->getTools()->renderEvent(
100+
$this->params['operator'] == 'up' ? 'add_to_cart' : 'remove_from_cart',
101+
$eventData
102+
);
103+
104+
// Store this event
105+
$ganalyticsDataHandler->persistData($jsCode);
106+
}
107+
108+
/**
109+
* @param array $params
110+
*/
111+
public function setParams($params)
112+
{
113+
$this->params = $params;
114+
}
115+
}

0 commit comments

Comments
 (0)