1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Instant Analytics plugin for Craft CMS |
4
|
|
|
* |
5
|
|
|
* @author nystudio107 |
|
|
|
|
6
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
7
|
|
|
* @link http://nystudio107.com |
|
|
|
|
8
|
|
|
* @package InstantAnalytics |
|
|
|
|
9
|
|
|
* @since 1.0.0 |
10
|
|
|
*/ |
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace nystudio107\instantanalyticsGa4\ga4; |
13
|
|
|
|
14
|
|
|
use Br33f\Ga4\MeasurementProtocol\Dto\Event\AbstractEvent; |
15
|
|
|
use Br33f\Ga4\MeasurementProtocol\Dto\Request\BaseRequest; |
16
|
|
|
use Br33f\Ga4\MeasurementProtocol\Dto\Response\BaseResponse; |
17
|
|
|
use Br33f\Ga4\MeasurementProtocol\HttpClient; |
18
|
|
|
use Craft; |
19
|
|
|
use craft\commerce\elements\Product; |
|
|
|
|
20
|
|
|
use craft\commerce\elements\Variant; |
|
|
|
|
21
|
|
|
use craft\errors\MissingComponentException; |
22
|
|
|
use craft\helpers\App; |
23
|
|
|
use nystudio107\instantanalyticsGa4\helpers\Analytics as AnalyticsHelper; |
24
|
|
|
use nystudio107\instantanalyticsGa4\InstantAnalytics; |
25
|
|
|
use nystudio107\seomatic\Seomatic; |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
|
|
|
|
28
|
|
|
* @author nystudio107 |
|
|
|
|
29
|
|
|
* @package InstantAnalytics |
|
|
|
|
30
|
|
|
* @since 1.2.0 |
|
|
|
|
31
|
|
|
* |
32
|
|
|
* @method Analytics setAllowGoogleSignals(string $value) |
33
|
|
|
* @method Analytics setAllowAdPersonalizationSignals(string $value) |
34
|
|
|
* @method Analytics setCampaignContent(string $value) |
35
|
|
|
* @method Analytics setCampaignId(string $value) |
36
|
|
|
* @method Analytics setCampaignMedium(string $value) |
37
|
|
|
* @method Analytics setCampaignName(string $value) |
38
|
|
|
* @method Analytics setCampaignSource(string $value) |
39
|
|
|
* @method Analytics setCampaignTerm(string $value) |
40
|
|
|
* @method Analytics setCampaign(string $value) |
41
|
|
|
* @method Analytics setClientId(string $value) |
42
|
|
|
* @method Analytics setContentGroup(string $value) |
43
|
|
|
* @method Analytics setCookieDomain(string $value) |
44
|
|
|
* @method Analytics setCookieExpires(string $value) |
45
|
|
|
* @method Analytics setCookieFlags(string $value) |
46
|
|
|
* @method Analytics setCookiePath(string $value) |
47
|
|
|
* @method Analytics setCookiePrefix(string $value) |
48
|
|
|
* @method Analytics setCookieUpdate(string $value) |
49
|
|
|
* @method Analytics setLanguage(string $value) |
50
|
|
|
* @method Analytics setPageLocation(string $value) |
51
|
|
|
* @method Analytics setPageReferrer(string $value) |
52
|
|
|
* @method Analytics setPageTitle(string $value) |
53
|
|
|
* @method Analytics setSendPageView(string $value) |
54
|
|
|
* @method Analytics setScreenResolution(string $value) |
55
|
|
|
* @method Analytics setUserId(string $value) |
56
|
|
|
*/ |
|
|
|
|
57
|
|
|
class Analytics |
58
|
|
|
{ |
59
|
|
|
/** |
|
|
|
|
60
|
|
|
* @var BaseRequest|null |
61
|
|
|
*/ |
62
|
|
|
private ?BaseRequest $_request = null; |
63
|
|
|
|
64
|
|
|
/** |
|
|
|
|
65
|
|
|
* @var Service|null|false |
66
|
|
|
*/ |
67
|
|
|
private mixed $_service = null; |
68
|
|
|
|
69
|
|
|
/** |
|
|
|
|
70
|
|
|
* @var string|null |
71
|
|
|
*/ |
72
|
|
|
private ?string $_affiliation = null; |
73
|
|
|
|
74
|
|
|
private ?bool $_shouldSendAnalytics = null; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Component factory for creating events. |
78
|
|
|
* |
79
|
|
|
* @return ComponentFactory |
80
|
|
|
*/ |
81
|
|
|
public function create(): ComponentFactory |
82
|
|
|
{ |
83
|
|
|
return new ComponentFactory(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Add an event to be sent to Google |
88
|
|
|
* |
89
|
|
|
* @param AbstractEvent $event |
|
|
|
|
90
|
|
|
* @return BaseRequest |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
public function addEvent(AbstractEvent $event): BaseRequest |
93
|
|
|
{ |
94
|
|
|
return $this->request()->addEvent($event); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Send the events collected so far. |
99
|
|
|
* |
100
|
|
|
* @return BaseResponse|null |
101
|
|
|
* @throws \Br33f\Ga4\MeasurementProtocol\Exception\HydrationException |
102
|
|
|
* @throws \Br33f\Ga4\MeasurementProtocol\Exception\ValidationException |
103
|
|
|
*/ |
104
|
|
|
public function sendCollectedEvents(): ?BaseResponse |
105
|
|
|
{ |
106
|
|
|
if ($this->_shouldSendAnalytics === null) { |
107
|
|
|
$this->_shouldSendAnalytics = AnalyticsHelper::shouldSendAnalytics(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (!$this->_shouldSendAnalytics) { |
|
|
|
|
111
|
|
|
return null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$service = $this->service(); |
115
|
|
|
|
116
|
|
|
if (!$service) { |
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$request = $this->request(); |
121
|
|
|
$eventCount = count($request->getEvents()->getEventList()); |
122
|
|
|
|
123
|
|
|
if (!InstantAnalytics::$settings->sendAnalyticsData) { |
124
|
|
|
InstantAnalytics::$plugin->logAnalyticsEvent( |
|
|
|
|
125
|
|
|
'Analytics not enabled - skipped sending {count} events', |
126
|
|
|
['count' => $eventCount], |
127
|
|
|
__METHOD__ |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($eventCount === 0) { |
134
|
|
|
InstantAnalytics::$plugin->logAnalyticsEvent( |
135
|
|
|
'No events collected to send', |
136
|
|
|
[], |
137
|
|
|
__METHOD__ |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
return null; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
InstantAnalytics::$plugin->logAnalyticsEvent( |
144
|
|
|
'Sending {count} analytics events', |
145
|
|
|
['count' => $eventCount], |
146
|
|
|
__METHOD__ |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
$response = $service->send($request); |
150
|
|
|
|
151
|
|
|
// Clear events already sent from the list. |
152
|
|
|
$request->getEvents()->setEventList([]); |
153
|
|
|
|
154
|
|
|
return $response; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Set affiliation for all the events that incorporate Commerce Product info for the remaining duration of request. |
159
|
|
|
* |
160
|
|
|
* @param string $affiliation |
|
|
|
|
161
|
|
|
* @return $this |
|
|
|
|
162
|
|
|
*/ |
163
|
|
|
public function setAffiliation(string $affiliation): self |
164
|
|
|
{ |
165
|
|
|
$this->_affiliation = $affiliation; |
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
public function getAffiliation(): ?string |
|
|
|
|
170
|
|
|
{ |
171
|
|
|
return $this->_affiliation; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Add a commerce item list impression. |
176
|
|
|
* |
177
|
|
|
* @param Product|Variant $productVariant |
|
|
|
|
178
|
|
|
* @param $index |
|
|
|
|
179
|
|
|
* @param string $listName |
|
|
|
|
180
|
|
|
* @throws \yii\base\InvalidConfigException |
|
|
|
|
181
|
|
|
*/ |
|
|
|
|
182
|
|
|
public function addCommerceProductImpression(Product|Variant $productVariant, $index, string $listName = 'default') { |
|
|
|
|
183
|
|
|
InstantAnalytics::$plugin->commerce->addCommerceProductImpression($productVariant, $index, $listName); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Add a commerce item list impression. |
188
|
|
|
* |
189
|
|
|
* @param Product|Variant $productVariant |
|
|
|
|
190
|
|
|
* @param $index |
|
|
|
|
191
|
|
|
* @param string $listName |
|
|
|
|
192
|
|
|
* @deprecated `Analytics::addCommerceProductDetailView()` is deprecated. Use `Analytics::addCommerceProductImpression()` instead. |
|
|
|
|
193
|
|
|
* @throws \yii\base\InvalidConfigException |
|
|
|
|
194
|
|
|
*/ |
|
|
|
|
195
|
|
|
public function addCommerceProductDetailView(Product|Variant $productVariant, $index, string $listName = 'default') { |
|
|
|
|
196
|
|
|
Craft::$app->getDeprecator()->log('Analytics::addCommerceProductDetailView()', '`Analytics::addCommerceProductDetailView()` is deprecated. Use `Analytics::addCommerceProductImpression()` instead.'); |
197
|
|
|
InstantAnalytics::$plugin->commerce->addCommerceProductImpression($productVariant, $index, $listName); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Add a commerce product list impression. |
202
|
|
|
* |
203
|
|
|
* @param array $products |
|
|
|
|
204
|
|
|
* @param $listName |
|
|
|
|
205
|
|
|
*/ |
|
|
|
|
206
|
|
|
public function addCommerceProductListImpression(array $products, $listName) { |
|
|
|
|
207
|
|
|
InstantAnalytics::$plugin->commerce->addCommerceProductListImpression($products, $listName); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Set the measurement id. |
212
|
|
|
* |
213
|
|
|
* @param string $measurementId |
|
|
|
|
214
|
|
|
* @return $this |
|
|
|
|
215
|
|
|
*/ |
216
|
|
|
public function setMeasurementId(string $measurementId): self |
217
|
|
|
{ |
218
|
|
|
$this->service()?->setMeasurementId($measurementId); |
219
|
|
|
return $this; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Set the API secret. |
224
|
|
|
* |
225
|
|
|
* @param string $apiSecret |
|
|
|
|
226
|
|
|
* @return $this |
|
|
|
|
227
|
|
|
*/ |
228
|
|
|
public function setApiSecret(string $apiSecret): self |
229
|
|
|
{ |
230
|
|
|
$this->service()?->setApiSecret($apiSecret); |
231
|
|
|
return $this; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
public function __call(string $methodName, array $arguments): ?self |
|
|
|
|
235
|
|
|
{ |
236
|
|
|
$knownProperties = [ |
237
|
|
|
'allowGoogleSignals' => 'allow_google_signals', |
238
|
|
|
'allowAdPersonalizationSignals' => 'allow_ad_personalization_signals', |
239
|
|
|
'campaignContent' => 'campaign_content', |
240
|
|
|
'campaignId' => 'campaign_id', |
241
|
|
|
'campaignMedium' => 'campaign_medium', |
242
|
|
|
'campaignName' => 'campaign_name', |
243
|
|
|
'campaignSource' => 'campaign_source', |
244
|
|
|
'campaignTerm' => 'campaign_term', |
245
|
|
|
'campaign' => 'campaign', |
246
|
|
|
'clientId' => 'client_id', |
247
|
|
|
'contentGroup' => 'content_group', |
248
|
|
|
'cookieDomain' => 'cookie_domain', |
249
|
|
|
'cookieExpires' => 'cookie_expires', |
250
|
|
|
'cookieFlags' => 'cookie_flags', |
251
|
|
|
'cookiePath' => 'cookie_path', |
252
|
|
|
'cookiePrefix' => 'cookie_prefix', |
253
|
|
|
'cookieUpdate' => 'cookie_update', |
254
|
|
|
'language' => 'language', |
255
|
|
|
'pageLocation' => 'page_location', |
256
|
|
|
'pageReferrer' => 'page_referrer', |
257
|
|
|
'pageTitle' => 'page_title', |
258
|
|
|
'sendPageView' => 'send_page_view', |
259
|
|
|
'screenResolution' => 'screen_resolution', |
260
|
|
|
'userId' => 'user_id' |
261
|
|
|
]; |
262
|
|
|
|
263
|
|
|
if (str_starts_with($methodName, 'set')) { |
264
|
|
|
$methodName = lcfirst(substr($methodName, 3)); |
265
|
|
|
|
266
|
|
|
$service = $this->service(); |
267
|
|
|
if ($service && !empty($knownProperties[$methodName])) { |
268
|
|
|
$service->setAdditionalQueryParam($knownProperties[$methodName], $arguments[0]); |
269
|
|
|
|
270
|
|
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return null; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
protected function request(): BaseRequest |
|
|
|
|
279
|
|
|
{ |
280
|
|
|
if ($this->_request === null) { |
281
|
|
|
$this->_request = new BaseRequest(); |
282
|
|
|
$this->_request->setClientId(AnalyticsHelper::getClientId()); |
|
|
|
|
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
return $this->_request; |
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Init the service used to send events |
290
|
|
|
*/ |
|
|
|
|
291
|
|
|
public function init(): void |
292
|
|
|
{ |
293
|
|
|
$this->service(); |
294
|
|
|
$this->request(); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
protected function service(): ?Service |
|
|
|
|
298
|
|
|
{ |
299
|
|
|
if ($this->_service === null) { |
300
|
|
|
$settings = InstantAnalytics::$settings; |
301
|
|
|
$apiSecret = App::parseEnv($settings->googleAnalyticsMeasurementApiSecret); |
302
|
|
|
$measurementId = App::parseEnv($settings->googleAnalyticsMeasurementId); |
303
|
|
|
|
304
|
|
|
if (empty($apiSecret) || empty($measurementId)) { |
305
|
|
|
InstantAnalytics::$plugin->logAnalyticsEvent( |
306
|
|
|
'API secret or measurement ID not set up for Instant Analytics', |
307
|
|
|
[], |
308
|
|
|
__METHOD__ |
309
|
|
|
); |
310
|
|
|
$this->_service = false; |
311
|
|
|
|
312
|
|
|
return null; |
313
|
|
|
} |
314
|
|
|
$this->_service = new Service($apiSecret, $measurementId); |
315
|
|
|
|
316
|
|
|
$ga4Client = new HttpClient(); |
317
|
|
|
$ga4Client->setClient(Craft::createGuzzleClient()); |
318
|
|
|
$this->_service->setHttpClient($ga4Client); |
319
|
|
|
|
320
|
|
|
$request = Craft::$app->getRequest(); |
321
|
|
|
try { |
322
|
|
|
$session = Craft::$app->getSession(); |
323
|
|
|
} catch (MissingComponentException $exception) { |
324
|
|
|
$session = null; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
$this->setPageReferrer($request->getReferrer()); |
328
|
|
|
|
329
|
|
|
// Load any campaign values from session or request |
330
|
|
|
$campaignParams = [ |
331
|
|
|
'utm_source' => 'CampaignSource', |
332
|
|
|
'utm_medium' => 'CampaignMedium', |
333
|
|
|
'utm_campaign' => 'CampaignName', |
334
|
|
|
'utm_content' => 'CampaignContent', |
335
|
|
|
'utm_term' => 'CampaignTerm', |
336
|
|
|
]; |
337
|
|
|
|
338
|
|
|
// Load them up for GA4 |
339
|
|
|
foreach ($campaignParams as $key => $method) { |
340
|
|
|
$value = $request->getParam($key) ?? $session->get($key) ?? null; |
341
|
|
|
$method = 'set' . $method; |
342
|
|
|
|
343
|
|
|
$this->$method($value); |
344
|
|
|
|
345
|
|
|
if ($session && $value) { |
346
|
|
|
$session->set($key, $value); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
// If SEOmatic is installed, set the affiliation as well |
352
|
|
|
if (InstantAnalytics::$seomaticPlugin && Seomatic::$settings->renderEnabled && Seomatic::$plugin->metaContainers->metaSiteVars !== null) { |
353
|
|
|
$siteName = Seomatic::$plugin->metaContainers->metaSiteVars->siteName; |
354
|
|
|
$this->setAffiliation($siteName); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
if ($this->_service === false) { |
360
|
|
|
return null; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
return $this->_service; |
364
|
|
|
} |
365
|
|
|
} |
366
|
|
|
|