Skip to content

Commit 20cdcca

Browse files
authored
Merge pull request #153 from Hlavtox/workload-1
Implement remaining tracking events for GA4
2 parents c80b709 + 9af1d3f commit 20cdcca

20 files changed

Lines changed: 805 additions & 612 deletions

classes/Database/Install.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ public function registerHooks()
110110
$this->module->registerHook('actionProductCancel') &&
111111
$this->module->registerHook('actionValidateOrder') &&
112112
$this->module->registerHook('actionOrderStatusPostUpdate') &&
113-
$this->module->registerHook('actionCartSave') &&
113+
$this->module->registerHook('actionCartUpdateQuantityBefore') &&
114+
$this->module->registerHook('actionObjectProductInCartDeleteBefore') &&
114115
$this->module->registerHook('displayBackOfficeHeader') &&
115116
$this->module->registerHook('actionCarrierProcess');
116117
}

classes/GoogleAnalyticsTools.php

Lines changed: 4 additions & 118 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
*
@@ -57,127 +40,30 @@ public function renderPurchaseEvent($orderProducts, $orderData, $callbackUrl)
5740
}
5841

5942
$callbackData = [
60-
'orderid' => $orderData['id'],
43+
'orderid' => $orderData['transaction_id'],
6144
'customer' => $orderData['customer'],
6245
];
6346

6447
$eventData = [
65-
'transaction_id' => (int) $orderData['id'],
48+
'transaction_id' => (int) $orderData['transaction_id'],
6649
'affiliation' => $orderData['affiliation'],
67-
'value' => (float) $orderData['revenue'],
50+
'value' => (float) $orderData['value'],
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,
8963
['event_callback']
9064
);
9165
}
9266

93-
/**
94-
* addProductClick
95-
*
96-
* @param array $products
97-
* @param string $currencyIsoCode
98-
*
99-
* @return string|void
100-
*/
101-
public function addProductClick($products, $currencyIsoCode)
102-
{
103-
if (!is_array($products)) {
104-
return;
105-
}
106-
107-
$js = '';
108-
foreach ($products as $key => $product) {
109-
$eventData = [
110-
'items' => [
111-
'item_id' => (int) $product['id'],
112-
'item_name' => $product['name'],
113-
'quantity' => (int) $product['quantity'],
114-
'price' => (float) $product['price'],
115-
'currency' => $currencyIsoCode,
116-
'index' => (int) $product['position'],
117-
'item_brand' => $product['brand'],
118-
'item_category' => $product['category'],
119-
'item_list_id' => $product['list'],
120-
'item_variant' => $product['variant'],
121-
],
122-
];
123-
124-
// Add send_to parameter to avoid sending extra events
125-
// to other gtag configs (Ads for example).
126-
$eventData = array_merge(
127-
['send_to' => Configuration::get('GA_ACCOUNT_ID')],
128-
$eventData
129-
);
130-
131-
$productId = explode('-', $product['id']);
132-
$js .= '$(\'article[data-id-product="' . $productId[0] . '"] a.quick-view\').on(
133-
"click",
134-
function() {
135-
gtag("event", "select_item", ' . json_encode($eventData, JSON_UNESCAPED_UNICODE) . ')
136-
});';
137-
}
138-
139-
return $js;
140-
}
141-
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-
18167
/**
18268
* Encodes array of data into JSON, optionally ignoring some of the values
18369
*

classes/Handler/GanalyticsDataHandler.php

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -41,48 +41,12 @@ public function __construct($cartId, $shopId)
4141
$this->shopId = (int) $shopId;
4242
}
4343

44-
/**
45-
* manageData
46-
*
47-
* @param string|array $data
48-
* @param string $action
49-
*
50-
* @return mixed
51-
*/
52-
public function manageData($data, $action)
53-
{
54-
if ('R' === $action) {
55-
return $this->readData();
56-
}
57-
58-
if ('W' === $action) {
59-
return $this->ganalyticsDataRepository->addNewRow(
60-
(int) $this->cartId,
61-
(int) $this->shopId,
62-
json_encode($data)
63-
);
64-
}
65-
66-
if ('A' === $action) {
67-
return $this->appendData($data);
68-
}
69-
70-
if ('D' === $action) {
71-
return $this->ganalyticsDataRepository->deleteRow(
72-
$this->cartId,
73-
$this->shopId
74-
);
75-
}
76-
77-
return false;
78-
}
79-
8044
/**
8145
* readData
8246
*
8347
* @return array
8448
*/
85-
private function readData()
49+
public function readData()
8650
{
8751
$dataReturned = $this->ganalyticsDataRepository->findDataByCartIdAndShopId(
8852
$this->cartId,
@@ -97,23 +61,38 @@ private function readData()
9761
}
9862

9963
/**
100-
* appendData
101-
*
102-
* @param string $data
64+
* Deletes all persisted data, probably because it was flushed.
10365
*
10466
* @return bool
10567
*/
106-
private function appendData($data)
68+
public function deleteData()
10769
{
108-
$dataReturned = $this->ganalyticsDataRepository->findDataByCartIdAndShopId(
70+
return $this->ganalyticsDataRepository->deleteRow(
10971
$this->cartId,
11072
$this->shopId
11173
);
74+
}
11275

113-
if (false === $dataReturned) {
114-
$newData = [$data];
76+
/**
77+
* Stores event into data repository so we can output it
78+
* on first available chance.
79+
*
80+
* @param string $dataToPersist
81+
*
82+
* @return bool
83+
*/
84+
public function persistData($dataToPersist)
85+
{
86+
// Try to get current data
87+
$currentData = $this->readData();
88+
89+
// If no data has been persisted yet, we create a new array, otherwise
90+
// we add it to the previous events stored.
91+
if (!empty($currentData)) {
92+
$newData = $currentData;
93+
$newData[] = $dataToPersist;
11594
} else {
116-
$newData[] = $this->jsonDecodeValidJson($dataReturned);
95+
$newData = [$dataToPersist];
11796
}
11897

11998
return $this->ganalyticsDataRepository->addNewRow(

classes/Hook/HookActionCarrierProcess.php

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
namespace PrestaShop\Module\Ps_Googleanalytics\Hooks;
2222

2323
use Context;
24-
use PrestaShop\Module\Ps_Googleanalytics\Handler\GanalyticsDataHandler;
2524
use PrestaShop\Module\Ps_Googleanalytics\Repository\CarrierRepository;
2625
use Ps_Googleanalytics;
2726

@@ -46,15 +45,28 @@ public function run()
4645
{
4746
if (isset($this->params['cart']->id_carrier)) {
4847
$carrierRepository = new CarrierRepository();
49-
$ganalyticsDataHandler = new GanalyticsDataHandler(
50-
$this->context->cart->id,
51-
$this->context->shop->id
52-
);
5348

54-
$carrierName = $carrierRepository->findByCarrierId((int) $this->params['cart']->id_carrier);
55-
$js = $this->getGoogleAnalytics4($carrierName);
49+
// Load carrier name
50+
$carrierName = (string) $carrierRepository->findByCarrierId((int) $this->params['cart']->id_carrier);
51+
52+
// Check if we actually have some name
53+
if (empty($carrierName)) {
54+
return;
55+
}
5656

57-
$ganalyticsDataHandler->manageData($js, 'A');
57+
// Prepare and render the event
58+
$eventData = [
59+
'currency' => $this->context->currency->iso_code,
60+
'value' => (float) $this->context->cart->getSummaryDetails()['total_price'],
61+
'shipping_tier' => $carrierName,
62+
];
63+
$jsCode = $this->module->getTools()->renderEvent(
64+
'add_shipping_info',
65+
$eventData
66+
);
67+
68+
// Store it into our repository so we can flush it on next page load
69+
$this->module->getDataHandler()->persistData($jsCode);
5870
}
5971
}
6072

@@ -65,21 +77,4 @@ public function setParams($params)
6577
{
6678
$this->params = $params;
6779
}
68-
69-
/**
70-
* @param string $carrierName
71-
*/
72-
protected function getGoogleAnalytics4($carrierName)
73-
{
74-
$eventData = [
75-
'currency' => $this->context->currency->iso_code,
76-
'value' => (float) $this->context->cart->getSummaryDetails()['total_price'],
77-
'shipping_tier' => $carrierName,
78-
];
79-
80-
return $this->module->getTools()->renderEvent(
81-
'add_shipping_info',
82-
$eventData
83-
);
84-
}
8580
}

0 commit comments

Comments
 (0)