Passed
Push — develop ( 2f004f...08c0a7 )
by Andrew
05:36
created

InstantAnalytics::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 14
c 3
b 0
f 0
dl 0
loc 23
rs 9.7998
cc 1
nc 1
nop 0
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
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
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 @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\instantanalytics;
12
13
use nystudio107\instantanalytics\assetbundles\instantanalytics\InstantAnalyticsAsset;
14
use nystudio107\instantanalytics\helpers\IAnalytics;
15
use nystudio107\instantanalytics\helpers\Field as FieldHelper;
16
use nystudio107\instantanalytics\models\Settings;
17
use nystudio107\instantanalytics\services\Commerce as CommerceService;
18
use nystudio107\instantanalytics\services\IA as IAService;
19
use nystudio107\instantanalytics\variables\InstantAnalyticsVariable;
20
use nystudio107\instantanalytics\twigextensions\InstantAnalyticsTwigExtension;
21
22
use nystudio107\pluginmanifest\services\ManifestService;
23
24
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...
25
26
use Craft;
27
use craft\base\Plugin;
28
use craft\events\PluginEvent;
29
use craft\events\RegisterUrlRulesEvent;
30
use craft\events\TemplateEvent;
31
use craft\helpers\UrlHelper;
32
use craft\services\Plugins;
33
use craft\web\twig\variables\CraftVariable;
34
use craft\web\UrlManager;
35
use craft\web\View;
36
37
use craft\commerce\Plugin as Commerce;
0 ignored issues
show
Bug introduced by
The type craft\commerce\Plugin 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...
38
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...
39
use craft\commerce\events\LineItemEvent;
0 ignored issues
show
Bug introduced by
The type craft\commerce\events\LineItemEvent 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...
40
41
use Twig\Error\LoaderError;
42
43
use yii\base\Event;
44
45
/** @noinspection MissingPropertyAnnotationsInspection */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
46
47
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
48
 * @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...
49
 * @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...
50
 * @since     1.0.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...
51
 *
52
 * @property IAService          $ia
53
 * @property CommerceService    $commerce
54
 * @property ManifestService    $manifest
55
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
56
class InstantAnalytics extends Plugin
57
{
58
    // Constants
59
    // =========================================================================
60
61
    const COMMERCE_PLUGIN_HANDLE = 'commerce';
62
    const SEOMATIC_PLUGIN_HANDLE = 'seomatic';
63
64
    // Static Properties
65
    // =========================================================================
66
67
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
68
     * @var InstantAnalytics
69
     */
70
    public static $plugin;
71
72
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
73
     * @var Settings
74
     */
75
    public static $settings;
76
77
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
78
     * @var Commerce|null
79
     */
80
    public static $commercePlugin;
81
82
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
83
     * @var Seomatic|null
84
     */
85
    public static $seomaticPlugin;
86
87
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
88
     * @var string
89
     */
90
    public static $currentTemplate = '';
91
92
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
93
     * @var bool
94
     */
95
    public static $pageViewSent = false;
96
97
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
98
     * @var bool
99
     */
100
    public static $craft31 = false;
101
102
    // Static Methods
103
    // =========================================================================
104
105
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $parent should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $id should have a doc-comment as per coding-style.
Loading history...
106
     * @inheritdoc
107
     */
108
    public function __construct($id, $parent = null, array $config = [])
109
    {
110
        $config['components'] = [
111
            'ia' => IAService::class,
112
            'commerce' => CommerceService::class,
113
            // Register the manifest service
114
            'manifest' => [
115
                'class' => ManifestService::class,
116
                'assetClass' => InstantAnalyticsAsset::class,
117
                'devServerManifestPath' => 'http://craft-instantanalytics-buildchain:8080/',
118
                'devServerPublicPath' => 'http://craft-instantanalytics-buildchain:8080/',
119
            ],
120
        ];
121
122
        parent::__construct($id, $parent, $config);
123
    }
124
125
    // Public Properties
126
    // =========================================================================
127
128
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
129
     * @var string
130
     */
131
    public $schemaVersion = '1.0.0';
132
133
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
134
     * @var bool
135
     */
136
    public $hasCpSection = false;
137
138
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
139
     * @var bool
140
     */
141
    public $hasCpSettings = true;
142
143
    // Public Methods
144
    // =========================================================================
145
146
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
147
     * @inheritdoc
148
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
149
    public function init()
150
    {
151
        parent::init();
152
        self::$plugin = $this;
153
        self::$settings = $this->getSettings();
154
        self::$craft31 = version_compare(Craft::$app->getVersion(), '3.1', '>=');
0 ignored issues
show
Documentation Bug introduced by
It seems like version_compare(Craft::a...Version(), '3.1', '>=') can also be of type integer. However, the property $craft31 is declared as type boolean. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
155
156
        // Determine if Craft Commerce is installed & enabled
157
        self::$commercePlugin = Craft::$app->getPlugins()->getPlugin(self::COMMERCE_PLUGIN_HANDLE);
158
        // Determine if SEOmatic is installed & enabled
159
        self::$seomaticPlugin = Craft::$app->getPlugins()->getPlugin(self::SEOMATIC_PLUGIN_HANDLE);
160
        // Add in our Craft components
161
        $this->addComponents();
162
        // Install our global event handlers
163
        $this->installEventListeners();
164
165
        Craft::info(
166
            Craft::t(
167
                'instant-analytics',
168
                '{name} plugin loaded',
169
                ['name' => $this->name]
170
            ),
171
            __METHOD__
172
        );
173
    }
174
175
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
176
     * @inheritdoc
177
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
178
    public function settingsHtml()
179
    {
180
        $commerceFields = [];
181
182
        if (self::$commercePlugin) {
183
            $productTypes = self::$commercePlugin->getProductTypes()->getAllProductTypes();
184
185
            foreach ($productTypes as $productType) {
186
                $productFields = $this->getPullFieldsFromLayoutId($productType->fieldLayoutId);
187
                /** @noinspection SlowArrayOperationsInLoopInspection */
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
188
                $commerceFields = \array_merge($commerceFields, $productFields);
189
                if ($productType->hasVariants) {
190
                    $variantFields = $this->getPullFieldsFromLayoutId($productType->variantFieldLayoutId);
191
                    /** @noinspection SlowArrayOperationsInLoopInspection */
0 ignored issues
show
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
192
                    $commerceFields = \array_merge($commerceFields, $variantFields);
193
                }
194
            }
195
        }
196
197
        // Rend the settings template
198
        try {
199
            return Craft::$app->getView()->renderTemplate(
200
                'instant-analytics/settings',
201
                [
202
                    'settings' => $this->getSettings(),
203
                    'commerceFields' => $commerceFields,
204
                ]
205
            );
206
        } catch (LoaderError $e) {
207
            Craft::error($e->getMessage(), __METHOD__);
208
        } catch (\Exception $e) {
209
            Craft::error($e->getMessage(), __METHOD__);
210
        }
211
212
        return '';
213
    }
214
215
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $context should have a doc-comment as per coding-style.
Loading history...
216
     * Handle the `{% hook iaSendPageView %}`
217
     *
218
     * @param array &$context
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter &$context does not match actual variable name $context
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
219
     *
220
     * @return string|null
221
     */
222
    public function iaSendPageView(/** @noinspection PhpUnusedParameterInspection */ array &$context)
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
223
    {
224
        $this->sendPageView();
225
226
        return '';
227
    }
228
229
    // Protected Methods
230
    // =========================================================================
231
232
    /**
233
     * Add in our Craft components
234
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
235
    protected function addComponents()
236
    {
237
        $view = Craft::$app->getView();
238
        // Add in our Twig extensions
239
        $view->registerTwigExtension(new InstantAnalyticsTwigExtension());
240
        // Install our template hook
241
        $view->hook('iaSendPageView', [$this, 'iaSendPageView']);
242
        // Register our variables
243
        Event::on(
244
            CraftVariable::class,
245
            CraftVariable::EVENT_INIT,
246
            function (Event $event) {
247
                /** @var CraftVariable $variable */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
248
                $variable = $event->sender;
249
                $variable->set('instantAnalytics', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
250
                    'class' => InstantAnalyticsVariable::class,
251
                    'manifestService' => $this->manifest,
252
                ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
253
            }
254
        );
255
    }
256
257
    /**
258
     * Install our event listeners
259
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
260
    protected function installEventListeners()
261
    {
262
        // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN
263
        Event::on(
264
            Plugins::class,
265
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
266
            function (PluginEvent $event) {
267
                if ($event->plugin === $this) {
268
                    $request = Craft::$app->getRequest();
269
                    if ($request->isCpRequest) {
270
                        Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('instant-analytics/welcome'))->send();
271
                    }
272
                }
273
            }
274
        );
275
        $request = Craft::$app->getRequest();
276
        // Install only for non-console site requests
277
        if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
278
            $this->installSiteEventListeners();
279
        }
280
        // Install only for non-console Control Panel requests
281
        if ($request->getIsCpRequest() && !$request->getIsConsoleRequest()) {
282
            $this->installCpEventListeners();
283
        }
284
    }
285
286
    /**
287
     * Install site event listeners for site requests only
288
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
289
    protected function installSiteEventListeners()
290
    {
291
        // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES
292
        Event::on(
293
            UrlManager::class,
294
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
295
            function (RegisterUrlRulesEvent $event) {
296
                Craft::debug(
297
                    'UrlManager::EVENT_REGISTER_SITE_URL_RULES',
298
                    __METHOD__
299
                );
300
                // Register our Control Panel routes
301
                $event->rules = array_merge(
302
                    $event->rules,
303
                    $this->customFrontendRoutes()
304
                );
305
            }
306
        );
307
        // Remember the name of the currently rendering template
308
        Event::on(
309
            View::class,
310
            View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
311
            function (TemplateEvent $event) {
312
                self::$currentTemplate = $event->template;
313
            }
314
        );
315
        // Remember the name of the currently rendering template
316
        Event::on(
317
            View::class,
318
            View::EVENT_AFTER_RENDER_PAGE_TEMPLATE,
319
            function (TemplateEvent $event) {
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

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

319
            function (/** @scrutinizer ignore-unused */ TemplateEvent $event) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
320
                if (self::$settings->autoSendPageView) {
321
                    $this->sendPageView();
322
                }
323
            }
324
        );
325
        // Commerce-specific hooks
326
        if (self::$commercePlugin) {
327
            Event::on(Order::class, Order::EVENT_AFTER_COMPLETE_ORDER, function (Event $e) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
328
                $order = $e->sender;
329
                if (self::$settings->autoSendPurchaseComplete) {
330
                    $this->commerce->orderComplete($order);
331
                }
332
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
333
334
            Event::on(Order::class, Order::EVENT_AFTER_ADD_LINE_ITEM, function (LineItemEvent $e) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
335
                $lineItem = $e->lineItem;
336
                if (self::$settings->autoSendAddToCart) {
337
                    $this->commerce->addToCart($lineItem->order, $lineItem);
338
                }
339
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
340
341
            // Check to make sure Order::EVENT_AFTER_REMOVE_LINE_ITEM is defined
342
            if (defined(Order::class . '::EVENT_AFTER_REMOVE_LINE_ITEM')) {
343
                Event::on(Order::class, Order::EVENT_AFTER_REMOVE_LINE_ITEM, function (LineItemEvent $e) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
344
                    $lineItem = $e->lineItem;
345
                    if (self::$settings->autoSendRemoveFromCart) {
346
                        $this->commerce->removeFromCart($lineItem->order, $lineItem);
347
                    }
348
                });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
349
            }
350
        }
351
    }
352
353
    /**
354
     * Install site event listeners for Control Panel requests only
355
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
356
    protected function installCpEventListeners()
357
    {
358
    }
359
360
    /**
361
     * Return the custom frontend routes
362
     *
363
     * @return array
364
     */
365
    protected function customFrontendRoutes(): array
366
    {
367
        return [
368
            'instantanalytics/pageViewTrack/<filename:[-\w\.*]+>?' =>
369
                'instant-analytics/track/track-page-view-url',
370
            'instantanalytics/eventTrack/<filename:[-\w\.*]+>?' =>
371
                'instant-analytics/track/track-event-url',
372
        ];
373
    }
374
375
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
376
     * @inheritdoc
377
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
378
    protected function createSettingsModel()
379
    {
380
        return new Settings();
381
    }
382
383
    // Private Methods
384
    // =========================================================================
385
386
    /**
387
     * Send a page view with the pre-loaded IAnalytics object
388
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
389
    private function sendPageView()
0 ignored issues
show
Coding Style introduced by
Private method name "InstantAnalytics::sendPageView" must be prefixed with an underscore
Loading history...
390
    {
391
        $request = Craft::$app->getRequest();
392
        if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest() && !self::$pageViewSent) {
393
            self::$pageViewSent = true;
394
            /** @var IAnalytics $analytics */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
395
            $analytics = self::$plugin->ia->getGlobals(self::$currentTemplate);
396
            // Bail if we have no analytics object
397
            if ($analytics === null) {
398
                return;
399
            }
400
            // If SEOmatic is installed, set the page title from it
401
            $this->setTitleFromSeomatic($analytics);
402
            // Send the page view
403
            if ($analytics) {
0 ignored issues
show
introduced by
$analytics is of type nystudio107\instantanalytics\helpers\IAnalytics, thus it always evaluated to true.
Loading history...
404
                $response = $analytics->sendPageview();
405
                Craft::info(
406
                    Craft::t(
407
                        'instant-analytics',
408
                        'pageView sent, response:: {response}',
409
                        [
410
                            'response' => print_r($response, true),
411
                        ]
412
                    ),
413
                    __METHOD__
414
                );
415
            } else {
416
                Craft::error(
417
                    Craft::t(
418
                        'instant-analytics',
419
                        'Analytics not sent because googleAnalyticsTracking is not set'
420
                    ),
421
                    __METHOD__
422
                );
423
            }
424
        }
425
    }
426
427
    /**
428
     * If SEOmatic is installed, set the page title from it
429
     *
430
     * @param $analytics
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
431
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
432
    private function setTitleFromSeomatic(IAnalytics $analytics)
0 ignored issues
show
Coding Style introduced by
Private method name "InstantAnalytics::setTitleFromSeomatic" must be prefixed with an underscore
Loading history...
433
    {
434
        if (self::$seomaticPlugin && Seomatic::$settings->renderEnabled) {
435
            $titleTag = Seomatic::$plugin->title->get('title');
436
            if ($titleTag) {
437
                $titleArray = $titleTag->renderAttributes();
438
                if (!empty($titleArray['title'])) {
439
                    $analytics->setDocumentTitle($titleArray['title']);
440
                }
441
            }
442
        }
443
    }
444
445
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
446
     * @param $layoutId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
447
     *
448
     * @return array
449
     */
450
    private function getPullFieldsFromLayoutId($layoutId): array
0 ignored issues
show
Coding Style introduced by
Private method name "InstantAnalytics::getPullFieldsFromLayoutId" must be prefixed with an underscore
Loading history...
451
    {
452
        $result = ['' => 'none'];
453
        if ($layoutId === null) {
454
            return $result;
455
        }
456
        $fieldLayout = Craft::$app->getFields()->getLayoutById($layoutId);
457
        if ($fieldLayout) {
458
            $result = FieldHelper::fieldsOfTypeFromLayout(FieldHelper::TEXT_FIELD_CLASS_KEY, $fieldLayout, false);
459
        }
460
461
        return $result;
462
    }
463
}
464