|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use CMSFactory\assetManager; |
|
4
|
|
|
use CMSFactory\Events; |
|
5
|
|
|
use Propel\Runtime\Exception\PropelException; |
|
6
|
|
|
|
|
7
|
|
|
(defined('BASEPATH')) OR exit('No direct script access allowed'); |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Image CMS |
|
11
|
|
|
* Module Frame |
|
12
|
|
|
* @property Cms_base $cms_base |
|
13
|
|
|
* @link https://enhancedecommerce.appspot.com/ |
|
14
|
|
|
*/ |
|
15
|
|
|
class Ga_dashboard extends MY_Controller |
|
16
|
|
|
{ |
|
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Ga_dashboard constructor. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct() { |
|
22
|
|
|
|
|
23
|
|
|
parent::__construct(); |
|
24
|
|
|
$lang = new MY_Lang(); |
|
25
|
|
|
$lang->load('ga_dashboard'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @throws Exception |
|
|
|
|
|
|
30
|
|
|
*/ |
|
31
|
|
|
public function autoload() { |
|
32
|
|
|
|
|
33
|
|
|
$settings = $this->cms_base->get_settings(); |
|
34
|
|
|
|
|
35
|
|
|
if ($settings['yandex_metric'] != '') { |
|
36
|
|
|
assetManager::create()->registerJsScript('window.dataLayer = window.dataLayer || [];', false, 'before'); |
|
37
|
|
|
Events::create()->onProductPageLoad()->setListener('YMProductPageLoad'); |
|
38
|
|
|
Events::create()->onShopOrderView()->setListener('YMShopOrderView'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if ($settings['google_analytics_ee'] == 1 and $settings['google_analytics_id'] != '') { |
|
42
|
|
|
Events::create()->onProductPageLoad()->setListener('ProductPageLoad'); |
|
43
|
|
|
// Events::create()->onAddItemToCart()->setListener('ProductAddToCart'); |
|
44
|
|
|
Events::create()->onCategoryPageLoad()->setListener('CategorySearchPageLoad'); |
|
45
|
|
|
Events::create()->onSearchPageLoad()->setListener('CategorySearchPageLoad'); |
|
46
|
|
|
Events::create()->onBrandPageLoad()->setListener('CategorySearchPageLoad'); |
|
47
|
|
|
Events::create()->onShopOrderView()->setListener('ShopOrderView'); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Search $categoryObj |
|
53
|
|
|
* @throws PropelException |
|
|
|
|
|
|
54
|
|
|
*/ |
|
55
|
|
|
public static function CategorySearchPageLoad($categoryObj) { |
|
56
|
|
|
|
|
57
|
|
|
$products = $categoryObj->data['products'] ?: $categoryObj['products']; |
|
58
|
|
|
|
|
59
|
|
|
if (count($products) > 0) { |
|
60
|
|
|
|
|
61
|
|
|
$url = CI::$APP->uri->uri_string(); |
|
62
|
|
|
|
|
63
|
|
|
if (false !== strpos($url, 'shop/search')) { |
|
64
|
|
|
$list = 'search_page'; |
|
65
|
|
|
} elseif (false !== strpos($url, 'shop/category')) { |
|
66
|
|
|
$list = 'category_page'; |
|
67
|
|
|
} elseif (false !== strpos($url, 'shop/brand')) { |
|
68
|
|
|
$list = 'brand_page'; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/* @var $product SProducts */ |
|
72
|
|
|
$gaProducts = []; |
|
73
|
|
|
foreach ($products as $key => $product) { |
|
74
|
|
|
$SBrands = $product->getBrand(); |
|
75
|
|
|
$brand = $SBrands ? $SBrands->getName() : ''; |
|
76
|
|
|
$gaProducts[$key]['id'] = $product->getId(); |
|
77
|
|
|
$gaProducts[$key]['name'] = $product->getName(); |
|
78
|
|
|
$gaProducts[$key]['price'] = $product->getFirstVariant()->toCurrency(); |
|
79
|
|
|
$gaProducts[$key]['brand'] = $brand; |
|
80
|
|
|
$gaProducts[$key]['category'] = $product->getMainCategory()->getName(); |
|
81
|
|
|
$gaProducts[$key]['list'] = $list; |
|
82
|
|
|
$gaProducts[$key]['position'] = $key; |
|
83
|
|
|
} |
|
84
|
|
|
assetManager::create() |
|
85
|
|
|
->registerJsScript('var gaProducts = ' . json_encode($gaProducts) . ';', FALSE, 'before') |
|
86
|
|
|
->registerScript('category', TRUE, 'after'); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param $data |
|
|
|
|
|
|
92
|
|
|
*/ |
|
93
|
|
|
public static function ProductAddToCart($data) { |
|
94
|
|
|
return; |
|
95
|
|
|
if ($data['instance'] != 'SProducts') { |
|
|
|
|
|
|
96
|
|
|
return; |
|
97
|
|
|
} |
|
98
|
|
|
$model = SProductsQuery::create() |
|
99
|
|
|
->joinProductVariant() |
|
100
|
|
|
->useProductVariantQuery() |
|
101
|
|
|
->filterById($data['id']) |
|
102
|
|
|
->endUse() |
|
103
|
|
|
->findOne(); |
|
104
|
|
|
|
|
105
|
|
|
assetManager::create() |
|
106
|
|
|
->registerJsScript("var id = {$model->getId()};", true, 'before') |
|
107
|
|
|
->registerJsScript("var name = {$model->getName()};", FALSE, 'before') |
|
108
|
|
|
->registerJsScript("var price = {$model->getFirstVariant()->toCurrency()};", FALSE, 'before') |
|
109
|
|
|
->registerJsScript("var brand = {$model->getId()};", FALSE, 'before') |
|
110
|
|
|
->registerJsScript("var category = {$model->getId()};", FALSE, 'before') |
|
111
|
|
|
->registerScript('addToCart', TRUE, 'after'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public static function ProductPageLoad($data) { |
|
115
|
|
|
assetManager::create() |
|
116
|
|
|
->registerJsScript('var gaProduct = ' . self::createProductData($data) . ';', FALSE, 'before') |
|
117
|
|
|
->registerScript('product', FALSE, 'after'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public static function YMProductPageLoad($data) { |
|
121
|
|
|
|
|
122
|
|
|
assetManager::create() |
|
123
|
|
|
->registerJsScript( |
|
124
|
|
|
'window.dataLayer.push({ |
|
125
|
|
|
"ecommerce": { |
|
126
|
|
|
"detail" : { |
|
127
|
|
|
"products" : [' . self::createProductData($data) . '] |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
});', |
|
131
|
|
|
false, |
|
132
|
|
|
'before' |
|
133
|
|
|
); |
|
134
|
|
|
assetManager::create(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param array $data |
|
139
|
|
|
* @throws PropelException |
|
|
|
|
|
|
140
|
|
|
*/ |
|
141
|
|
|
public static function YMShopOrderView($data) { |
|
142
|
|
|
|
|
143
|
|
|
/** @var $data SOrders */ |
|
144
|
|
|
if ($data instanceof SOrders && CI::$APP->session->flashdata('makeOrderForGA')) { |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
assetManager::create() |
|
147
|
|
|
->registerJsScript( |
|
148
|
|
|
'window.dataLayer.push({ |
|
149
|
|
|
"ecommerce": { |
|
150
|
|
|
"purchase" : { |
|
151
|
|
|
"actionField": ' . self::createOrderData($data, true) . ', |
|
152
|
|
|
"products" : ' . self::createOrderProductsData($data) . ' |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
});', |
|
156
|
|
|
false, |
|
157
|
|
|
'before' |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
private static function createProductData($data) { |
|
163
|
|
|
/* @var $model SProducts */ |
|
164
|
|
|
$model = $data['model']; |
|
165
|
|
|
|
|
166
|
|
|
$gaProduct = []; |
|
167
|
|
|
$gaProduct['id'] = $model->getId(); |
|
168
|
|
|
$gaProduct['name'] = $model->getName(); |
|
169
|
|
|
$gaProduct['price'] = $model->getFirstVariant()->toCurrency(); |
|
170
|
|
|
$SBrands = $model->getBrand(); |
|
171
|
|
|
$gaProduct['brand'] = $SBrands ? $SBrands->getName() : ''; |
|
172
|
|
|
$gaProduct['category'] = $model->getMainCategory()->getName(); |
|
173
|
|
|
|
|
174
|
|
|
return json_encode($gaProduct); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
private static function createOrderData($data, $ym = false) { |
|
178
|
|
|
$order['id'] = $data->getKey(); |
|
|
|
|
|
|
179
|
|
|
$order['revenue'] = $data->getTotalPrice(); |
|
180
|
|
|
if (!$ym) { |
|
181
|
|
|
$order['shipping'] = $data->getDeliveryPrice() ?: 0; |
|
182
|
|
|
} |
|
183
|
|
|
return json_encode($order); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
private static function createOrderProductsData($data) { |
|
187
|
|
|
$products = []; |
|
188
|
|
|
foreach ($data->getSOrderProductss() as $key => $orderProduct) { |
|
189
|
|
|
$productVariant = $orderProduct->getVariant(); |
|
190
|
|
|
if ($productVariant instanceof SProductVariants) { |
|
|
|
|
|
|
191
|
|
|
$product = $productVariant->getSProducts(); |
|
192
|
|
|
|
|
193
|
|
|
$products[$key]['id'] = $productVariant->getId(); |
|
194
|
|
|
$products[$key]['name'] = $orderProduct->getProductName(); |
|
195
|
|
|
$products[$key]['price'] = $orderProduct->getPrice(); |
|
196
|
|
|
$SBrands = $product->getBrand(); |
|
197
|
|
|
$products[$key]['brand'] = $SBrands ? $SBrands->getName() : ''; |
|
198
|
|
|
$products[$key]['category'] = $product->getMainCategory()->getName(); |
|
199
|
|
|
$products[$key]['variant'] = $productVariant->getName(); |
|
200
|
|
|
$products[$key]['position'] = $key; |
|
201
|
|
|
$products[$key]['quantity'] = $orderProduct->getQuantity(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return json_encode($products); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param array $data |
|
211
|
|
|
* @throws PropelException |
|
|
|
|
|
|
212
|
|
|
*/ |
|
213
|
|
|
public static function ShopOrderView($data) { |
|
214
|
|
|
|
|
215
|
|
|
/** @var $data SOrders */ |
|
216
|
|
|
if ($data instanceof SOrders && CI::$APP->session->flashdata('makeOrderForGA')) { |
|
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
assetManager::create() |
|
219
|
|
|
->registerJsScript('var gaOrderObject = ' . self::createOrderData($data) . ';') |
|
220
|
|
|
->registerJsScript('var gaOrderProductsObject = ' . self::createOrderProductsData($data) . ';') |
|
221
|
|
|
->registerScript('order_view', FALSE, 'after'); |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
public function index() { |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
public function _deinstall() { |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
public function _install() { |
|
232
|
|
|
$this->db |
|
233
|
|
|
->where('name', 'ga_dashboard') |
|
234
|
|
|
->update('components', ['autoload' => '1', 'enabled' => '1']); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/* End of file sample_module.php */ |