Passed
Push — develop-v3 ( 2011c7...a56f50 )
by Andrew
09:00
created

Analytics::setMeasurementId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Instant Analytics plugin for Craft CMS
4
 *
5
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
6
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 2 should be the @author tag
Loading history...
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
7
 * @link      http://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @copyright tag
Loading history...
8
 * @package   InstantAnalytics
0 ignored issues
show
Coding Style introduced by
The tag in position 4 should be the @link tag
Loading history...
9
 * @since     1.0.0
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
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\Exception\HydrationException;
18
use Br33f\Ga4\MeasurementProtocol\Exception\ValidationException;
19
use Br33f\Ga4\MeasurementProtocol\HttpClient;
20
use Craft;
21
use craft\commerce\elements\Order;
0 ignored issues
show
Bug introduced by
The type craft\commerce\elements\Order was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use craft\commerce\elements\Product;
0 ignored issues
show
Bug introduced by
The type craft\commerce\elements\Product was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use craft\commerce\elements\Variant;
0 ignored issues
show
Bug introduced by
The type craft\commerce\elements\Variant was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use craft\errors\MissingComponentException;
25
use nystudio107\instantanalyticsGa4\helpers\Analytics as AnalyticsHelper;
26
use nystudio107\instantanalyticsGa4\InstantAnalytics;
27
use nystudio107\seomatic\Seomatic;
0 ignored issues
show
Bug introduced by
The type nystudio107\seomatic\Seomatic was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use yii\base\InvalidConfigException;
29
30
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
32
 * @package   InstantAnalytics
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
33
 * @since     1.2.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
34
 *
35
 * @method Analytics setAllowGoogleSignals(string $value)
36
 * @method Analytics setAllowAdPersonalizationSignals(string $value)
37
 * @method Analytics setCampaignContent(string $value)
38
 * @method Analytics setCampaignId(string $value)
39
 * @method Analytics setCampaignMedium(string $value)
40
 * @method Analytics setCampaignName(string $value)
41
 * @method Analytics setCampaignSource(string $value)
42
 * @method Analytics setCampaignTerm(string $value)
43
 * @method Analytics setCampaign(string $value)
44
 * @method Analytics setClientId(string $value)
45
 * @method Analytics setContentGroup(string $value)
46
 * @method Analytics setCookieDomain(string $value)
47
 * @method Analytics setCookieExpires(string $value)
48
 * @method Analytics setCookieFlags(string $value)
49
 * @method Analytics setCookiePath(string $value)
50
 * @method Analytics setCookiePrefix(string $value)
51
 * @method Analytics setCookieUpdate(string $value)
52
 * @method Analytics setLanguage(string $value)
53
 * @method Analytics setPageLocation(string $value)
54
 * @method Analytics setPageReferrer(string $value)
55
 * @method Analytics setPageTitle(string $value)
56
 * @method Analytics setSendPageView(string $value)
57
 * @method Analytics setScreenResolution(string $value)
58
 * @method Analytics setUserId(string $value)
59
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
60
class Analytics
61
{
62
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
63
     * @var BaseRequest|null
64
     */
65
    private $_request = null;
66
67
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
68
     * @var Service|null|false
69
     */
70
    private $_service = null;
71
72
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
73
     * @var string|null
74
     */
75
    private $_affiliation = null;
76
77
    private $_shouldSendAnalytics = null;
78
79
    /**
80
     * Component factory for creating events.
81
     *
82
     * @return ComponentFactory
83
     */
84
    public function create(): ComponentFactory
85
    {
86
        return new ComponentFactory();
87
    }
88
89
    /**
90
     * Add an event to be sent to Google
91
     *
92
     * @param AbstractEvent $event
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
93
     * @return BaseRequest
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
94
     */
95
    public function addEvent(AbstractEvent $event): BaseRequest
96
    {
97
        $clientId = $this->request()->getClientId();
98
99
        if (strpos($clientId, '.') !== false) {
100
            [$sessionId, $sessionNumber] = explode('.', $clientId);
101
            $event->setSessionId($sessionId);
102
            $event->setSessionNumber($sessionNumber);
103
        }
104
105
        return $this->request()->addEvent($event);
106
    }
107
108
    /**
109
     * Send the events collected so far.
110
     *
111
     * @return BaseResponse|null
112
     * @throws HydrationException
113
     * @throws ValidationException
114
     */
115
    public function sendCollectedEvents(): ?BaseResponse
116
    {
117
        if ($this->_shouldSendAnalytics === null) {
118
            $this->_shouldSendAnalytics = AnalyticsHelper::shouldSendAnalytics();
119
        }
120
121
        if (!$this->_shouldSendAnalytics) {
122
            return null;
123
        }
124
125
        $service = $this->service();
126
127
        if (!$service) {
0 ignored issues
show
introduced by
$service is of type nystudio107\instantanalyticsGa4\ga4\Service, thus it always evaluated to true.
Loading history...
128
            return null;
129
        }
130
131
        $request = $this->request();
132
        $eventCount = count($request->getEvents()->getEventList());
133
134
        if (!InstantAnalytics::$settings->sendAnalyticsData) {
135
            InstantAnalytics::$plugin->logAnalyticsEvent(
0 ignored issues
show
Bug introduced by
The method logAnalyticsEvent() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
            InstantAnalytics::$plugin->/** @scrutinizer ignore-call */ 
136
                                       logAnalyticsEvent(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
136
                'Analytics not enabled - skipped sending {count} events',
137
                ['count' => $eventCount],
138
                __METHOD__
139
            );
140
141
            return null;
142
        }
143
144
        if ($eventCount === 0) {
145
            InstantAnalytics::$plugin->logAnalyticsEvent(
146
                'No events collected to send',
147
                [],
148
                __METHOD__
149
            );
150
151
            return null;
152
        }
153
154
        InstantAnalytics::$plugin->logAnalyticsEvent(
155
            'Sending {count} analytics events',
156
            ['count' => $eventCount],
157
            __METHOD__
158
        );
159
160
        $response = $service->send($request);
161
162
        // Clear events already sent from the list.
163
        $request->getEvents()->setEventList([]);
164
165
        return $response;
166
    }
167
168
    /**
169
     * Set affiliation for all the events that incorporate Commerce Product info for the remaining duration of request.
170
     *
171
     * @param string $affiliation
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
172
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
173
     */
174
    public function setAffiliation(string $affiliation): self
175
    {
176
        $this->_affiliation = $affiliation;
177
        return $this;
178
    }
179
180
    public function getAffiliation(): ?string
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getAffiliation()
Loading history...
181
    {
182
        return $this->_affiliation;
183
    }
184
185
    /**
186
     * Add a commerce item list impression.
187
     *
188
     * @param Product|Variant $productVariant
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
189
     * @param int $index
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 13 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
190
     * @param string $listName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
191
     * @throws InvalidConfigException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
192
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
193
    public function addCommerceProductImpression($productVariant, int $index = 0, string $listName = 'default') {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
194
        InstantAnalytics::$plugin->commerce->addCommerceProductImpression($productVariant);
195
    }
196
197
    /**
198
     * Begin checkout.
199
     *
200
     * @param Order $cart
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
201
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
202
    public function beginCheckout(Order $cart) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
203
        InstantAnalytics::$plugin->commerce->triggerBeginCheckoutEvent($cart);
204
    }
205
206
    /**
207
     * Add a commerce item list impression.
208
     *
209
     * @param Product|Variant $productVariant
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 6 spaces but found 1
Loading history...
210
     * @param int $index
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 13 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 6 spaces but found 1
Loading history...
211
     * @param string $listName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 10 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 6 spaces but found 1
Loading history...
212
     * @deprecated `Analytics::addCommerceProductDetailView()` is deprecated. Use `Analytics::addCommerceProductImpression()` instead.
0 ignored issues
show
Coding Style introduced by
Tag @deprecated cannot be grouped with parameter tags in a doc comment
Loading history...
213
     * @throws InvalidConfigException
0 ignored issues
show
Coding Style introduced by
Tag @throws cannot be grouped with parameter tags in a doc comment
Loading history...
Coding Style introduced by
Tag value for @throws tag indented incorrectly; expected 5 spaces but found 1
Loading history...
214
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
215
    public function addCommerceProductDetailView($productVariant, int $index = 0, string $listName = 'default') {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
216
        Craft::$app->getDeprecator()->log('Analytics::addCommerceProductDetailView()', '`Analytics::addCommerceProductDetailView()` is deprecated. Use `Analytics::addCommerceProductImpression()` instead.');
217
        $this->addCommerceProductImpression($productVariant);
218
    }
219
220
    /**
221
     * Add a commerce product list impression.
222
     *
223
     * @param array $products
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
224
     * @param $listName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
225
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
226
    public function addCommerceProductListImpression(array $products, $listName) {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on a new line
Loading history...
227
        InstantAnalytics::$plugin->commerce->addCommerceProductListImpression($products, $listName);
228
    }
229
230
    /**
231
     * Set the measurement id.
232
     *
233
     * @param string $measurementId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
234
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
235
     */
236
    public function setMeasurementId(string $measurementId): self
237
    {
238
        $service = $this->service();
239
240
        if (!$service) {
0 ignored issues
show
introduced by
$service is of type nystudio107\instantanalyticsGa4\ga4\Service, thus it always evaluated to true.
Loading history...
241
            throw new InvalidConfigException('instant-analytics-ga4', 'Unable to create GA4 service object');
242
        }
243
244
        $service->setMeasurementId($measurementId);
245
        return $this;
246
    }
247
248
    /**
249
     * Set the API secret.
250
     *
251
     * @param string $apiSecret
0 ignored issues
show
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
252
     * @return $this
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
253
     */
254
    public function setApiSecret(string $apiSecret): self
255
    {
256
        $service = $this->service();
257
258
        if (!$service) {
0 ignored issues
show
introduced by
$service is of type nystudio107\instantanalyticsGa4\ga4\Service, thus it always evaluated to true.
Loading history...
259
            throw new InvalidConfigException('instant-analytics-ga4', 'Unable to create GA4 service object');
260
        }
261
262
        return $this;
263
    }
264
265
    public function __call(string $methodName, array $arguments): ?self
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __call()
Loading history...
266
    {
267
        $knownProperties = [
268
            'allowGoogleSignals' => 'allow_google_signals',
269
            'allowAdPersonalizationSignals' => 'allow_ad_personalization_signals',
270
            'campaignContent' => 'campaign_content',
271
            'campaignId' => 'campaign_id',
272
            'campaignMedium' => 'campaign_medium',
273
            'campaignName' => 'campaign_name',
274
            'campaignSource' => 'campaign_source',
275
            'campaignTerm' => 'campaign_term',
276
            'campaign' => 'campaign',
277
            'clientId' => 'client_id',
278
            'contentGroup' => 'content_group',
279
            'cookieDomain' => 'cookie_domain',
280
            'cookieExpires' => 'cookie_expires',
281
            'cookieFlags' => 'cookie_flags',
282
            'cookiePath' => 'cookie_path',
283
            'cookiePrefix' => 'cookie_prefix',
284
            'cookieUpdate' => 'cookie_update',
285
            'language' => 'language',
286
            'pageLocation' => 'page_location',
287
            'pageReferrer' => 'page_referrer',
288
            'pageTitle' => 'page_title',
289
            'sendPageView' => 'send_page_view',
290
            'screenResolution' => 'screen_resolution',
291
            'userId' => 'user_id'
292
        ];
293
294
        if (str_starts_with($methodName, 'set')) {
295
            $methodName = lcfirst(substr($methodName, 3));
296
297
            $service = $this->service();
298
            if ($service && !empty($knownProperties[$methodName])) {
299
                $service->setAdditionalQueryParam($knownProperties[$methodName], $arguments[0]);
300
301
                return $this;
302
            }
303
304
        }
305
306
        return null;
307
    }
308
309
    protected function request(): BaseRequest
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function request()
Loading history...
310
    {
311
        if ($this->_request === null) {
312
            $this->_request = new BaseRequest();
313
314
            $this->_request->setClientId(AnalyticsHelper::getClientId());
315
316
            if (InstantAnalytics::$settings->sendUserId) {
317
                $userId = AnalyticsHelper::getUserId();
318
319
                if ($userId) {
320
                    $this->request()->setUserId($userId);
321
                }
322
            }
323
        }
324
325
326
        return $this->_request;
327
    }
328
329
    /**
330
     * Init the service used to send events
331
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
332
    public function init(): void
333
    {
334
        $this->service();
335
        $this->request();
336
    }
337
338
    protected function service(): ?Service
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function service()
Loading history...
339
    {
340
        if ($this->_service === null) {
341
            $settings = InstantAnalytics::$settings;
342
            $apiSecret = Craft::parseEnv($settings->googleAnalyticsMeasurementApiSecret);
0 ignored issues
show
Deprecated Code introduced by
The function Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

342
            $apiSecret = /** @scrutinizer ignore-deprecated */ Craft::parseEnv($settings->googleAnalyticsMeasurementApiSecret);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
343
            $measurementId = Craft::parseEnv($settings->googleAnalyticsMeasurementId);
0 ignored issues
show
Deprecated Code introduced by
The function Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

343
            $measurementId = /** @scrutinizer ignore-deprecated */ Craft::parseEnv($settings->googleAnalyticsMeasurementId);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
344
345
            if (empty($apiSecret) || empty($measurementId)) {
346
                InstantAnalytics::$plugin->logAnalyticsEvent(
347
                    'API secret or measurement ID not set up for Instant Analytics',
348
                    [],
349
                    __METHOD__
350
                );
351
                $this->_service = false;
352
353
                return null;
354
            }
355
            $this->_service = new Service($apiSecret, $measurementId);
0 ignored issues
show
Bug introduced by
It seems like $apiSecret can also be of type boolean; however, parameter $apiSecret of nystudio107\instantanaly...\Service::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

355
            $this->_service = new Service(/** @scrutinizer ignore-type */ $apiSecret, $measurementId);
Loading history...
Bug introduced by
It seems like $measurementId can also be of type boolean; however, parameter $measurementId of nystudio107\instantanaly...\Service::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

355
            $this->_service = new Service($apiSecret, /** @scrutinizer ignore-type */ $measurementId);
Loading history...
356
357
            $ga4Client = new HttpClient();
358
            $ga4Client->setClient(Craft::createGuzzleClient());
359
            $this->_service->setHttpClient($ga4Client);
360
361
            $request = Craft::$app->getRequest();
362
            try {
363
                $session = Craft::$app->getSession();
364
            } catch (MissingComponentException $exception) {
365
                $session = null;
366
            }
367
368
            $this->setPageReferrer($request->getReferrer());
369
370
            // Load any campaign values from session or request
371
            $campaignParams = [
372
                'utm_source' => 'CampaignSource',
373
                'utm_medium' => 'CampaignMedium',
374
                'utm_campaign' => 'CampaignName',
375
                'utm_content' => 'CampaignContent',
376
                'utm_term' => 'CampaignTerm',
377
            ];
378
379
            // Load them up for GA4
380
            foreach ($campaignParams as $key => $method) {
381
                $value = $request->getParam($key) ?? $session->get($key) ?? null;
382
                $method = 'set' . $method;
383
384
                $this->$method($value);
385
386
                if ($session && $value) {
387
                    $session->set($key, $value);
388
                }
389
390
            }
391
392
            // If SEOmatic is installed, set the affiliation as well
393
            if (InstantAnalytics::$seomaticPlugin && Seomatic::$settings->renderEnabled && Seomatic::$plugin->metaContainers->metaSiteVars !== null) {
394
                $siteName = Seomatic::$plugin->metaContainers->metaSiteVars->siteName;
395
                $this->setAffiliation($siteName);
396
            }
397
398
        }
399
400
        if ($this->_service === false) {
401
            return null;
402
        }
403
404
        return $this->_service;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->_service could return the type true which is incompatible with the type-hinted return null|nystudio107\instantanalyticsGa4\ga4\Service. Consider adding an additional type-check to rule them out.
Loading history...
405
    }
406
}
407