1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Instant Analytics plugin for Craft CMS 3.x |
4
|
|
|
* |
5
|
|
|
* Instant Analytics brings full Google Analytics support to your Twig templates |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace nystudio107\instantanalytics\services; |
12
|
|
|
|
13
|
|
|
use nystudio107\instantanalytics\InstantAnalytics; |
14
|
|
|
|
15
|
|
|
use Craft; |
16
|
|
|
use craft\base\Component; |
17
|
|
|
use craft\elements\Category; |
18
|
|
|
use craft\elements\MatrixBlock; |
19
|
|
|
use craft\elements\Tag; |
20
|
|
|
use craft\helpers\ArrayHelper; |
21
|
|
|
|
22
|
|
|
use craft\commerce\Plugin as CommercePlugin; |
|
|
|
|
23
|
|
|
use craft\commerce\base\Purchasable; |
|
|
|
|
24
|
|
|
use craft\commerce\elements\Product; |
|
|
|
|
25
|
|
|
use craft\commerce\elements\Variant; |
|
|
|
|
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Commerce Service |
30
|
|
|
* |
31
|
|
|
* @author nystudio107 |
|
|
|
|
32
|
|
|
* @package InstantAnalytics |
|
|
|
|
33
|
|
|
* @since 1.0.0 |
|
|
|
|
34
|
|
|
*/ |
|
|
|
|
35
|
|
|
class Commerce extends Component |
36
|
|
|
{ |
37
|
|
|
// Public Methods |
38
|
|
|
// ========================================================================= |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Send analytics information for the completed order |
42
|
|
|
* @param IAnalytics $analytics the Analytics object |
|
|
|
|
43
|
|
|
* @param Order $order the Product or Variant |
|
|
|
|
44
|
|
|
*/ |
|
|
|
|
45
|
|
|
public function orderComplete($order = null) |
46
|
|
|
{ |
47
|
|
|
if ($order) { |
48
|
|
|
$analytics = InstantAnalytics::$plugin->ia->eventAnalytics("Commerce", "Purchase", $order->number, $order->totalPrice); |
49
|
|
|
|
50
|
|
|
if ($analytics) { |
51
|
|
|
$this->addCommerceOrderToAnalytics($analytics, $order); |
52
|
|
|
// Don't forget to set the product action, in this case to PURCHASE |
53
|
|
|
$analytics->setProductActionToPurchase(); |
54
|
|
|
$analytics->sendEvent(); |
55
|
|
|
|
56
|
|
|
Craft::info(Craft::t('instant-analytics', 'orderComplete for `Commerce` - `Purchase` - `{number}` - `{price}`', [ 'number' => $order->number, 'price' => $order->totalPrice ]), __METHOD__); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Send analytics information for the item added to the cart |
63
|
|
|
* @param Order $order the Product or Variant |
|
|
|
|
64
|
|
|
* @param LineItem $lineItem the line item that was added |
|
|
|
|
65
|
|
|
*/ |
|
|
|
|
66
|
|
|
public function addToCart($order = null, $lineItem = null) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if ($lineItem) { |
69
|
|
|
$title = $lineItem->purchasable->title; |
70
|
|
|
$quantity = $lineItem->qty; |
71
|
|
|
$analytics = InstantAnalytics::$plugin->ia->eventAnalytics("Commerce", "Add to Cart", $title, $quantity); |
72
|
|
|
|
73
|
|
|
if ($analytics) { |
74
|
|
|
$title = $this->addProductDataFromLineItem($analytics, $lineItem); |
75
|
|
|
$analytics->setEventLabel($title); |
76
|
|
|
// Don't forget to set the product action, in this case to ADD |
77
|
|
|
$analytics->setProductActionToAdd(); |
78
|
|
|
$analytics->sendEvent(); |
79
|
|
|
|
80
|
|
|
Craft::info(Craft::t('instant-analytics', 'addToCart for `Commerce` - `Add to Cart` - `{title}` - `{quantity}`', [ 'title' => $title, 'quantity' => $quantity ]), __METHOD__); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
|
|
|
|
86
|
|
|
* Send analytics information for the item removed from the cart |
87
|
|
|
*/ |
|
|
|
|
88
|
|
|
public function removeFromCart($order = null, $lineItem = null) |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
if ($lineItem) { |
91
|
|
|
$title = $lineItem->purchasable->title; |
92
|
|
|
$quantity = $lineItem->qty; |
93
|
|
|
$analytics = InstantAnalytics::$plugin->ia->eventAnalytics("Commerce", "Remove from Cart", $title, $quantity); |
94
|
|
|
|
95
|
|
|
if ($analytics) { |
96
|
|
|
$title = $this->addProductDataFromLineItem($analytics, $lineItem); |
97
|
|
|
$analytics->setEventLabel($title); |
98
|
|
|
// Don't forget to set the product action, in this case to ADD |
99
|
|
|
$analytics->setProductActionToRemove(); |
100
|
|
|
$analytics->sendEvent(); |
101
|
|
|
|
102
|
|
|
Craft::info(Craft::t('instant-analytics', 'removeFromCart for `Commerce` - `Remove to Cart` - `{title}` - `{quantity}`', [ 'title' => $title, 'quantity' => $quantity ]), __METHOD__); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
|
|
|
|
109
|
|
|
* Add a Craft Commerce OrderModel to an Analytics object |
110
|
|
|
* @param IAnalytics $analytics the Analytics object |
|
|
|
|
111
|
|
|
* @param Order $orderModel the Product or Variant |
|
|
|
|
112
|
|
|
*/ |
|
|
|
|
113
|
|
|
public function addCommerceOrderToAnalytics($analytics = null, $order = null) |
114
|
|
|
{ |
115
|
|
|
if ($order && $analytics) { |
116
|
|
|
// First, include the transaction data |
117
|
|
|
$analytics->setTransactionId($order->number) |
118
|
|
|
->setRevenue($order->totalPrice) |
119
|
|
|
->setTax($order->totalTax) |
120
|
|
|
->setShipping($order->totalShippingCost); |
121
|
|
|
|
122
|
|
|
// Coupon code? |
123
|
|
|
if ($order->couponCode) { |
124
|
|
|
$analytics->setCouponCode($order->couponCode); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Add each line item in the transaction |
128
|
|
|
// Two cases - variant and non variant products |
129
|
|
|
$index = 1; |
130
|
|
|
|
131
|
|
|
foreach ($order->lineItems as $key => $lineItem) { |
132
|
|
|
$this->addProductDataFromLineItem($analytics, $lineItem, $index, ""); |
133
|
|
|
$index++; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
|
|
|
|
139
|
|
|
* Add a Craft Commerce LineItem to an Analytics object |
140
|
|
|
* @return string the title of the product |
|
|
|
|
141
|
|
|
*/ |
142
|
|
|
public function addProductDataFromLineItem($analytics = null, $lineItem = null, $index = 0, $listName = "") |
143
|
|
|
{ |
144
|
|
|
$result = ""; |
145
|
|
|
if ($lineItem) { |
146
|
|
|
if ($analytics) { |
147
|
|
|
//This is the same for both variant and non variant products |
148
|
|
|
$productData = [ |
149
|
|
|
'sku' => $lineItem->purchasable->sku, |
150
|
|
|
'price' => $lineItem->salePrice, |
151
|
|
|
'quantity' => $lineItem->qty, |
152
|
|
|
]; |
153
|
|
|
|
154
|
|
|
if (isset($lineItem->purchasable->product)) { |
155
|
|
|
$productVariant = $lineItem->purchasable->product; |
156
|
|
|
|
157
|
|
|
if (!$lineItem->purchasable->product->type->hasVariants) { |
158
|
|
|
//No variants (i.e. default variant) |
159
|
|
|
$productData['name'] = $lineItem->purchasable->title; |
160
|
|
|
$productData['category'] = $lineItem->purchasable->product->type['name']; |
161
|
|
|
} else { |
162
|
|
|
// Product with variants |
163
|
|
|
$productData['name'] = $lineItem->purchasable->product->title; |
164
|
|
|
$productData['category'] = $lineItem->purchasable->product->type['name']; |
165
|
|
|
$productData['variant'] = $lineItem->purchasable->title; |
166
|
|
|
} |
167
|
|
|
} else { |
168
|
|
|
$productVariant = $lineItem->purchasable; |
169
|
|
|
$productData['name'] = $lineItem->purchasable->title; |
170
|
|
|
$productData['category'] = $lineItem->purchasable->type->name; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$result = $productData['name']; |
174
|
|
|
|
175
|
|
|
if ($index) { |
176
|
|
|
$productData['position'] = $index; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
if ($listName) { |
180
|
|
|
$productData['list'] = $listName; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$settings = InstantAnalytics::$plugin->getSettings(); |
184
|
|
|
|
185
|
|
|
if (isset($settings) && isset($settings['productCategoryField']) && $settings['productCategoryField'] != "") { |
186
|
|
|
$productData['category'] = $this->_pullDataFromField($productVariant, $settings['productCategoryField']); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if (isset($settings) && isset($settings['productBrandField']) && $settings['productBrandField'] != "") { |
190
|
|
|
$productData['brand'] = $this->_pullDataFromField($productVariant, $settings['productBrandField']); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
//Add each product to the hit to be sent |
194
|
|
|
$analytics->addProduct($productData); |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $result; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
|
|
|
|
202
|
|
|
* Add a product impression from a Craft Commerce Product or Variant |
203
|
|
|
* @param IAnalytics $analytics the Analytics object |
|
|
|
|
204
|
|
|
* @param Commerce_ProductModel or Commerce_VariantModel $productVariant the Product or Variant |
|
|
|
|
205
|
|
|
* @param int $index Where the product appears in the list |
|
|
|
|
206
|
|
|
*/ |
|
|
|
|
207
|
|
|
public function addCommerceProductImpression($analytics = null, $productVariant = null, $index = 0, $listName = "default", $listIndex = 1) |
208
|
|
|
{ |
209
|
|
|
if ($productVariant && $analytics) { |
210
|
|
|
$productData = $this->getProductDataFromProduct($productVariant); |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* As per: https://github.com/theiconic/php-ga-measurement-protocol/issues/26 |
214
|
|
|
*/ |
215
|
|
|
if ($listName && $listIndex) { |
216
|
|
|
$analytics->setProductImpressionListName($listName, $listIndex); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
if ($index) { |
220
|
|
|
$productData['position'] = $index; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
//Add the product to the hit to be sent |
224
|
|
|
$analytics->addProductImpression($productData, $listIndex); |
225
|
|
|
|
226
|
|
|
Craft::info(Craft::t('instant-analytics', "addCommerceProductImpression for `{sku}` - `{name}` - `{name}` - `{index}`", [ 'sku' => $productData['sku'], 'name' => $productData['name'], 'index' => $index ]), __METHOD__); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Add a product detail view from a Craft Commerce Product or Variant |
232
|
|
|
* @param IAnalytics $analytics the Analytics object |
|
|
|
|
233
|
|
|
* @param Product or Variant $productVariant the Product or Variant |
|
|
|
|
234
|
|
|
*/ |
|
|
|
|
235
|
|
|
public function addCommerceProductDetailView($analytics = null, $productVariant = null) |
236
|
|
|
{ |
237
|
|
|
if ($productVariant && $analytics) { |
238
|
|
|
$productData = $this->getProductDataFromProduct($productVariant); |
239
|
|
|
|
240
|
|
|
// Don't forget to set the product action, in this case to DETAIL |
241
|
|
|
$analytics->setProductActionToDetail(); |
242
|
|
|
|
243
|
|
|
//Add the product to the hit to be sent |
244
|
|
|
$analytics->addProduct($productData); |
245
|
|
|
|
246
|
|
|
Craft::info(Craft::t('instant-analytics', "addCommerceProductDetailView for `{sku}` - `{name} - `{name}`", [ 'sku' => $productData['sku'], 'name' => $productData['name'] ]), __METHOD__); |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Add a checkout step and option to an Analytics object |
252
|
|
|
* @param IAnalytics $analytics the Analytics object |
|
|
|
|
253
|
|
|
* @param Commerce_OrderModel $orderModel the Product or Variant |
|
|
|
|
254
|
|
|
* @param int $step the checkout step |
|
|
|
|
255
|
|
|
* @param string $option the checkout option |
|
|
|
|
256
|
|
|
*/ |
|
|
|
|
257
|
|
|
public function addCommerceCheckoutStep($analytics = null, $orderModel = null, $step = 1, $option = "") |
258
|
|
|
{ |
259
|
|
|
if ($orderModel && $analytics) { |
260
|
|
|
// Add each line item in the transaction |
261
|
|
|
// Two cases - variant and non variant products |
262
|
|
|
$index = 1; |
263
|
|
|
|
264
|
|
|
foreach ($orderModel->lineItems as $key => $lineItem) { |
265
|
|
|
$this->addProductDataFromLineItem($analytics, $lineItem, $index, ""); |
266
|
|
|
$index++; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
$analytics->setCheckoutStep($step); |
270
|
|
|
|
271
|
|
|
if ($option) { |
272
|
|
|
$analytics->setCheckoutStepOption($option); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
// Don't forget to set the product action, in this case to CHECKOUT |
276
|
|
|
$analytics->setProductActionToCheckout(); |
277
|
|
|
|
278
|
|
|
Craft::info(Craft::t('instant-analytics', "addCommerceCheckoutStep step: `{step}` with option: `{option}`", [ 'step' => $step, 'option' => $option ]), __METHOD__); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Extract product data from a Craft Commerce Product or Variant |
284
|
|
|
* @param Commerce_ProductModel|Commerce_VariantModel $productVariant the Product or Variant |
|
|
|
|
285
|
|
|
* @return array the product data |
|
|
|
|
286
|
|
|
*/ |
287
|
|
|
public function getProductDataFromProduct($productVariant = null) |
288
|
|
|
{ |
289
|
|
|
$result = []; |
290
|
|
|
|
291
|
|
|
if ($productVariant) { |
292
|
|
|
if (is_object($productVariant) && (is_a($productVariant, Product::class) || is_a($productVariant, Purchasable::class))) { |
293
|
|
|
$productType = property_exists($productVariant, "typeId") ? CommercePlugin::getInstance()->getProductTypes()->getProductTypeById($productVariant->typeId) : null; |
294
|
|
|
|
295
|
|
|
if ($productType && $productType->hasVariants) { |
296
|
|
|
$productVariant = ArrayHelper::getFirstValue($productVariant->getVariants()); |
|
|
|
|
297
|
|
|
$product = $productVariant->getProduct(); |
298
|
|
|
|
299
|
|
|
if ($product) { |
300
|
|
|
$category = $product->getType()['name']; |
301
|
|
|
$name = $product->title; |
302
|
|
|
$variant = $productVariant->title; |
303
|
|
|
} else { |
304
|
|
|
$category = $productVariant->getType()['name']; |
305
|
|
|
$name = $productVariant->title; |
306
|
|
|
$variant = ""; |
307
|
|
|
} |
308
|
|
|
} else { |
309
|
|
|
if (isset($productVariant->defaultVariantId)) { |
310
|
|
|
$productVariant = CommercePlugin::getInstance()->getVariants()->getVariantById($productVariant->defaultVariantId); |
311
|
|
|
$category = $productVariant->getProduct()->getType()['name']; |
312
|
|
|
$name = $productVariant->title; |
313
|
|
|
$variant = ""; |
314
|
|
|
} else { |
315
|
|
|
if (isset($productVariant->product)) { |
316
|
|
|
$category = $productVariant->product->getType()['name']; |
317
|
|
|
$name = $productVariant->product->title; |
318
|
|
|
} else { |
319
|
|
|
$category = $productVariant->getType()['name']; |
320
|
|
|
$name = $productVariant->title; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
$variant = $productVariant->title; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
$productData = [ |
329
|
|
|
'sku' => $productVariant->sku, |
330
|
|
|
'name' => $name, |
|
|
|
|
331
|
|
|
'price' => number_format($productVariant->price, 2, '.', ''), |
332
|
|
|
'category' => $category, |
|
|
|
|
333
|
|
|
]; |
334
|
|
|
|
335
|
|
|
if ($variant) { |
|
|
|
|
336
|
|
|
$productData['variant'] = $variant; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
$settings = InstantAnalytics::$plugin->getSettings(); |
340
|
|
|
$isVariant = is_a($productVariant, Variant::class); |
341
|
|
|
|
342
|
|
|
if (isset($settings) && isset($settings['productCategoryField']) && $settings['productCategoryField'] != "") { |
343
|
|
|
$productData['category'] = $this->_pullDataFromField( |
344
|
|
|
$productVariant, |
345
|
|
|
$settings['productCategoryField'] |
346
|
|
|
); |
347
|
|
|
|
348
|
|
|
if (empty($productData['category']) && $isVariant) { |
349
|
|
|
$productData['category'] = $this->_pullDataFromField( |
350
|
|
|
$productVariant->product, |
351
|
|
|
$settings['productCategoryField'] |
352
|
|
|
); |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
if (isset($settings) && isset($settings['productBrandField']) && $settings['productBrandField'] != "") { |
357
|
|
|
$productData['brand'] = $this->_pullDataFromField( |
358
|
|
|
$productVariant, |
359
|
|
|
$settings['productBrandField'], |
360
|
|
|
true |
361
|
|
|
); |
362
|
|
|
|
363
|
|
|
if (empty($productData['brand']) && $isVariant) { |
364
|
|
|
$productData['brand'] = $this->_pullDataFromField( |
365
|
|
|
$productVariant, |
366
|
|
|
$settings['productBrandField'], |
367
|
|
|
true |
368
|
|
|
); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
$result = $productData; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
return $result; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
|
379
|
|
|
/** |
|
|
|
|
380
|
|
|
* Extract the value of a field |
381
|
|
|
* @param Commerce_OrderModel $orderModel the Product or Variant |
|
|
|
|
382
|
|
|
* @param Commerce_LineItemModel $lineItem the line item that was added |
|
|
|
|
383
|
|
|
* @param boolean $isBrand Are we getting the brand? |
|
|
|
|
384
|
|
|
* @return string |
|
|
|
|
385
|
|
|
*/ |
386
|
|
|
private function _pullDataFromField($productVariant, $fieldHandle, $isBrand = false) |
387
|
|
|
{ |
388
|
|
|
$result = ""; |
389
|
|
|
|
390
|
|
|
if ($productVariant) { |
391
|
|
|
if ($fieldHandle) { |
392
|
|
|
$srcField = $productVariant[$fieldHandle]; |
393
|
|
|
|
394
|
|
|
if ($srcField == null) { |
395
|
|
|
$srcField = $productVariant->product->content->attributes[$fieldHandle]; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
if (isset($srcField->elementType)) { |
399
|
|
|
switch ($srcField->elementType->classHandle) { |
400
|
|
|
case MatrixBlock::class: |
|
|
|
|
401
|
|
|
break; |
402
|
|
|
case Tag::class: |
|
|
|
|
403
|
|
|
break; |
404
|
|
|
case Category::class: { |
|
|
|
|
405
|
|
|
$cats = []; |
406
|
|
|
|
407
|
|
|
if ($isBrand) { |
|
|
|
|
408
|
|
|
// Because we can only have one brand, we'll get |
409
|
|
|
// the very last category. This means if our |
410
|
|
|
// brand is a sub-category, we'll get the child |
411
|
|
|
// not the parent. |
412
|
|
|
/** @var CategoryModel $cat */ |
|
|
|
|
413
|
|
|
foreach ($srcField as $cat) { |
|
|
|
|
414
|
|
|
$cats = [$cat->getTitle()]; |
415
|
|
|
} |
|
|
|
|
416
|
|
|
} else { |
|
|
|
|
417
|
|
|
// For every category, show its ancestors |
418
|
|
|
// delimited by a slash. |
419
|
|
|
/** @var CategoryModel $cat */ |
|
|
|
|
420
|
|
|
foreach ($srcField as $cat) { |
|
|
|
|
421
|
|
|
$name = $cat->getTitle(); |
422
|
|
|
|
423
|
|
|
while ($cat = $cat->getParent()) { |
|
|
|
|
424
|
|
|
$name = $cat->getTitle() . "/" . $name; |
425
|
|
|
} |
|
|
|
|
426
|
|
|
|
427
|
|
|
$cats[] = $name; |
428
|
|
|
} |
|
|
|
|
429
|
|
|
} |
|
|
|
|
430
|
|
|
|
431
|
|
|
// Join separate categories with a pipe. |
432
|
|
|
$result = implode("|", $cats); |
433
|
|
|
break; |
434
|
|
|
} |
|
|
|
|
435
|
|
|
|
436
|
|
|
default: |
|
|
|
|
437
|
|
|
$result = strip_tags($srcField); |
|
|
|
|
438
|
|
|
break; |
439
|
|
|
} |
440
|
|
|
} else { |
441
|
|
|
$result = strip_tags($srcField); |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
return $result; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
} |
450
|
|
|
|