|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Instant Analytics plugin for Craft CMS |
|
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\instantanalyticsGa4\services; |
|
12
|
|
|
|
|
13
|
|
|
use Br33f\Ga4\MeasurementProtocol\Dto\Event\ItemBaseEvent; |
|
14
|
|
|
use Br33f\Ga4\MeasurementProtocol\Dto\Event\PurchaseEvent; |
|
15
|
|
|
use Br33f\Ga4\MeasurementProtocol\Dto\Parameter\ItemParameter; |
|
16
|
|
|
use Craft; |
|
17
|
|
|
use craft\base\Component; |
|
18
|
|
|
use craft\commerce\elements\Order; |
|
|
|
|
|
|
19
|
|
|
use craft\commerce\elements\Product; |
|
|
|
|
|
|
20
|
|
|
use craft\commerce\elements\Variant; |
|
|
|
|
|
|
21
|
|
|
use craft\commerce\models\LineItem; |
|
|
|
|
|
|
22
|
|
|
use craft\elements\db\CategoryQuery; |
|
23
|
|
|
use craft\elements\db\EntryQuery; |
|
24
|
|
|
use craft\elements\db\MatrixBlockQuery; |
|
25
|
|
|
use craft\elements\db\TagQuery; |
|
26
|
|
|
use nystudio107\instantanalyticsGa4\InstantAnalytics; |
|
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
|
|
|
* Enqueue analytics information for the completed order |
|
42
|
|
|
* |
|
43
|
|
|
* @param ?Order $order the Product or Variant |
|
44
|
|
|
*/ |
|
|
|
|
|
|
45
|
|
|
public function triggerOrderCompleteEvent(Order $order = null) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($order) { |
|
48
|
|
|
$event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->PurchaseEvent(); |
|
49
|
|
|
$this->addCommerceOrderToEvent($event, $order); |
|
50
|
|
|
|
|
51
|
|
|
InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
|
52
|
|
|
|
|
53
|
|
|
InstantAnalytics::$plugin->logAnalyticsEvent( |
|
|
|
|
|
|
54
|
|
|
'Adding `Commerce - Order Complete event`: `{reference}` => `{price}`', |
|
55
|
|
|
['reference' => $order->reference, 'price' => $order->totalPrice], |
|
56
|
|
|
__METHOD__ |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Send analytics information for the item added to the cart |
|
63
|
|
|
* |
|
64
|
|
|
* @param LineItem $lineItem the line item that was added |
|
65
|
|
|
*/ |
|
|
|
|
|
|
66
|
|
|
public function triggerAddToCartEvent(LineItem $lineItem): void |
|
67
|
|
|
{ |
|
68
|
|
|
$event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->AddToCartEvent(); |
|
69
|
|
|
$this->addProductDataFromLineItem($event, $lineItem); |
|
70
|
|
|
InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
|
71
|
|
|
|
|
72
|
|
|
InstantAnalytics::$plugin->logAnalyticsEvent( |
|
73
|
|
|
'Adding `Commerce - Add to Cart event`: `{title}` => `{quantity}`', |
|
74
|
|
|
['title' => $lineItem->purchasable->title ?? $lineItem->getDescription(), 'quantity' => $lineItem->qty], |
|
75
|
|
|
__METHOD__ |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Send analytics information for the item removed from the cart |
|
81
|
|
|
* |
|
82
|
|
|
* @param LineItem $lineItem |
|
|
|
|
|
|
83
|
|
|
*/ |
|
|
|
|
|
|
84
|
|
|
public function triggerRemoveFromCartEvent(LineItem $lineItem) |
|
85
|
|
|
{ |
|
86
|
|
|
$event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->RemoveFromCartEvent(); |
|
87
|
|
|
$this->addProductDataFromLineItem($event, $lineItem); |
|
88
|
|
|
InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
|
89
|
|
|
|
|
90
|
|
|
InstantAnalytics::$plugin->logAnalyticsEvent( |
|
91
|
|
|
'Adding `Commerce - Remove from Cart event`: `{title}` => `{quantity}`', |
|
92
|
|
|
['title' => $lineItem->purchasable->title ?? $lineItem->getDescription(), 'quantity' => $lineItem->qty], |
|
93
|
|
|
__METHOD__ |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Add a Craft Commerce OrderModel to a Purchase Event |
|
100
|
|
|
* |
|
101
|
|
|
* @param PurchaseEvent $event The PurchaseEvent |
|
102
|
|
|
* @param Order $order |
|
|
|
|
|
|
103
|
|
|
*/ |
|
|
|
|
|
|
104
|
|
|
protected function addCommerceOrderToEvent(PurchaseEvent $event, Order $order) |
|
105
|
|
|
{ |
|
106
|
|
|
// First, include the transaction data |
|
107
|
|
|
$event->setCurrency($order->getPaymentCurrency()) |
|
108
|
|
|
->setTransactionId($order->reference) |
|
109
|
|
|
->setValue($order->getTotalPrice()) |
|
110
|
|
|
->setTax($order->getTotalTax()) |
|
111
|
|
|
->setShipping($order->getTotalShippingCost()); |
|
112
|
|
|
|
|
113
|
|
|
// Coupon code |
|
114
|
|
|
if ($order->couponCode) { |
|
115
|
|
|
$event->setCoupon($order->couponCode); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// Add each line item in the transaction |
|
119
|
|
|
// Two cases - variant and non variant products |
|
120
|
|
|
$index = 1; |
|
121
|
|
|
|
|
122
|
|
|
foreach ($order->lineItems as $lineItem) { |
|
123
|
|
|
$this->addProductDataFromLineItem($event, $lineItem, $index); |
|
124
|
|
|
$index++; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Add a Craft Commerce LineItem to an Analytics object |
|
130
|
|
|
* |
|
131
|
|
|
* @param ItemBaseEvent $event |
|
|
|
|
|
|
132
|
|
|
* @param LineItem $lineItem |
|
|
|
|
|
|
133
|
|
|
* @param int $index |
|
|
|
|
|
|
134
|
|
|
* @param string $listName |
|
|
|
|
|
|
135
|
|
|
* |
|
136
|
|
|
* @return string the title of the product |
|
137
|
|
|
* @throws \yii\base\InvalidConfigException |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function addProductDataFromLineItem(ItemBaseEvent $event, LineItem $lineItem, int $index = 0, string $listName = ''): string |
|
140
|
|
|
{ |
|
141
|
|
|
$eventItem = $this->getNewItemParameter(); |
|
142
|
|
|
|
|
143
|
|
|
$product = null; |
|
144
|
|
|
$purchasable = $lineItem->purchasable; |
|
145
|
|
|
|
|
146
|
|
|
$eventItem->setItemName($purchasable->title ?? $lineItem->getDescription()); |
|
147
|
|
|
$eventItem->setItemId($purchasable->getSku() ?? $lineItem->getSku()); |
|
148
|
|
|
$eventItem->setPrice($lineItem->salePrice); |
|
149
|
|
|
$eventItem->setQuantity($lineItem->qty); |
|
150
|
|
|
|
|
151
|
|
|
// Handle this purchasable being a Variant |
|
152
|
|
|
if (is_a($purchasable, Variant::class)) { |
|
153
|
|
|
/** @var Variant $purchasable */ |
|
|
|
|
|
|
154
|
|
|
$product = $purchasable->getProduct(); |
|
155
|
|
|
$variant = $purchasable; |
|
156
|
|
|
// Product with variants |
|
157
|
|
|
$eventItem->setItemName($product->title); |
|
158
|
|
|
$eventItem->setItemVariant($variant->title); |
|
159
|
|
|
$eventItem->setItemCategory($product->getType()); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
// Handle this purchasable being a Product |
|
163
|
|
|
if (is_a($purchasable, Product::class)) { |
|
164
|
|
|
/** @var Product $purchasable */ |
|
|
|
|
|
|
165
|
|
|
$product = $purchasable; |
|
166
|
|
|
$eventItem->setItemName($product->title); |
|
167
|
|
|
$eventItem->setItemVariant($product->title); |
|
168
|
|
|
$eventItem->setItemCategory($product->getType()); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
// Handle product lists |
|
172
|
|
|
if ($index) { |
|
173
|
|
|
$eventItem->setIndex($index); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
if ($listName) { |
|
177
|
|
|
$eventItem->setItemListName($listName); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
// Add in any custom categories/brands that might be set |
|
181
|
|
|
if (InstantAnalytics::$settings && $product) { |
|
182
|
|
|
if (isset(InstantAnalytics::$settings['productCategoryField']) |
|
183
|
|
|
&& !empty(InstantAnalytics::$settings['productCategoryField'])) { |
|
|
|
|
|
|
184
|
|
|
$category = $this->pullDataFromField( |
|
185
|
|
|
$product, |
|
186
|
|
|
InstantAnalytics::$settings['productCategoryField'] |
|
187
|
|
|
); |
|
188
|
|
|
$eventItem->setItemCategory($category); |
|
189
|
|
|
} |
|
190
|
|
|
if (isset(InstantAnalytics::$settings['productBrandField']) |
|
191
|
|
|
&& !empty(InstantAnalytics::$settings['productBrandField'])) { |
|
|
|
|
|
|
192
|
|
|
$brand = $this->pullDataFromField( |
|
193
|
|
|
$product, |
|
194
|
|
|
InstantAnalytics::$settings['productBrandField'] |
|
195
|
|
|
); |
|
196
|
|
|
|
|
197
|
|
|
$eventItem->setItemBrand($brand); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
//Add each product to the hit to be sent |
|
202
|
|
|
$event->addItem($eventItem); |
|
203
|
|
|
|
|
204
|
|
|
return $eventItem->getItemName(); |
|
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Add a product impression from a Craft Commerce Product or Variant |
|
209
|
|
|
* |
|
210
|
|
|
* @param Product|Variant $productVariant the Product or Variant |
|
|
|
|
|
|
211
|
|
|
* @param int $index |
|
|
|
|
|
|
212
|
|
|
* @param string $listName |
|
|
|
|
|
|
213
|
|
|
* @throws \yii\base\InvalidConfigException |
|
|
|
|
|
|
214
|
|
|
*/ |
|
|
|
|
|
|
215
|
|
|
public function addCommerceProductImpression(Variant|Product $productVariant, int $index = 0, string $listName = 'default'): void |
|
216
|
|
|
{ |
|
217
|
|
|
if ($productVariant) { |
|
218
|
|
|
$event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->ViewItemEvent(); |
|
219
|
|
|
$this->addProductDataFromProductOrVariant($event, $productVariant, $index, $listName); |
|
220
|
|
|
|
|
221
|
|
|
InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
|
222
|
|
|
|
|
223
|
|
|
$sku = $productVariant instanceof Product ? $productVariant->getDefaultVariant()->sku : $productVariant->sku; |
|
224
|
|
|
$name = $productVariant instanceof Product ? $productVariant->getName() : $productVariant->getProduct()->getName(); |
|
225
|
|
|
InstantAnalytics::$plugin->logAnalyticsEvent( |
|
226
|
|
|
'Adding view item event for `{sku}` - `{name}` - `{name}` - `{index}`', |
|
227
|
|
|
['sku' => $sku, 'name' => $name, 'index' => $index], |
|
228
|
|
|
__METHOD__ |
|
229
|
|
|
); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Add a product list impression from a Craft Commerce Product or Variant list |
|
235
|
|
|
* |
|
236
|
|
|
* @param Product[]|Variant[] $products |
|
|
|
|
|
|
237
|
|
|
* @param string $listName |
|
|
|
|
|
|
238
|
|
|
*/ |
|
|
|
|
|
|
239
|
|
|
public function addCommerceProductListImpression(array $products, string $listName = 'default'): void |
|
240
|
|
|
{ |
|
241
|
|
|
if (!empty($products)) { |
|
242
|
|
|
$event = InstantAnalytics::$plugin->ga4->getAnalytics()->create()->ViewItemListEvent(); |
|
243
|
|
|
foreach ($products as $index => $productVariant) { |
|
244
|
|
|
$this->addProductDataFromProductOrVariant($event, $productVariant, $index, $listName); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
InstantAnalytics::$plugin->ga4->getAnalytics()->addEvent($event); |
|
248
|
|
|
|
|
249
|
|
|
InstantAnalytics::$plugin->logAnalyticsEvent( |
|
250
|
|
|
'Adding view item list event. Listing {number} of items from the `{listName}` list.', |
|
251
|
|
|
['number' => count($products), 'listName' => $listName], |
|
252
|
|
|
__METHOD__ |
|
253
|
|
|
); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
|
|
|
|
|
258
|
|
|
* Extract product data from a Craft Commerce Product or Variant |
|
259
|
|
|
* |
|
260
|
|
|
* @param Product|Variant $productVariant the Product or Variant |
|
|
|
|
|
|
261
|
|
|
* |
|
262
|
|
|
* @throws \yii\base\InvalidConfigException |
|
263
|
|
|
*/ |
|
|
|
|
|
|
264
|
|
|
protected function addProductDataFromProductOrVariant(ItemBaseEvent $event, $productVariant = null, $index = 0, $listName = 'default'): void |
|
265
|
|
|
{ |
|
266
|
|
|
$eventItem = $this->getNewItemParameter(); |
|
267
|
|
|
|
|
268
|
|
|
$isVariant = $productVariant instanceof Variant; |
|
269
|
|
|
$variant = $isVariant ? $productVariant : $productVariant->getDefaultVariant(); |
|
|
|
|
|
|
270
|
|
|
|
|
271
|
|
|
if (!$variant) { |
|
272
|
|
|
return; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
$eventItem->setItemId($variant->sku); |
|
276
|
|
|
$eventItem->setItemName($variant->title); |
|
277
|
|
|
$eventItem->setPrice(number_format($variant->price, 2, '.', '')); |
|
|
|
|
|
|
278
|
|
|
|
|
279
|
|
|
$category = ($isVariant ? $variant->getProduct() : $productVariant)->getType()['name']; |
|
|
|
|
|
|
280
|
|
|
|
|
281
|
|
|
if (InstantAnalytics::$settings) { |
|
282
|
|
|
if (isset(InstantAnalytics::$settings['productCategoryField']) |
|
283
|
|
|
&& !empty(InstantAnalytics::$settings['productCategoryField'])) { |
|
|
|
|
|
|
284
|
|
|
$category = $this->pullDataFromField( |
|
285
|
|
|
$productVariant, |
|
286
|
|
|
InstantAnalytics::$settings['productCategoryField'] |
|
287
|
|
|
); |
|
288
|
|
|
if (empty($productData['category']) && $isVariant) { |
|
|
|
|
|
|
289
|
|
|
$category = $this->pullDataFromField( |
|
290
|
|
|
$productVariant->product, |
|
291
|
|
|
InstantAnalytics::$settings['productCategoryField'] |
|
292
|
|
|
); |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
$eventItem->setItemCategory($category); |
|
296
|
|
|
|
|
297
|
|
|
if (isset(InstantAnalytics::$settings['productBrandField']) |
|
298
|
|
|
&& !empty(InstantAnalytics::$settings['productBrandField'])) { |
|
|
|
|
|
|
299
|
|
|
$brand = $this->pullDataFromField( |
|
300
|
|
|
$productVariant, |
|
301
|
|
|
InstantAnalytics::$settings['productBrandField'], |
|
302
|
|
|
true |
|
303
|
|
|
); |
|
304
|
|
|
|
|
305
|
|
|
if (empty($productData['brand']) && $isVariant) { |
|
306
|
|
|
$brand = $this->pullDataFromField( |
|
307
|
|
|
$productVariant, |
|
308
|
|
|
InstantAnalytics::$settings['productBrandField'], |
|
309
|
|
|
true |
|
310
|
|
|
); |
|
311
|
|
|
} |
|
312
|
|
|
$eventItem->setItemBrand($brand); |
|
313
|
|
|
} |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
$eventItem->setIndex($index); |
|
317
|
|
|
$eventItem->setItemListName($listName); |
|
318
|
|
|
|
|
319
|
|
|
// Add item info to the event |
|
320
|
|
|
$event->addItem($eventItem); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
|
|
|
|
|
324
|
|
|
* @param Product|Variant|null $productVariant |
|
|
|
|
|
|
325
|
|
|
* @param string $fieldHandle |
|
|
|
|
|
|
326
|
|
|
* @param bool $isBrand |
|
|
|
|
|
|
327
|
|
|
* |
|
328
|
|
|
* @return string |
|
329
|
|
|
*/ |
|
330
|
|
|
protected function pullDataFromField($productVariant, $fieldHandle, $isBrand = false): string |
|
331
|
|
|
{ |
|
332
|
|
|
$result = ''; |
|
333
|
|
|
if ($productVariant && $fieldHandle) { |
|
334
|
|
|
$srcField = $productVariant[$fieldHandle] ?? $productVariant->product[$fieldHandle] ?? null; |
|
335
|
|
|
// Handle eager loaded elements |
|
336
|
|
|
if (is_array($srcField)) { |
|
337
|
|
|
return $this->getDataFromElements($isBrand, $srcField); |
|
338
|
|
|
} |
|
339
|
|
|
// If the source field isn't an object, return nothing |
|
340
|
|
|
if (!is_object($srcField)) { |
|
341
|
|
|
return $result; |
|
342
|
|
|
} |
|
343
|
|
|
switch (\get_class($srcField)) { |
|
344
|
|
|
case MatrixBlockQuery::class: |
|
|
|
|
|
|
345
|
|
|
case TagQuery::class: |
|
|
|
|
|
|
346
|
|
|
break; |
|
347
|
|
|
case CategoryQuery::class: |
|
|
|
|
|
|
348
|
|
|
case EntryQuery::class: |
|
|
|
|
|
|
349
|
|
|
$result = $this->getDataFromElements($isBrand, $srcField->all()); |
|
350
|
|
|
break; |
|
351
|
|
|
|
|
352
|
|
|
|
|
353
|
|
|
default: |
|
|
|
|
|
|
354
|
|
|
$result = strip_tags($srcField); |
|
|
|
|
|
|
355
|
|
|
break; |
|
356
|
|
|
} |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
return $result; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
|
|
|
|
|
363
|
|
|
* @param bool $isBrand |
|
|
|
|
|
|
364
|
|
|
* @param array $elements |
|
|
|
|
|
|
365
|
|
|
* @return string |
|
|
|
|
|
|
366
|
|
|
*/ |
|
367
|
|
|
protected function getDataFromElements(bool $isBrand, array $elements): string |
|
368
|
|
|
{ |
|
369
|
|
|
$cats = []; |
|
370
|
|
|
|
|
371
|
|
|
if ($isBrand) { |
|
372
|
|
|
// Because we can only have one brand, we'll get |
|
373
|
|
|
// the very last category. This means if our |
|
374
|
|
|
// brand is a sub-category, we'll get the child |
|
375
|
|
|
// not the parent. |
|
376
|
|
|
foreach ($elements as $cat) { |
|
377
|
|
|
$cats = [$cat->title]; |
|
378
|
|
|
} |
|
379
|
|
|
} else { |
|
380
|
|
|
// For every category, show its ancestors |
|
381
|
|
|
// delimited by a slash. |
|
382
|
|
|
foreach ($elements as $cat) { |
|
383
|
|
|
$name = $cat->title; |
|
384
|
|
|
|
|
385
|
|
|
while ($cat = $cat->parent) { |
|
386
|
|
|
$name = $cat->title . '/' . $name; |
|
387
|
|
|
} |
|
388
|
|
|
|
|
389
|
|
|
$cats[] = $name; |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
// Join separate categories with a pipe. |
|
394
|
|
|
return implode('|', $cats); |
|
395
|
|
|
} |
|
396
|
|
|
|
|
397
|
|
|
/** |
|
398
|
|
|
* Create an item parameter and set affiliation on it, if any exists. |
|
399
|
|
|
* |
|
400
|
|
|
* @return ItemParameter |
|
401
|
|
|
*/ |
|
402
|
|
|
protected function getNewItemParameter(): ItemParameter |
|
403
|
|
|
{ |
|
404
|
|
|
$parameter = new ItemParameter(); |
|
405
|
|
|
$parameter->setAffiliation(InstantAnalytics::$plugin->ga4->getAnalytics()->getAffiliation()); |
|
406
|
|
|
return $parameter; |
|
407
|
|
|
} |
|
408
|
|
|
} |
|
409
|
|
|
|