|
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\services\Commerce as CommerceService; |
|
15
|
|
|
use nystudio107\instantanalytics\services\IA as IAService; |
|
16
|
|
|
use nystudio107\instantanalytics\variables\InstantAnalyticsVariable; |
|
17
|
|
|
use nystudio107\instantanalytics\twigextensions\InstantAnalyticsTwigExtension; |
|
18
|
|
|
use nystudio107\instantanalytics\models\Settings; |
|
19
|
|
|
|
|
20
|
|
|
use Craft; |
|
21
|
|
|
use craft\base\Plugin; |
|
22
|
|
|
use craft\events\PluginEvent; |
|
23
|
|
|
use craft\events\RegisterUrlRulesEvent; |
|
24
|
|
|
use craft\events\TemplateEvent; |
|
25
|
|
|
use craft\helpers\UrlHelper; |
|
26
|
|
|
use craft\services\Plugins; |
|
27
|
|
|
use craft\web\twig\variables\CraftVariable; |
|
28
|
|
|
use craft\web\UrlManager; |
|
29
|
|
|
use craft\web\View; |
|
30
|
|
|
|
|
31
|
|
|
use yii\base\Event; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @author nystudio107 |
|
35
|
|
|
* @package InstantAnalytics |
|
36
|
|
|
* @since 1.0.0 |
|
37
|
|
|
* |
|
38
|
|
|
* @property IAService $ia |
|
39
|
|
|
* @property CommerceService $commerce |
|
40
|
|
|
*/ |
|
41
|
|
|
class InstantAnalytics extends Plugin |
|
42
|
|
|
{ |
|
43
|
|
|
// Static Properties |
|
44
|
|
|
// ========================================================================= |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var InstantAnalytics |
|
48
|
|
|
*/ |
|
49
|
|
|
public static $plugin; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var Plugin|null |
|
53
|
|
|
*/ |
|
54
|
|
|
public static $commercePlugin; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var Plugin|null |
|
58
|
|
|
*/ |
|
59
|
|
|
public static $seomaticPlugin; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
public static $currentTemplate = ''; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var bool |
|
68
|
|
|
*/ |
|
69
|
|
|
public static $pageViewSent = false; |
|
70
|
|
|
|
|
71
|
|
|
// Public Methods |
|
72
|
|
|
// ========================================================================= |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @inheritdoc |
|
76
|
|
|
*/ |
|
77
|
|
|
public function init() |
|
78
|
|
|
{ |
|
79
|
|
|
parent::init(); |
|
80
|
|
|
self::$plugin = $this; |
|
81
|
|
|
$view = Craft::$app->getView(); |
|
82
|
|
|
$request = Craft::$app->getRequest(); |
|
83
|
|
|
// Add in our Twig extensions |
|
84
|
|
|
$view->registerTwigExtension(new InstantAnalyticsTwigExtension()); |
|
85
|
|
|
// Install our template hook |
|
86
|
|
|
$view->hook('iaSendPageView', [$this, 'iaSendPageView']); |
|
87
|
|
|
// Determine if Craft Commerce is installed & enabled |
|
88
|
|
|
self::$commercePlugin = Craft::$app->getPlugins()->getPlugin('commerce'); |
|
89
|
|
|
// Determine if SEOmatic is installed & enabled |
|
90
|
|
|
self::$seomaticPlugin = Craft::$app->getPlugins()->getPlugin('seomatic'); |
|
91
|
|
|
// Register our variables |
|
92
|
|
|
Event::on( |
|
93
|
|
|
CraftVariable::class, |
|
94
|
|
|
CraftVariable::EVENT_INIT, |
|
95
|
|
|
function (Event $event) { |
|
96
|
|
|
/** @var CraftVariable $variable */ |
|
97
|
|
|
$variable = $event->sender; |
|
98
|
|
|
$variable->set('instantAnalytics', InstantAnalyticsVariable::class); |
|
99
|
|
|
} |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
// Do something after we're installed |
|
103
|
|
|
Event::on( |
|
104
|
|
|
Plugins::class, |
|
105
|
|
|
Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
|
106
|
|
|
function (PluginEvent $event) { |
|
107
|
|
|
if ($event->plugin === $this) { |
|
108
|
|
|
$request = Craft::$app->getRequest(); |
|
109
|
|
|
if (($request->isCpRequest) && (!$request->isConsoleRequest)) { |
|
110
|
|
|
Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('instant-analytics/welcome'))->send(); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
// We're only interested in site requests that are not console requests |
|
117
|
|
|
if (($request->isSiteRequest) && (!$request->isConsoleRequest)) { |
|
118
|
|
|
// Remember the name of the currently rendering template |
|
119
|
|
|
Event::on( |
|
120
|
|
|
View::class, |
|
121
|
|
|
View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, |
|
122
|
|
|
function (TemplateEvent $event) { |
|
123
|
|
|
self::$currentTemplate = $event->template; |
|
124
|
|
|
} |
|
125
|
|
|
); |
|
126
|
|
|
// Remember the name of the currently rendering template |
|
127
|
|
|
Event::on( |
|
128
|
|
|
View::class, |
|
129
|
|
|
View::EVENT_AFTER_RENDER_PAGE_TEMPLATE, |
|
130
|
|
|
function (TemplateEvent $event) { |
|
131
|
|
|
$settings = InstantAnalytics::$plugin->getSettings(); |
|
132
|
|
|
if ($settings->autoSendPageView) { |
|
133
|
|
|
$this->sendPageView(); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
); |
|
137
|
|
|
// Register our site routes |
|
138
|
|
|
Event::on( |
|
139
|
|
|
UrlManager::class, |
|
140
|
|
|
UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
|
141
|
|
|
function (RegisterUrlRulesEvent $event) { |
|
142
|
|
|
$event->rules['instantanalytics/pageViewTrack/<filename:[-\w\.*]+>?'] = |
|
143
|
|
|
'instant-analytics/track/track-page-view-url'; |
|
144
|
|
|
$event->rules['instantanalytics/eventTrack/<filename:[-\w\.*]+>?'] = |
|
145
|
|
|
'instant-analytics/track/track-event-url'; |
|
146
|
|
|
} |
|
147
|
|
|
); |
|
148
|
|
|
// Commerce-specific hooks |
|
149
|
|
|
if (self::$commercePlugin) { |
|
150
|
|
|
// TODO: pending Commerce for Craft 3 |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
Craft::info( |
|
155
|
|
|
Craft::t( |
|
156
|
|
|
'instant-analytics', |
|
157
|
|
|
'{name} plugin loaded', |
|
158
|
|
|
['name' => $this->name] |
|
159
|
|
|
), |
|
160
|
|
|
__METHOD__ |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @inheritdoc |
|
166
|
|
|
*/ |
|
167
|
|
|
public function settingsHtml() |
|
168
|
|
|
{ |
|
169
|
|
|
$commerceFields = []; |
|
170
|
|
|
|
|
171
|
|
|
if (self::$commercePlugin) { |
|
172
|
|
|
/** |
|
173
|
|
|
* TODO: pending Commerce for Craft 3 |
|
174
|
|
|
$productTypes = craft()->commerce_productTypes->getAllProductTypes(); |
|
175
|
|
|
foreach ($productTypes as $productType) { |
|
176
|
|
|
$productFields = $this->_getPullFieldsFromLayoutId($productType->fieldLayoutId); |
|
177
|
|
|
$commerceFields = array_merge($commerceFields, $productFields); |
|
178
|
|
|
if ($productType->hasVariants) { |
|
179
|
|
|
$variantFields = $this->_getPullFieldsFromLayoutId($productType->variantFieldLayoutId); |
|
180
|
|
|
$commerceFields = array_merge($commerceFields, $variantFields); |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
*/ |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
// Render the settings template |
|
188
|
|
|
return Craft::$app->getView()->renderTemplate( |
|
189
|
|
|
'instant-analytics/settings', |
|
190
|
|
|
[ |
|
191
|
|
|
'settings' => $this->getSettings(), |
|
192
|
|
|
'commerceFields' => $commerceFields, |
|
193
|
|
|
] |
|
194
|
|
|
); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
// Protected Methods |
|
198
|
|
|
// ========================================================================= |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @inheritdoc |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function createSettingsModel() |
|
204
|
|
|
{ |
|
205
|
|
|
return new Settings(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param int $layoutId |
|
210
|
|
|
* |
|
211
|
|
|
* @return array |
|
212
|
|
|
*/ |
|
213
|
|
|
private function getPullFieldsFromLayoutId(int $layoutId) |
|
214
|
|
|
{ |
|
215
|
|
|
$result = ['' => "none"]; |
|
216
|
|
|
$fieldLayout = Craft::$app->getFields()->getLayoutById($layoutId); |
|
217
|
|
|
$fieldLayoutFields = $fieldLayout->getFields(); |
|
218
|
|
|
foreach ($fieldLayoutFields as $fieldLayoutField) { |
|
219
|
|
|
$field = $fieldLayoutField->field; |
|
220
|
|
|
switch ($field->type) { |
|
221
|
|
|
case "PlainText": |
|
222
|
|
|
case "RichText": |
|
223
|
|
|
case "RedactorI": |
|
224
|
|
|
case "Categories": |
|
225
|
|
|
$result[$field->handle] = $field->name; |
|
226
|
|
|
break; |
|
227
|
|
|
|
|
228
|
|
|
case "Tags": |
|
229
|
|
|
break; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
return $result; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
// Private Methods |
|
237
|
|
|
// ========================================================================= |
|
238
|
|
|
|
|
239
|
|
|
private function sendPageView() |
|
240
|
|
|
{ |
|
241
|
|
|
if (!self::$pageViewSent) { |
|
242
|
|
|
self::$pageViewSent = true; |
|
243
|
|
|
/** @var IAnalytics $analytics */ |
|
244
|
|
|
$analytics = InstantAnalytics::$plugin->ia->getGlobals(self::$currentTemplate); |
|
245
|
|
|
// Send the page view |
|
246
|
|
|
if ($analytics) { |
|
247
|
|
|
$response = $analytics->sendPageView(); |
|
248
|
|
|
Craft::info( |
|
249
|
|
|
"pageView sent, response: " . print_r($response, true), |
|
250
|
|
|
__METHOD__ |
|
251
|
|
|
); |
|
252
|
|
|
} else { |
|
253
|
|
|
Craft::error( |
|
254
|
|
|
"Analytics not sent because googleAnalyticsTracking is not set", |
|
255
|
|
|
__METHOD__ |
|
256
|
|
|
); |
|
257
|
|
|
} |
|
258
|
|
|
} |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Send a page view with the pre-loaded IAnalytics object |
|
263
|
|
|
* |
|
264
|
|
|
* @param array &$context |
|
265
|
|
|
* |
|
266
|
|
|
* @return string|null |
|
267
|
|
|
*/ |
|
268
|
|
|
private function iaSendPageView(array &$context) |
|
269
|
|
|
{ |
|
270
|
|
|
$request = Craft::$app->getRequest(); |
|
271
|
|
|
if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) { |
|
272
|
|
|
// If SEOmatic is installed, set the page title from it |
|
273
|
|
|
if (self::$seomaticPlugin && isset($context['seomaticMeta'])) { |
|
274
|
|
|
/** |
|
275
|
|
|
* TODO: fix for SEOmatic |
|
276
|
|
|
$seomaticMeta = $context['seomaticMeta']; |
|
277
|
|
|
$analytics->setDocumentTitle($seomaticMeta['seoTitle']); |
|
278
|
|
|
*/ |
|
279
|
|
|
} |
|
280
|
|
|
$this->sendPageView(); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
return ''; |
|
284
|
|
|
} |
|
285
|
|
|
} |
|
286
|
|
|
|