Ga4::addSimpleEvent()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 15
rs 9.9666
1
<?php
2
/**
3
 * Instant Analytics plugin for Craft CMS
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\instantanalyticsGa4\services;
12
13
use Br33f\Ga4\MeasurementProtocol\Dto\Event\BaseEvent;
14
use Br33f\Ga4\MeasurementProtocol\Dto\Parameter\BaseParameter;
15
use Craft;
16
use craft\base\Component;
17
use craft\helpers\Json;
18
use nystudio107\instantanalyticsGa4\ga4\Analytics;
19
use nystudio107\instantanalyticsGa4\ga4\events\PageViewEvent;
20
use nystudio107\instantanalyticsGa4\helpers\Analytics as AnalyticsHelper;
21
use nystudio107\instantanalyticsGa4\InstantAnalytics;
22
23
/** @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...
24
25
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
26
 * @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...
27
 * @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...
28
 * @since     5.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...
29
 */
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...
30
class Ga4 extends Component
31
{
32
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
33
     * @var ?Analytics
34
     */
35
    private $_analytics = null;
36
37
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
38
     * @var bool
39
     */
40
    private $_pageViewSent = false;
41
42
    public function getAnalytics(): Analytics
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getAnalytics()
Loading history...
43
    {
44
        if (!$this->_analytics) {
45
            $this->_analytics = Craft::createObject(Analytics::class);
46
            $this->_analytics->init();
47
        }
48
49
        return $this->_analytics;
50
    }
51
52
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $url should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $pageTitle should have a doc-comment as per coding-style.
Loading history...
53
     * Send a page view event
54
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
55
    public function addPageViewEvent(string $url = '', string $pageTitle = ''): void
56
    {
57
        $request = Craft::$app->getRequest();
58
59
        if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest() && !$this->_pageViewSent) {
60
            $this->_pageViewSent = true;
61
62
            $pageView = $this->getPageViewEvent($url, !empty($pageTitle) ? $pageTitle : InstantAnalytics::$currentTemplate);
63
            $this->getAnalytics()->addEvent($pageView);
64
65
            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

65
            InstantAnalytics::$plugin->/** @scrutinizer ignore-call */ 
66
                                       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...
66
                'pageView event queued for sending',
67
                [],
68
                __METHOD__
69
            );
70
        }
71
    }
72
73
    /**
74
     * Add a basic event to be sent to GA4
75
     *
76
     * @param string $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
77
     * @param string $eventName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
78
     * @param array $params
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
79
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
80
    public function addSimpleEvent(string $url, string $eventName, array $params): void
81
    {
82
        $baseEvent = $this->getSimpleEvent($eventName);
83
        $baseEvent->setParamValue('documentPath', parse_url($url, PHP_URL_PATH));
84
85
        foreach ($params as $param => $value) {
86
            $baseEvent->addParam($param, new BaseParameter($value));
87
        }
88
89
        $this->getAnalytics()->addEvent($baseEvent);
90
91
        InstantAnalytics::$plugin->logAnalyticsEvent(
92
            'Simple event queued for {url} with the following parameters {params}',
93
            ['url' => $url, 'params' => Json::encode($params)],
94
            __METHOD__
95
        );
96
    }
97
98
    /**
99
     * Create a page view event
100
     *
101
     * @param string $url
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...
102
     * @param string $pageTitle
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...
103
     * @return PageViewEvent
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
104
     */
105
    public function getPageViewEvent(string $url = '', string $pageTitle = ''): PageViewEvent
106
    {
107
        $event = $this->getAnalytics()->create()->PageViewEvent();
108
        $event->setPageTitle($pageTitle);
109
110
        // If SEOmatic is installed, set the page title from it
111
        $seomaticTitle = AnalyticsHelper::getTitleFromSeomatic();
112
113
        if ($seomaticTitle) {
114
            $event->setPageTitle($seomaticTitle);
115
        }
116
117
        $event->setPageLocation(AnalyticsHelper::getDocumentPathFromUrl($url));
118
119
        return $event;
120
    }
121
122
    /**
123
     * Create a simple event
124
     *
125
     * @param string $eventName
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...
126
     * @return BaseEvent
0 ignored issues
show
Coding Style introduced by
Tag @return cannot be grouped with parameter tags in a doc comment
Loading history...
127
     */
128
    public function getSimpleEvent(string $eventName): BaseEvent
129
    {
130
        $baseEvent = $this->getAnalytics()->create()->BaseEvent();
131
        $baseEvent->setName($eventName);
132
133
        return $baseEvent;
134
    }
135
}
136