Skip to content

Commit 94eec00

Browse files
committed
Added magefanUniqueEventId paramether for each event
1 parent 770ec17 commit 94eec00

File tree

5 files changed

+56
-16
lines changed

5 files changed

+56
-16
lines changed

Model/AbstractDataLayer.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,48 @@ protected function getCustomerGroupCode(): string
255255

256256
return $this->customerGroupCode;
257257
}
258+
259+
/**
260+
* @param array $data
261+
* @return array
262+
*/
263+
protected function eventWrap(array $data): array
264+
{
265+
if (empty($data)) {
266+
return $data;
267+
}
268+
269+
$data = $this->addCustomerGroup($data);
270+
$data = $this->addMfUniqueEventId($data);
271+
272+
return $data;
273+
}
274+
275+
/**
276+
* @param array $data
277+
* @return array
278+
*/
279+
protected function addCustomerGroup(array $data): array
280+
{
281+
if (!isset($data['customerGroup'])) {
282+
$data['customerGroup'] = $this->getCustomerGroupCode();
283+
}
284+
return $data;
285+
}
286+
287+
/**
288+
* @param array $data
289+
* @return array
290+
*/
291+
protected function addMfUniqueEventId(array $data): array
292+
{
293+
294+
$hash = md5(json_encode($data) . microtime());
295+
$event = isset($data['event']) ? $data['event'] : 'event';
296+
$eventId = $event . '_' . $hash;
297+
298+
$data['magefanUniqueEventId'] = $eventId;
299+
300+
return $data;
301+
}
258302
}

Model/DataLayer/BeginCheckout.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,14 @@ public function get(Quote $quote): array
5252
$items[] = $this->gtmItem->get($item);
5353
}
5454

55-
return [
55+
return $this->eventWrap([
5656
'event' => 'begin_checkout',
5757
'ecommerce' => [
5858
'currency' => $this->getCurrentCurrencyCode(),
5959
'value' => $this->formatPrice((float)$quote->getGrandTotal()),
6060
'coupon' => $quote->getCouponCode() ?: '',
6161
'items' => $items
62-
],
63-
'customerGroup' => $this->getCustomerGroupCode()
64-
];
62+
]
63+
]);
6564
}
6665
}

Model/DataLayer/Purchase.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function get(Order $order): array
5252
$items[] = $this->gtmItem->get($item);
5353
}
5454

55-
return [
55+
return $this->eventWrap([
5656
'event' => 'purchase',
5757
'ecommerce' => [
5858
'transaction_id' => $order->getIncrementId(),
@@ -65,9 +65,8 @@ public function get(Order $order): array
6565
],
6666
'is_virtual' => (bool)$order->getIsVirtual(),
6767
'shipping_description' => $order->getShippingDescription(),
68-
'customer_is_guest' => (bool)$order->getCustomerIsGuest(),
69-
'customerGroup' => $this->getCustomerGroupCode()
70-
];
68+
'customer_is_guest' => (bool)$order->getCustomerIsGuest()
69+
]);
7170
}
7271

7372
return [];

Model/DataLayer/ViewCart.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function get(Quote $quote): array
5454
$itemsQty += $item->getQty() * 1;
5555
}
5656

57-
return [
57+
return $this->eventWrap([
5858
'event' => 'view_cart',
5959
'ecommerce' => [
6060
'currency' => $this->getCurrentCurrencyCode(),
@@ -63,8 +63,7 @@ public function get(Quote $quote): array
6363
],
6464
'items_count' => count($items),
6565
'items_qty' => $itemsQty,
66-
'coupon_code' => $quote->getCouponCode() ?: '',
67-
'customerGroup' => $this->getCustomerGroupCode()
68-
];
66+
'coupon_code' => $quote->getCouponCode() ?: ''
67+
]);
6968
}
7069
}

Model/DataLayer/ViewItem.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,15 @@ public function get(Product $product): array
4848
{
4949
$item = $this->gtmItem->get($product);
5050

51-
return [
51+
return $this->eventWrap([
5252
'event' => 'view_item',
5353
'ecommerce' => [
5454
'currency' => $this->getCurrentCurrencyCode(),
5555
'value' => $this->getPrice($product),
5656
'items' => [
5757
$item
5858
]
59-
],
60-
'customerGroup' => $this->getCustomerGroupCode()
61-
];
59+
]
60+
]);
6261
}
6362
}

0 commit comments

Comments
 (0)