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
![]() |
|||||||
8 | * @copyright Copyright (c) 2017 nystudio107 |
||||||
0 ignored issues
–
show
|
|||||||
9 | */ |
||||||
0 ignored issues
–
show
|
|||||||
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
|
|||||||
24 | |||||||
25 | /** |
||||||
0 ignored issues
–
show
|
|||||||
26 | * @author nystudio107 |
||||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||||||
27 | * @package InstantAnalytics |
||||||
0 ignored issues
–
show
|
|||||||
28 | * @since 5.0.0 |
||||||
0 ignored issues
–
show
|
|||||||
29 | */ |
||||||
0 ignored issues
–
show
|
|||||||
30 | class Ga4 extends Component |
||||||
31 | { |
||||||
32 | /** |
||||||
0 ignored issues
–
show
|
|||||||
33 | * @var ?Analytics |
||||||
34 | */ |
||||||
35 | private $_analytics = null; |
||||||
36 | |||||||
37 | /** |
||||||
0 ignored issues
–
show
|
|||||||
38 | * @var bool |
||||||
39 | */ |
||||||
40 | private $_pageViewSent = false; |
||||||
41 | |||||||
42 | public function getAnalytics(): Analytics |
||||||
0 ignored issues
–
show
|
|||||||
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
|
|||||||
53 | * Send a page view event |
||||||
54 | */ |
||||||
0 ignored issues
–
show
|
|||||||
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
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
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. ![]() |
|||||||
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
|
|||||||
77 | * @param string $eventName |
||||||
0 ignored issues
–
show
|
|||||||
78 | * @param array $params |
||||||
0 ignored issues
–
show
|
|||||||
79 | */ |
||||||
0 ignored issues
–
show
|
|||||||
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
|
|||||||
102 | * @param string $pageTitle |
||||||
0 ignored issues
–
show
|
|||||||
103 | * @return PageViewEvent |
||||||
0 ignored issues
–
show
|
|||||||
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
|
|||||||
126 | * @return BaseEvent |
||||||
0 ignored issues
–
show
|
|||||||
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 |