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; |
12
|
|
|
|
13
|
|
|
use Craft; |
14
|
|
|
use craft\base\Model; |
15
|
|
|
use craft\base\Plugin; |
16
|
|
|
use craft\commerce\elements\Order; |
|
|
|
|
17
|
|
|
use craft\commerce\events\LineItemEvent; |
|
|
|
|
18
|
|
|
use craft\commerce\Plugin as Commerce; |
|
|
|
|
19
|
|
|
use craft\events\PluginEvent; |
20
|
|
|
use craft\events\RegisterUrlRulesEvent; |
21
|
|
|
use craft\events\TemplateEvent; |
22
|
|
|
use craft\helpers\UrlHelper; |
23
|
|
|
use craft\services\Plugins; |
24
|
|
|
use craft\web\twig\variables\CraftVariable; |
25
|
|
|
use craft\web\UrlManager; |
26
|
|
|
use craft\web\View; |
27
|
|
|
use Exception; |
28
|
|
|
use nystudio107\instantanalyticsGa4\helpers\Analytics; |
29
|
|
|
use nystudio107\instantanalyticsGa4\helpers\Field as FieldHelper; |
30
|
|
|
use nystudio107\instantanalyticsGa4\models\Settings; |
31
|
|
|
use nystudio107\instantanalyticsGa4\services\ServicesTrait; |
32
|
|
|
use nystudio107\instantanalyticsGa4\twigextensions\InstantAnalyticsTwigExtension; |
33
|
|
|
use nystudio107\instantanalyticsGa4\variables\InstantAnalyticsVariable; |
34
|
|
|
use nystudio107\seomatic\Seomatic; |
|
|
|
|
35
|
|
|
use yii\base\Event; |
36
|
|
|
use yii\web\Response; |
37
|
|
|
use function array_merge; |
38
|
|
|
|
39
|
|
|
/** |
|
|
|
|
40
|
|
|
* @author nystudio107 |
|
|
|
|
41
|
|
|
* @package InstantAnalytics |
|
|
|
|
42
|
|
|
* @since 1.0.0 |
|
|
|
|
43
|
|
|
*/ |
|
|
|
|
44
|
|
|
class InstantAnalytics extends Plugin |
45
|
|
|
{ |
46
|
|
|
// Traits |
47
|
|
|
// ========================================================================= |
48
|
|
|
|
49
|
|
|
use ServicesTrait; |
50
|
|
|
|
51
|
|
|
// Constants |
52
|
|
|
// ========================================================================= |
53
|
|
|
|
54
|
|
|
/** |
|
|
|
|
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected const COMMERCE_PLUGIN_HANDLE = 'commerce'; |
58
|
|
|
|
59
|
|
|
/** |
|
|
|
|
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
protected const SEOMATIC_PLUGIN_HANDLE = 'seomatic'; |
63
|
|
|
|
64
|
|
|
// Static Properties |
65
|
|
|
// ========================================================================= |
66
|
|
|
|
67
|
|
|
/** |
|
|
|
|
68
|
|
|
* @var null|InstantAnalytics |
69
|
|
|
*/ |
70
|
|
|
public static $plugin = null; |
71
|
|
|
|
72
|
|
|
/** |
|
|
|
|
73
|
|
|
* @var null|Settings |
74
|
|
|
*/ |
75
|
|
|
public static $settings = null; |
76
|
|
|
|
77
|
|
|
/** |
|
|
|
|
78
|
|
|
* @var null|Commerce |
79
|
|
|
*/ |
80
|
|
|
public static $commercePlugin = null; |
81
|
|
|
|
82
|
|
|
/** |
|
|
|
|
83
|
|
|
* @var null|Seomatic |
84
|
|
|
*/ |
85
|
|
|
public static $seomaticPlugin = null; |
86
|
|
|
|
87
|
|
|
/** |
|
|
|
|
88
|
|
|
* @var string |
89
|
|
|
*/ |
90
|
|
|
public static $currentTemplate = ''; |
91
|
|
|
|
92
|
|
|
/** |
|
|
|
|
93
|
|
|
* @var bool |
94
|
|
|
*/ |
95
|
|
|
public static $pageViewSent = false; |
96
|
|
|
|
97
|
|
|
// Public Properties |
98
|
|
|
// ========================================================================= |
99
|
|
|
|
100
|
|
|
/** |
|
|
|
|
101
|
|
|
* @var string |
102
|
|
|
*/ |
103
|
|
|
public $schemaVersion = '1.0.0'; |
104
|
|
|
|
105
|
|
|
/** |
|
|
|
|
106
|
|
|
* @var bool |
107
|
|
|
*/ |
108
|
|
|
public $hasCpSection = false; |
109
|
|
|
|
110
|
|
|
/** |
|
|
|
|
111
|
|
|
* @var bool |
112
|
|
|
*/ |
113
|
|
|
public $hasCpSettings = true; |
114
|
|
|
|
115
|
|
|
// Public Methods |
116
|
|
|
// ========================================================================= |
117
|
|
|
|
118
|
|
|
/** |
|
|
|
|
119
|
|
|
* @inheritdoc |
120
|
|
|
*/ |
|
|
|
|
121
|
|
|
public function init(): void |
122
|
|
|
{ |
123
|
|
|
parent::init(); |
124
|
|
|
self::$plugin = $this; |
125
|
|
|
self::$settings = $this->getSettings(); |
126
|
|
|
|
127
|
|
|
// Add in our Craft components |
128
|
|
|
$this->addComponents(); |
129
|
|
|
// Install our global event handlers |
130
|
|
|
$this->installEventListeners(); |
131
|
|
|
|
132
|
|
|
Craft::info( |
133
|
|
|
Craft::t( |
134
|
|
|
'instant-analytics-ga4', |
135
|
|
|
'{name} plugin loaded', |
136
|
|
|
['name' => $this->name] |
137
|
|
|
), |
138
|
|
|
__METHOD__ |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
|
|
|
|
143
|
|
|
* @inheritdoc |
144
|
|
|
*/ |
|
|
|
|
145
|
|
|
protected function settingsHtml(): ?string |
146
|
|
|
{ |
147
|
|
|
$commerceFields = []; |
148
|
|
|
|
149
|
|
|
if (self::$commercePlugin !== null) { |
150
|
|
|
$productTypes = self::$commercePlugin->getProductTypes()->getAllProductTypes(); |
151
|
|
|
|
152
|
|
|
foreach ($productTypes as $productType) { |
153
|
|
|
$productFields = $this->getPullFieldsFromLayoutId($productType->fieldLayoutId); |
154
|
|
|
/** @noinspection SlowArrayOperationsInLoopInspection */ |
|
|
|
|
155
|
|
|
$commerceFields = array_merge($commerceFields, $productFields); |
156
|
|
|
if ($productType->hasVariants) { |
157
|
|
|
$variantFields = $this->getPullFieldsFromLayoutId($productType->variantFieldLayoutId); |
158
|
|
|
/** @noinspection SlowArrayOperationsInLoopInspection */ |
|
|
|
|
159
|
|
|
$commerceFields = array_merge($commerceFields, $variantFields); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
// Rend the settings template |
165
|
|
|
try { |
166
|
|
|
return Craft::$app->getView()->renderTemplate( |
167
|
|
|
'instant-analytics-ga4/settings', |
168
|
|
|
[ |
169
|
|
|
'settings' => $this->getSettings(), |
170
|
|
|
'commerceFields' => $commerceFields, |
171
|
|
|
] |
172
|
|
|
); |
173
|
|
|
} catch (Exception $exception) { |
174
|
|
|
Craft::error($exception->getMessage(), __METHOD__); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
return ''; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
|
|
|
|
181
|
|
|
* Handle the `{% hook iaSendPageView %}` |
182
|
|
|
*/ |
|
|
|
|
183
|
|
|
public function iaSendPageView(/** @noinspection PhpUnusedParameterInspection */ array &$context = []): string |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
$this->ga4->addPageViewEvent(); |
186
|
|
|
return ''; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
|
|
|
|
190
|
|
|
* Handle the `{% hook iaInsertGtag %}` |
191
|
|
|
*/ |
|
|
|
|
192
|
|
|
public function iaInsertGtag(/** @noinspection PhpUnusedParameterInspection */ array &$context = []): string |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
$userSegment = ''; |
195
|
|
|
|
196
|
|
|
if (self::$settings->sendUserId) { |
197
|
|
|
$userId = Analytics::getUserId(); |
198
|
|
|
if (!empty($userId)) { |
199
|
|
|
$userSegment = "'user_id': '$userId'"; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$measurementId = Craft::parseEnv(self::$settings->googleAnalyticsMeasurementId); |
|
|
|
|
204
|
|
|
|
205
|
|
|
return <<<GTAG |
206
|
|
|
<!-- Google tag (gtag.js) --> |
207
|
|
|
<script async src="https://www.googletagmanager.com/gtag/js?id=$measurementId"></script> |
208
|
|
|
<script> |
209
|
|
|
window.dataLayer = window.dataLayer || []; |
210
|
|
|
function gtag(){dataLayer.push(arguments);} |
211
|
|
|
gtag('js', new Date()); |
212
|
|
|
|
213
|
|
|
gtag('config', '$measurementId', |
214
|
|
|
{ |
215
|
|
|
$userSegment |
216
|
|
|
}); |
217
|
|
|
</script> |
218
|
|
|
GTAG; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function logAnalyticsEvent(string $message, array $variables = [], string $category = ''): void |
|
|
|
|
222
|
|
|
{ |
223
|
|
|
Craft::info( |
224
|
|
|
Craft::t('instant-analytics-ga4', $message, $variables), |
225
|
|
|
$category |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
// Protected Methods |
229
|
|
|
// ========================================================================= |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Add in our Craft components |
233
|
|
|
*/ |
|
|
|
|
234
|
|
|
protected function addComponents(): void |
235
|
|
|
{ |
236
|
|
|
$view = Craft::$app->getView(); |
237
|
|
|
// Add in our Twig extensions |
238
|
|
|
$view->registerTwigExtension(new InstantAnalyticsTwigExtension()); |
239
|
|
|
|
240
|
|
|
// Install our template hooks |
241
|
|
|
$view->hook('iaSendPageView', [$this, 'iaSendPageView']); |
242
|
|
|
$view->hook('iaInsertGtag', [$this, 'iaInsertGtag']); |
243
|
|
|
|
244
|
|
|
// Register our variables |
245
|
|
|
Event::on( |
246
|
|
|
CraftVariable::class, |
247
|
|
|
CraftVariable::EVENT_INIT, |
248
|
|
|
function (Event $event): void { |
249
|
|
|
/** @var CraftVariable $variable */ |
|
|
|
|
250
|
|
|
$variable = $event->sender; |
251
|
|
|
$variable->set('instantAnalytics', [ |
|
|
|
|
252
|
|
|
'class' => InstantAnalyticsVariable::class, |
253
|
|
|
'viteService' => $this->vite, |
254
|
|
|
]); |
|
|
|
|
255
|
|
|
} |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Install our event listeners |
261
|
|
|
*/ |
|
|
|
|
262
|
|
|
protected function installEventListeners(): void |
263
|
|
|
{ |
264
|
|
|
// Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN |
265
|
|
|
Event::on( |
266
|
|
|
Plugins::class, |
267
|
|
|
Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
268
|
|
|
function (PluginEvent $event): void { |
269
|
|
|
if ($event->plugin === $this) { |
270
|
|
|
$request = Craft::$app->getRequest(); |
271
|
|
|
if ($request->isCpRequest) { |
272
|
|
|
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('instant-analytics-ga4/welcome'))->send(); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
} |
276
|
|
|
); |
277
|
|
|
|
278
|
|
|
// Handler: Plugins::EVENT_AFTER_LOAD_PLUGINS |
279
|
|
|
Event::on( |
280
|
|
|
Plugins::class, |
281
|
|
|
Plugins::EVENT_AFTER_LOAD_PLUGINS, |
282
|
|
|
function (Event $event): void { |
|
|
|
|
283
|
|
|
// Determine if Craft Commerce is installed & enabled |
284
|
|
|
self::$commercePlugin = Craft::$app->getPlugins()->getPlugin(self::COMMERCE_PLUGIN_HANDLE); |
285
|
|
|
// Determine if SEOmatic is installed & enabled |
286
|
|
|
self::$seomaticPlugin = Craft::$app->getPlugins()->getPlugin(self::SEOMATIC_PLUGIN_HANDLE); |
287
|
|
|
|
288
|
|
|
// Make sure to install these only after we definitely know whether other plugins are installed |
289
|
|
|
$request = Craft::$app->getRequest(); |
290
|
|
|
// Install only for non-console site requests |
291
|
|
|
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
292
|
|
|
$this->installSiteEventListeners(); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
// Install only for non-console Control Panel requests |
296
|
|
|
if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
297
|
|
|
$this->installCpEventListeners(); |
298
|
|
|
} |
299
|
|
|
} |
300
|
|
|
); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Install site event listeners for site requests only |
305
|
|
|
*/ |
|
|
|
|
306
|
|
|
protected function installSiteEventListeners(): void |
307
|
|
|
{ |
308
|
|
|
// Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
309
|
|
|
Event::on( |
310
|
|
|
UrlManager::class, |
311
|
|
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
312
|
|
|
function (RegisterUrlRulesEvent $event): void { |
313
|
|
|
Craft::debug( |
314
|
|
|
'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
315
|
|
|
__METHOD__ |
316
|
|
|
); |
317
|
|
|
// Register our Control Panel routes |
318
|
|
|
$event->rules = array_merge( |
319
|
|
|
$event->rules, |
320
|
|
|
$this->customFrontendRoutes() |
321
|
|
|
); |
322
|
|
|
} |
323
|
|
|
); |
324
|
|
|
// Remember the name of the currently rendering template |
325
|
|
|
Event::on( |
326
|
|
|
View::class, |
327
|
|
|
View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, |
328
|
|
|
static function (TemplateEvent $event): void { |
329
|
|
|
self::$currentTemplate = $event->template; |
330
|
|
|
} |
331
|
|
|
); |
332
|
|
|
// Send the page-view event. |
333
|
|
|
Event::on( |
334
|
|
|
View::class, |
335
|
|
|
View::EVENT_AFTER_RENDER_PAGE_TEMPLATE, |
336
|
|
|
function (TemplateEvent $event): void { |
|
|
|
|
337
|
|
|
if (self::$settings->autoSendPageView) { |
338
|
|
|
$request = Craft::$app->getRequest(); |
339
|
|
|
if (!$request->getIsAjax()) { |
340
|
|
|
$this->ga4->addPageViewEvent(); |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
); |
345
|
|
|
|
346
|
|
|
// Send the collected events |
347
|
|
|
Event::on( |
348
|
|
|
Response::class, |
349
|
|
|
Response::EVENT_BEFORE_SEND, |
350
|
|
|
function (Event $event): void { |
|
|
|
|
351
|
|
|
// Initialize this sooner rather than later, since it's possible this will want to tinker with cookies |
352
|
|
|
$this->ga4->getAnalytics(); |
353
|
|
|
} |
354
|
|
|
); |
355
|
|
|
|
356
|
|
|
// Send the collected events |
357
|
|
|
Event::on( |
358
|
|
|
Response::class, |
359
|
|
|
Response::EVENT_AFTER_SEND, |
360
|
|
|
function (Event $event): void { |
|
|
|
|
361
|
|
|
$this->ga4->getAnalytics()->sendCollectedEvents(); |
362
|
|
|
} |
363
|
|
|
); |
364
|
|
|
|
365
|
|
|
// Commerce-specific hooks |
366
|
|
|
if (self::$commercePlugin !== null) { |
367
|
|
|
Event::on(Order::class, Order::EVENT_AFTER_COMPLETE_ORDER, function (Event $e): void { |
|
|
|
|
368
|
|
|
$order = $e->sender; |
369
|
|
|
if (self::$settings->autoSendPurchaseComplete) { |
370
|
|
|
$this->commerce->triggerOrderCompleteEvent($order); |
371
|
|
|
} |
372
|
|
|
}); |
|
|
|
|
373
|
|
|
|
374
|
|
|
Event::on(Order::class, Order::EVENT_AFTER_ADD_LINE_ITEM, function (LineItemEvent $e): void { |
|
|
|
|
375
|
|
|
$lineItem = $e->lineItem; |
376
|
|
|
if (self::$settings->autoSendAddToCart) { |
377
|
|
|
$this->commerce->triggerAddToCartEvent($lineItem); |
378
|
|
|
} |
379
|
|
|
}); |
|
|
|
|
380
|
|
|
|
381
|
|
|
// Check to make sure Order::EVENT_AFTER_REMOVE_LINE_ITEM is defined |
382
|
|
|
if (defined(Order::class . '::EVENT_AFTER_REMOVE_LINE_ITEM')) { |
383
|
|
|
Event::on(Order::class, Order::EVENT_AFTER_REMOVE_LINE_ITEM, function (LineItemEvent $e): void { |
|
|
|
|
384
|
|
|
$lineItem = $e->lineItem; |
385
|
|
|
if (self::$settings->autoSendRemoveFromCart) { |
386
|
|
|
$this->commerce->triggerRemoveFromCartEvent($lineItem); |
387
|
|
|
} |
388
|
|
|
}); |
|
|
|
|
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Install site event listeners for Control Panel requests only |
395
|
|
|
*/ |
|
|
|
|
396
|
|
|
protected function installCpEventListeners(): void |
397
|
|
|
{ |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Return the custom frontend routes |
402
|
|
|
* |
403
|
|
|
* @return array<string, string> |
404
|
|
|
*/ |
405
|
|
|
protected function customFrontendRoutes(): array |
406
|
|
|
{ |
407
|
|
|
return [ |
408
|
|
|
'instantanalytics/pageViewTrack' => |
409
|
|
|
'instant-analytics-ga4/track/track-page-view-url', |
410
|
|
|
'instantanalytics/eventTrack/<filename:[-\w\.*]+>?' => |
411
|
|
|
'instant-analytics-ga4/track/track-event-url', |
412
|
|
|
]; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
|
|
|
|
416
|
|
|
* @inheritdoc |
417
|
|
|
*/ |
|
|
|
|
418
|
|
|
protected function createSettingsModel(): ?Model |
419
|
|
|
{ |
420
|
|
|
return new Settings(); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
// Private Methods |
424
|
|
|
// ========================================================================= |
425
|
|
|
|
426
|
|
|
/** |
|
|
|
|
427
|
|
|
* @param $layoutId |
|
|
|
|
428
|
|
|
* |
429
|
|
|
* @return array|array<string, string> |
430
|
|
|
*/ |
431
|
|
|
private function getPullFieldsFromLayoutId($layoutId): array |
|
|
|
|
432
|
|
|
{ |
433
|
|
|
$result = ['' => 'none']; |
434
|
|
|
if ($layoutId === null) { |
435
|
|
|
return $result; |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
$fieldLayout = Craft::$app->getFields()->getLayoutById($layoutId); |
439
|
|
|
if ($fieldLayout) { |
440
|
|
|
$result = FieldHelper::fieldsOfTypeFromLayout(FieldHelper::TEXT_FIELD_CLASS_KEY, $fieldLayout, false); |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
return $result; |
444
|
|
|
} |
445
|
|
|
} |
446
|
|
|
|