-
Notifications
You must be signed in to change notification settings - Fork 1
/
SessionDataLayer.php
83 lines (73 loc) · 1.79 KB
/
SessionDataLayer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace Taggrs\DataLayer\Block;
use Magento\Customer\Model\Session;
use Taggrs\DataLayer\Helper\UserDataHelper;
use Magento\Framework\View\Element\Template;
/**
* Renders Data Layer from session
*/
class SessionDataLayer extends DataLayer
{
/**
* @var Session customer session
*/
private Session $session;
/**
* Class constructor
*
* @param Session $session
* @param UserDataHelper $userDataHelper
* @param Template\Context $context
* @param array $data
*/
public function __construct(
Session $session,
UserDataHelper $userDataHelper,
Template\Context $context,
array $data = []
) {
parent::__construct($userDataHelper, $context, $data);
$this->session = $session;
}
/**
* Get the Data Layer as an associative array
*
* @return array the Data Layer
*/
public function getDataLayer(): array
{
if ($this->session->getDataLayer()) {
$dataLayer = $this->session->getDataLayer();
$this->session->unsDataLayer();
return $dataLayer;
}
return [];
}
/**
* Get Data Layer as JSON-encoded object
*
* @return string JSON-encoded Data Layer
*/
public function getDataLayerJson(): string
{
return json_encode($this->getDataLayer());
}
/**
* Get the event name. Override this method in child classes.
*
* @return string event name
*/
public function getEvent(): string
{
return '';
}
/**
* Get the e-commerce Data Layer. Override this method in child classes.
*
* @return array e-commerce Data Layer
*/
public function getEcommerce(): array
{
return [];
}
}