|
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; |
|
12
|
|
|
|
|
13
|
|
|
use nystudio107\instantanalytics\helpers\IAnalytics; |
|
14
|
|
|
use nystudio107\instantanalytics\helpers\Field as FieldHelper; |
|
15
|
|
|
use nystudio107\instantanalytics\models\Settings; |
|
16
|
|
|
use nystudio107\instantanalytics\services\Commerce as CommerceService; |
|
17
|
|
|
use nystudio107\instantanalytics\services\IA as IAService; |
|
18
|
|
|
use nystudio107\instantanalytics\variables\InstantAnalyticsVariable; |
|
19
|
|
|
use nystudio107\instantanalytics\twigextensions\InstantAnalyticsTwigExtension; |
|
20
|
|
|
|
|
21
|
|
|
use Craft; |
|
22
|
|
|
use craft\base\Plugin; |
|
23
|
|
|
use craft\events\PluginEvent; |
|
24
|
|
|
use craft\events\RegisterUrlRulesEvent; |
|
25
|
|
|
use craft\events\TemplateEvent; |
|
26
|
|
|
use craft\helpers\UrlHelper; |
|
27
|
|
|
use craft\services\Plugins; |
|
28
|
|
|
use craft\web\twig\variables\CraftVariable; |
|
29
|
|
|
use craft\web\UrlManager; |
|
30
|
|
|
use craft\web\View; |
|
31
|
|
|
|
|
32
|
|
|
use nystudio107\seomatic\Seomatic; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
use yii\base\Event; |
|
35
|
|
|
use yii\base\Exception; |
|
36
|
|
|
|
|
37
|
|
|
/** @noinspection MissingPropertyAnnotationsInspection */ |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
|
|
|
|
|
40
|
|
|
* @author nystudio107 |
|
|
|
|
|
|
41
|
|
|
* @package InstantAnalytics |
|
|
|
|
|
|
42
|
|
|
* @since 1.0.0 |
|
|
|
|
|
|
43
|
|
|
* |
|
44
|
|
|
* @property IAService $ia |
|
|
|
|
|
|
45
|
|
|
* @property CommerceService $commerce |
|
|
|
|
|
|
46
|
|
|
*/ |
|
|
|
|
|
|
47
|
|
|
class InstantAnalytics extends Plugin |
|
48
|
|
|
{ |
|
49
|
|
|
// Constants |
|
50
|
|
|
// ========================================================================= |
|
51
|
|
|
|
|
52
|
|
|
const COMMERCE_PLUGIN_HANDLE = 'commerce'; |
|
53
|
|
|
const SEOMATIC_PLUGIN_HANDLE = 'seomatic'; |
|
54
|
|
|
|
|
55
|
|
|
// Static Properties |
|
56
|
|
|
// ========================================================================= |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
|
|
|
|
|
59
|
|
|
* @var InstantAnalytics |
|
60
|
|
|
*/ |
|
61
|
|
|
public static $plugin; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
|
|
|
|
|
64
|
|
|
* @var Plugin|null |
|
65
|
|
|
*/ |
|
66
|
|
|
public static $commercePlugin; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
|
|
|
|
|
69
|
|
|
* @var Plugin|null |
|
70
|
|
|
*/ |
|
71
|
|
|
public static $seomaticPlugin; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
|
|
|
|
|
74
|
|
|
* @var string |
|
75
|
|
|
*/ |
|
76
|
|
|
public static $currentTemplate = ''; |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
|
|
|
|
|
79
|
|
|
* @var bool |
|
80
|
|
|
*/ |
|
81
|
|
|
public static $pageViewSent = false; |
|
82
|
|
|
|
|
83
|
|
|
// Public Methods |
|
84
|
|
|
// ========================================================================= |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
|
|
|
|
|
87
|
|
|
* @inheritdoc |
|
88
|
|
|
*/ |
|
|
|
|
|
|
89
|
|
|
public function init() |
|
90
|
|
|
{ |
|
91
|
|
|
parent::init(); |
|
92
|
|
|
self::$plugin = $this; |
|
93
|
|
|
|
|
94
|
|
|
// Determine if Craft Commerce is installed & enabled |
|
95
|
|
|
self::$commercePlugin = Craft::$app->getPlugins()->getPlugin(self::COMMERCE_PLUGIN_HANDLE); |
|
96
|
|
|
// Determine if SEOmatic is installed & enabled |
|
97
|
|
|
self::$seomaticPlugin = Craft::$app->getPlugins()->getPlugin(self::SEOMATIC_PLUGIN_HANDLE); |
|
98
|
|
|
// Add in our Craft components |
|
99
|
|
|
$this->addComponents(); |
|
100
|
|
|
// Install our global event handlers |
|
101
|
|
|
$this->installEventListeners(); |
|
102
|
|
|
|
|
103
|
|
|
Craft::info( |
|
104
|
|
|
Craft::t( |
|
105
|
|
|
'instant-analytics', |
|
106
|
|
|
'{name} plugin loaded', |
|
107
|
|
|
['name' => $this->name] |
|
108
|
|
|
), |
|
109
|
|
|
__METHOD__ |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
|
|
|
|
|
114
|
|
|
* @inheritdoc |
|
115
|
|
|
*/ |
|
|
|
|
|
|
116
|
|
|
public function settingsHtml() |
|
117
|
|
|
{ |
|
118
|
|
|
$commerceFields = []; |
|
119
|
|
|
|
|
120
|
|
|
if (self::$commercePlugin) { |
|
121
|
|
|
/** |
|
122
|
|
|
* TODO: pending Commerce for Craft 3 |
|
123
|
|
|
* $productTypes = craft()->commerce_productTypes->getAllProductTypes(); |
|
124
|
|
|
* foreach ($productTypes as $productType) { |
|
125
|
|
|
* $productFields = $this->_getPullFieldsFromLayoutId($productType->fieldLayoutId); |
|
126
|
|
|
* $commerceFields = array_merge($commerceFields, $productFields); |
|
127
|
|
|
* if ($productType->hasVariants) { |
|
128
|
|
|
* $variantFields = $this->_getPullFieldsFromLayoutId($productType->variantFieldLayoutId); |
|
129
|
|
|
* $commerceFields = array_merge($commerceFields, $variantFields); |
|
130
|
|
|
* } |
|
131
|
|
|
* } |
|
132
|
|
|
*/ |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// Rend the settings template |
|
136
|
|
|
try { |
|
137
|
|
|
return Craft::$app->getView()->renderTemplate( |
|
138
|
|
|
'instant-analytics/settings', |
|
139
|
|
|
[ |
|
140
|
|
|
'settings' => $this->getSettings(), |
|
141
|
|
|
'commerceFields' => $commerceFields, |
|
142
|
|
|
] |
|
143
|
|
|
); |
|
144
|
|
|
} catch (\Twig_Error_Loader $e) { |
|
145
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
146
|
|
|
} catch (Exception $e) { |
|
147
|
|
|
Craft::error($e->getMessage(), __METHOD__); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return ''; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// Protected Methods |
|
154
|
|
|
// ========================================================================= |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Add in our Craft components |
|
159
|
|
|
*/ |
|
|
|
|
|
|
160
|
|
|
protected function addComponents() |
|
161
|
|
|
{ |
|
162
|
|
|
$view = Craft::$app->getView(); |
|
163
|
|
|
// Add in our Twig extensions |
|
164
|
|
|
$view->registerTwigExtension(new InstantAnalyticsTwigExtension()); |
|
165
|
|
|
// Install our template hook |
|
166
|
|
|
$view->hook('iaSendPageView', [$this, 'iaSendPageView']); |
|
167
|
|
|
// Register our variables |
|
168
|
|
|
Event::on( |
|
169
|
|
|
CraftVariable::class, |
|
170
|
|
|
CraftVariable::EVENT_INIT, |
|
171
|
|
|
function (Event $event) { |
|
172
|
|
|
/** @var CraftVariable $variable */ |
|
|
|
|
|
|
173
|
|
|
$variable = $event->sender; |
|
174
|
|
|
$variable->set('instantAnalytics', InstantAnalyticsVariable::class); |
|
175
|
|
|
} |
|
176
|
|
|
); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Install our event listeners |
|
181
|
|
|
*/ |
|
|
|
|
|
|
182
|
|
|
protected function installEventListeners() |
|
183
|
|
|
{ |
|
184
|
|
|
// Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN |
|
185
|
|
|
Event::on( |
|
186
|
|
|
Plugins::class, |
|
187
|
|
|
Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
|
188
|
|
|
function (PluginEvent $event) { |
|
189
|
|
|
if ($event->plugin === $this) { |
|
190
|
|
|
$request = Craft::$app->getRequest(); |
|
191
|
|
|
if ($request->isCpRequest) { |
|
192
|
|
|
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('instant-analytics/welcome'))->send(); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
); |
|
197
|
|
|
$request = Craft::$app->getRequest(); |
|
198
|
|
|
// Install only for non-console site requests |
|
199
|
|
|
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
|
200
|
|
|
$this->installSiteEventListeners(); |
|
201
|
|
|
} |
|
202
|
|
|
// Install only for non-console AdminCP requests |
|
203
|
|
|
if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) { |
|
204
|
|
|
$this->installCpEventListeners(); |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Install site event listeners for site requests only |
|
210
|
|
|
*/ |
|
|
|
|
|
|
211
|
|
|
protected function installSiteEventListeners() |
|
212
|
|
|
{ |
|
213
|
|
|
// Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES |
|
214
|
|
|
Event::on( |
|
215
|
|
|
UrlManager::class, |
|
216
|
|
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
|
217
|
|
|
function (RegisterUrlRulesEvent $event) { |
|
218
|
|
|
Craft::debug( |
|
219
|
|
|
'UrlManager::EVENT_REGISTER_SITE_URL_RULES', |
|
220
|
|
|
__METHOD__ |
|
221
|
|
|
); |
|
222
|
|
|
// Register our AdminCP routes |
|
223
|
|
|
$event->rules = array_merge( |
|
224
|
|
|
$event->rules, |
|
225
|
|
|
$this->customFrontendRoutes() |
|
226
|
|
|
); |
|
227
|
|
|
} |
|
228
|
|
|
); |
|
229
|
|
|
// Remember the name of the currently rendering template |
|
230
|
|
|
Event::on( |
|
231
|
|
|
View::class, |
|
232
|
|
|
View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, |
|
233
|
|
|
function (TemplateEvent $event) { |
|
234
|
|
|
self::$currentTemplate = $event->template; |
|
235
|
|
|
} |
|
236
|
|
|
); |
|
237
|
|
|
// Remember the name of the currently rendering template |
|
238
|
|
|
Event::on( |
|
239
|
|
|
View::class, |
|
240
|
|
|
View::EVENT_AFTER_RENDER_PAGE_TEMPLATE, |
|
241
|
|
|
function (TemplateEvent $event) { |
|
242
|
|
|
$settings = InstantAnalytics::$plugin->getSettings(); |
|
243
|
|
|
if ($settings->autoSendPageView) { |
|
244
|
|
|
$this->sendPageView(); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
); |
|
248
|
|
|
// Register our site routes |
|
249
|
|
|
Event::on( |
|
250
|
|
|
UrlManager::class, |
|
251
|
|
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
|
252
|
|
|
function (RegisterUrlRulesEvent $event) { |
|
253
|
|
|
$event->rules['instantanalytics/pageViewTrack/<filename:[-\w\.*]+>?'] = |
|
|
|
|
|
|
254
|
|
|
'instant-analytics/track/track-page-view-url'; |
|
255
|
|
|
$event->rules['instantanalytics/eventTrack/<filename:[-\w\.*]+>?'] = |
|
|
|
|
|
|
256
|
|
|
'instant-analytics/track/track-event-url'; |
|
257
|
|
|
} |
|
258
|
|
|
); |
|
259
|
|
|
// Commerce-specific hooks |
|
260
|
|
|
if (self::$commercePlugin) { |
|
261
|
|
|
// TODO: pending Commerce for Craft 3 |
|
262
|
|
|
} |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* Install site event listeners for AdminCP requests only |
|
267
|
|
|
*/ |
|
|
|
|
|
|
268
|
|
|
protected function installCpEventListeners() |
|
269
|
|
|
{ |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Return the custom frontend routes |
|
274
|
|
|
* |
|
275
|
|
|
* @return array |
|
276
|
|
|
*/ |
|
277
|
|
|
protected function customFrontendRoutes(): array |
|
278
|
|
|
{ |
|
279
|
|
|
return [ |
|
280
|
|
|
// Make webpack async bundle loading work out of published AssetBundles |
|
281
|
|
|
'/cpresources/instant-analytics/<resourceType:{handle}>/<fileName>' => 'instant-analytics/cp-nav/resource', |
|
282
|
|
|
]; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
|
|
|
|
|
286
|
|
|
* @inheritdoc |
|
287
|
|
|
*/ |
|
|
|
|
|
|
288
|
|
|
protected function createSettingsModel() |
|
289
|
|
|
{ |
|
290
|
|
|
return new Settings(); |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
// Private Methods |
|
294
|
|
|
// ========================================================================= |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* Send a page view with the pre-loaded IAnalytics object |
|
298
|
|
|
*/ |
|
|
|
|
|
|
299
|
|
|
private function sendPageView() |
|
|
|
|
|
|
300
|
|
|
{ |
|
301
|
|
|
$request = Craft::$app->getRequest(); |
|
302
|
|
|
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest() && !self::$pageViewSent) { |
|
303
|
|
|
self::$pageViewSent = true; |
|
304
|
|
|
/** @var IAnalytics $analytics */ |
|
|
|
|
|
|
305
|
|
|
$analytics = InstantAnalytics::$plugin->ia->getGlobals(self::$currentTemplate); |
|
306
|
|
|
// If SEOmatic is installed, set the page title from it |
|
307
|
|
|
$this->setTitleFromSeomatic($analytics); |
|
308
|
|
|
// Send the page view |
|
309
|
|
|
if ($analytics) { |
|
|
|
|
|
|
310
|
|
|
$response = $analytics->sendPageview(); |
|
311
|
|
|
Craft::info( |
|
312
|
|
|
Craft::t( |
|
313
|
|
|
'instant-analytics', |
|
314
|
|
|
'pageView sent, response:: {response}', |
|
315
|
|
|
[ |
|
316
|
|
|
'response' => print_r($response, true), |
|
317
|
|
|
] |
|
318
|
|
|
), |
|
319
|
|
|
__METHOD__ |
|
320
|
|
|
); |
|
321
|
|
|
} else { |
|
322
|
|
|
Craft::error( |
|
323
|
|
|
Craft::t( |
|
324
|
|
|
'instant-analytics', |
|
325
|
|
|
'Analytics not sent because googleAnalyticsTracking is not set' |
|
326
|
|
|
), |
|
327
|
|
|
__METHOD__ |
|
328
|
|
|
); |
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
|
|
|
|
|
334
|
|
|
* Handle the `{% hook isSendPageView %}` |
|
335
|
|
|
* |
|
336
|
|
|
* @param array &$context |
|
|
|
|
|
|
337
|
|
|
* |
|
338
|
|
|
* @return string|null |
|
339
|
|
|
*/ |
|
340
|
|
|
private function iaSendPageView(/** @noinspection PhpUnusedParameterInspection */ array &$context) |
|
|
|
|
|
|
341
|
|
|
{ |
|
342
|
|
|
$this->sendPageView(); |
|
343
|
|
|
|
|
344
|
|
|
return ''; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
|
|
|
|
|
348
|
|
|
* If SEOmatic is installed, set the page title from it |
|
349
|
|
|
* |
|
350
|
|
|
* @param $analytics |
|
|
|
|
|
|
351
|
|
|
*/ |
|
|
|
|
|
|
352
|
|
|
private function setTitleFromSeomatic(IAnalytics $analytics) |
|
|
|
|
|
|
353
|
|
|
{ |
|
354
|
|
|
if (self::$seomaticPlugin && Seomatic::$settings->renderEnabled) { |
|
355
|
|
|
$titleTag = Seomatic::$plugin->title->get('title'); |
|
356
|
|
|
if ($titleTag) { |
|
357
|
|
|
$titleArray = $titleTag->renderAttributes(); |
|
358
|
|
|
if (!empty($titleArray['title'])) { |
|
359
|
|
|
$analytics->setDocumentTitle($titleArray['title']); |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
} |
|
364
|
|
|
} |
|
365
|
|
|
|