Skip to content

Commit d25406c

Browse files
committed
Added User ID to all events
1 parent 50b66a3 commit d25406c

File tree

9 files changed

+178
-19
lines changed

9 files changed

+178
-19
lines changed

Block/AbstractDataLayer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ protected function _toHtml(): string
5252
if ($this->config->isEnabled()) {
5353
$dataLayer = $this->getDataLayer();
5454
if ($dataLayer) {
55+
$json = json_encode($dataLayer);
56+
$json = str_replace('"getMfGtmCustomerIdentifier()"', 'getMfGtmCustomerIdentifier()', $json);
5557
//style always should be displayed none, since some sliders add a class that makes the script display flex/block
5658
return '<script style="display: none;">
5759
window.dataLayer = window.dataLayer || [];
58-
window.dataLayer.push(' . json_encode($dataLayer) . ');
60+
window.dataLayer.push(' . $json . ');
5961
</script>';
6062
}
6163
}

Model/AbstractDataLayer.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ protected function eventWrap(array $data): array
292292
$data = $this->addCustomerGroup($data);
293293
$data = $this->addMfUniqueEventId($data);
294294
$data = $this->addEcommPageType($data);
295+
$data = $this->addCustomerIdentifier($data);
295296

296297
return $data;
297298
}
@@ -332,4 +333,16 @@ protected function addEcommPageType(array $data): array
332333

333334
return $data;
334335
}
336+
337+
/**
338+
* @param array $data
339+
* @return array
340+
*/
341+
protected function addCustomerIdentifier(array $data): array
342+
{
343+
if (empty($data['customer_identifier'])) {
344+
$data['customer_identifier'] = 'getMfGtmCustomerIdentifier()';
345+
}
346+
return $data;
347+
}
335348
}

Model/DataLayer/BeginCheckout.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public function get(Quote $quote): array
5959
'value' => $this->formatPrice((float)$quote->getGrandTotal()),
6060
'coupon' => $quote->getCouponCode() ?: '',
6161
'items' => $items
62-
]
62+
],
63+
'customer_identifier' => $quote->getCustomerEmail() ? hash('sha256', (string)$quote->getCustomerEmail()) : ''
6364
]);
6465
}
6566
}

Model/DataLayer/ViewCart.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public function get(Quote $quote): array
6363
],
6464
'items_count' => count($items),
6565
'items_qty' => $itemsQty,
66-
'coupon_code' => $quote->getCouponCode() ?: ''
66+
'coupon_code' => $quote->getCouponCode() ?: '',
67+
'customer_identifier' => $quote->getCustomerEmail() ? hash('sha256', (string)$quote->getCustomerEmail()) : ''
6768
]);
6869
}
6970
}

Model/WebContainer.php

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,7 @@ public function generate(string $storeId = null): array
8686
],
8787
'tag' => $isAnalyticsEnabled ? $this->generateTags($accountId, $containerId, $timestamp, $storeId) : [],
8888
'trigger' => $isAnalyticsEnabled ? $this->generateTriggers($accountId, $containerId, $timestamp) : [],
89-
'variable' => [
90-
[
91-
'accountId' => $accountId,
92-
'containerId' => $containerId,
93-
'variableId' => '692',
94-
'name' => 'Magefan GA4 - Measurement ID',
95-
'type' => 'c',
96-
'parameter' => [
97-
[
98-
'type' => 'TEMPLATE',
99-
'key' => 'value',
100-
'value' => $this->config->getMeasurementId($storeId)
101-
],
102-
]
103-
]
104-
],
89+
'variable' => $isAnalyticsEnabled ? $this->generateVariable($accountId, $containerId, $timestamp, $storeId) : [],
10590
'builtInVariable' => $isAnalyticsEnabled ? [
10691
[
10792
'accountId' => $accountId,
@@ -282,6 +267,11 @@ private function generateTags(
282267
'type' => 'TEMPLATE',
283268
'key' => 'measurementIdOverride',
284269
'value' => '{{Magefan GA4 - Measurement ID}}'
270+
],
271+
[
272+
"type" => "TEMPLATE",
273+
"key" => "eventSettingsVariable",
274+
"value" => "{{Magefan GA4 - User ID}}"
285275
]
286276
],
287277
'fingerprint' => $timestamp,
@@ -298,4 +288,66 @@ private function generateTags(
298288
]
299289
];
300290
}
291+
292+
/**
293+
* @param string $accountId
294+
* @param string $containerId
295+
* @param string $timestamp
296+
* @param string|null $storeId
297+
* @return array[]
298+
*/
299+
private function generateVariable(
300+
string $accountId,
301+
string $containerId,
302+
string $timestamp,
303+
string $storeId = null
304+
): array{
305+
return [
306+
[
307+
'accountId' => $accountId,
308+
'containerId' => $containerId,
309+
'variableId' => '692',
310+
'name' => 'Magefan GA4 - Measurement ID',
311+
'type' => 'c',
312+
'parameter' => [
313+
[
314+
'type' => 'TEMPLATE',
315+
'key' => 'value',
316+
'value' => $this->config->getMeasurementId($storeId)
317+
],
318+
]
319+
],
320+
[
321+
'accountId' => $accountId,
322+
'containerId' => $containerId,
323+
'variableId'=>'43',
324+
'name'=>'Magefan GA4 - User ID',
325+
'type'=>'gtes',
326+
'parameter'=>[
327+
[
328+
'type'=>'LIST',
329+
'key'=>'eventSettingsTable',
330+
'list'=>[
331+
[
332+
'type'=>'MAP',
333+
'map'=>[
334+
[
335+
'type'=>'TEMPLATE',
336+
'key'=>'parameter',
337+
'value'=>'user_id'
338+
],
339+
[
340+
'type'=>'TEMPLATE',
341+
'key'=>'parameterValue',
342+
'value'=>'{{Magefan DLV - Customer Email Hash}}'
343+
]
344+
]
345+
]
346+
]
347+
]
348+
],
349+
'fingerprint' => $timestamp
350+
]
351+
];
352+
}
301353
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © Magefan ([email protected]). All rights reserved.
4+
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\GoogleTagManager\Plugin\Magento\Customer\CustomerData;
10+
11+
use Magento\Customer\Model\Session;
12+
use Magefan\GoogleTagManager\Model\Config;
13+
14+
class Customer
15+
{
16+
/**
17+
* @var Session
18+
*/
19+
private $session;
20+
/**
21+
* @var Config
22+
*/
23+
private $config;
24+
25+
/**
26+
* @param Session $session
27+
*/
28+
public function __construct(
29+
Session $session,
30+
Config $config
31+
) {
32+
$this->session = $session;
33+
$this->config = $config;
34+
}
35+
36+
/**
37+
* @param \Magento\Customer\CustomerData\Customer $subject
38+
* @param $result
39+
* @return void
40+
*/
41+
public function afterGetSectionData(
42+
\Magento\Customer\CustomerData\Customer $subject,
43+
$result
44+
)
45+
{
46+
if ($this->config->isEnabled()){
47+
if ($this->session->getCustomerId()) {
48+
$result['mf_gtm_customer_identifier'] = hash('sha256', (string)$this->session->getCustomer()->getEmail());
49+
}
50+
}
51+
52+
return $result;
53+
}
54+
}

etc/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@
2222
<argument name="container" xsi:type="object">Magefan\GoogleTagManager\Model\WebContainer</argument>
2323
</arguments>
2424
</type>
25+
<type name="Magento\Customer\CustomerData\Customer">
26+
<plugin name="Magefan_GoogleTagManager_Plugin_Magento_Customer_CustomerData_Customer" type="Magefan\GoogleTagManager\Plugin\Magento\Customer\CustomerData\Customer" sortOrder="10" disabled="false"/>
27+
</type>
2528
</config>

etc/module.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<module name="Magefan_GoogleTagManager">
1010
<sequence>
1111
<module name="Magefan_Community" />
12+
<module name="Magento_Customer"/>
1213
</sequence>
1314
</module>
1415
</config>

view/frontend/templates/js_code.phtml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,38 @@ if (!isset($escaper)) {
126126
mfLoadGtm();
127127
<?php } ?>
128128

129+
require(['jquery', 'Magento_Customer/js/customer-data'], function ($, customer) {
130+
function updateMfGtmCustomerIdentifier(data) {
131+
if (data.mf_gtm_customer_identifier) {
132+
localStorage.setItem("mf_gtm_customer_identifier", data.mf_gtm_customer_identifier);
133+
}
134+
}
135+
let customerData = customer.get('customer');
136+
137+
customerData.subscribe(function (data) {
138+
updateMfGtmCustomerIdentifier(data);
139+
}.bind(this));
140+
updateMfGtmCustomerIdentifier(customerData());
141+
});
142+
143+
/* Hyva code */
144+
function mfHyvaGtmSetCustomerIdentifier(event) {
145+
let data, j;
146+
let sections = event.detail.data;
147+
for (j in sections) {
148+
data = sections[j];
149+
if (data.mf_gtm_customer_identifier) {
150+
localStorage.setItem("mf_gtm_customer_identifier", data.mf_gtm_customer_identifier);
151+
return;
152+
}
153+
}
154+
}
155+
window.addEventListener("private-content-loaded", mfHyvaGtmSetCustomerIdentifier);
156+
/* End Hyva code */
157+
158+
function getMfGtmCustomerIdentifier() {
159+
return localStorage.getItem("mf_gtm_customer_identifier") ? localStorage.getItem("mf_gtm_customer_identifier") : null;
160+
}
129161

130162
function mfLoadGtm() {
131163
if (!window.mfGtmUserActionDetected) return false;

0 commit comments

Comments
 (0)