-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrackingCodeGenerator.php
More file actions
195 lines (174 loc) · 6.31 KB
/
TrackingCodeGenerator.php
File metadata and controls
195 lines (174 loc) · 6.31 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/
namespace OxidEsales\PersonalizationModule\Component\Tracking;
use \Econda\Tracking;
use Econda\Tracking\OrderProcess;
use Econda\Tracking\Product;
use Econda\Tracking\ProductDetailView;
use Econda\Tracking\Registration;
use Econda\Tracking\Target;
use Econda\Tracking\TransactionProduct;
use OxidEsales\PersonalizationModule\Component\Tracking\SdkExtension\Email;
use OxidEsales\PersonalizationModule\Component\Tracking\SdkExtension\PageView;
use OxidEsales\PersonalizationModule\Component\Tracking\SdkExtension\Promotions;
/**
* Class responsible for generating Econda tracking script.
*/
class TrackingCodeGenerator
{
/**
* @var ActivePageEntityInterface
*/
private $activePageEntity;
/**
* @var PageView
*/
private $pageView;
/**
* @param ActivePageEntityInterface $activePageEntity
*/
public function __construct(ActivePageEntityInterface $activePageEntity)
{
$this->activePageEntity = $activePageEntity;
}
/**
* Generates JS script for Econda tracking.
*
* @return string
*/
public function generateCode()
{
$this->pageView = $this->initializePageView();
$this->addCurrentProductDataToPageView();
$this->addCurrentOrderProcessToPageView();
$this->addUserRegistrationDataToPageView();
$this->addTransactionDataToPageView();
$this->addPromotionsDataToPageView();
$this->addSearchQueryDataToPageView();
$this->addLoginUserDataToPageView();
$this->addCartDataToPageView();
$this->addContactFormDataToPageView();
$this->addNewsletterSubscriptionDataToPageView();
$this->addEmailToPageView();
$jsCode = '<script type="text/javascript">';
$jsCode .= 'econda.privacyprotection.setEmos3PrivacySettings();';
$jsCode .= '</script>';
$jsCode .= (string) $this->pageView;
return $jsCode;
}
/**
* @return PageView
*/
private function initializePageView()
{
$trackingPage = new PageView([
'siteId' => $this->activePageEntity->getSiteid(),
'contentLabel' => $this->activePageEntity->getContent(),
'countryId' => $this->activePageEntity->getCountryid(),
'pageId' => $this->activePageEntity->getPageid(),
'langId' => $this->activePageEntity->getLangid(),
]);
return $trackingPage;
}
private function addTransactionDataToPageView()
{
$billing = $this->activePageEntity->getBilling();
if ($billing && $this->activePageEntity->getBoughtProducts()) {
$transactionProducts = [];
foreach ($this->activePageEntity->getBoughtProducts() as $transactionProductData) {
$transactionProducts[] = new TransactionProduct($transactionProductData);
}
$billingInfo = [
'number' => $billing[0],
'customerId' => $billing[1],
'location' => $billing[2],
'value' => $billing[3],
'products' => $transactionProducts,
];
$this->pageView->add(new Tracking\Order($billingInfo));
}
}
/**
* @see https://docs.econda.de/de/MONDE/data-services/data-model-management/promotions+und+gutscheine.html
*/
private function addPromotionsDataToPageView()
{
if ($this->activePageEntity->getPromotions()) {
$this->pageView->add(new Promotions(['data' => $this->activePageEntity->getPromotions()]));
}
}
private function addCurrentProductDataToPageView()
{
if (!empty($this->activePageEntity->getProductData() && empty($this->activePageEntity->getProductToBasket()))) {
$product = new Product($this->activePageEntity->getProductData());
$this->pageView->add(new ProductDetailView($product));
}
}
private function addCurrentOrderProcessToPageView()
{
if ($this->activePageEntity->getOrderProcess()) {
$this->pageView->add(new OrderProcess($this->activePageEntity->getOrderProcess()));
}
}
private function addUserRegistrationDataToPageView()
{
if ($this->activePageEntity->getRegisteredUserId()) {
$this->pageView->add(new Registration($this->activePageEntity->getRegisteredUserId(), $this->activePageEntity->getRegisteredUserResult()));
}
}
private function addSearchQueryDataToPageView()
{
$searchQuery = $this->activePageEntity->getSearchQuery();
if ($this->activePageEntity->getSearchQuery()) {
$this->pageView->add(new \Econda\Tracking\Search($searchQuery, $this->activePageEntity->getSearchNumberOfHits()));
}
}
private function addLoginUserDataToPageView()
{
$loginUserId = $this->activePageEntity->getLoginUserId();
if ($loginUserId) {
$this->pageView->add(new \Econda\Tracking\Login($loginUserId, $this->activePageEntity->getLoginResult()));
}
}
private function addCartDataToPageView()
{
$productToBasket = $this->activePageEntity->getProductToBasket();
if (!empty($productToBasket)) {
$this->pageView->add(new Tracking\ProductAddToCart(new TransactionProduct($productToBasket)));
}
}
private function addContactFormDataToPageView()
{
$contactMessage = $this->activePageEntity->getContactsMessage();
if (!empty($contactMessage)) {
$target = new Target([
'group' => 'Kontakt',
'name' => $contactMessage,
'value' => 1
]);
$this->pageView->add($target);
}
}
private function addNewsletterSubscriptionDataToPageView()
{
$newsletterMessage = $this->activePageEntity->getNewsletterMessage();
if (!empty($newsletterMessage)) {
$target = new Target([
'group' => 'Newsletter',
'name' => $newsletterMessage,
'value' => 1
]);
$this->pageView->add($target);
}
}
private function addEmailToPageView()
{
$email = $this->activePageEntity->getEmail();
if (!empty($email)) {
$this->pageView->add(new Email($email));
}
}
}