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 |
|
|
|
|
8
|
|
|
* @copyright Copyright (c) 2017 nystudio107 |
|
|
|
|
9
|
|
|
*/ |
|
|
|
|
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 */ |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @author nystudio107 |
|
|
|
|
27
|
|
|
* @package InstantAnalytics |
|
|
|
|
28
|
|
|
* @since 5.0.0 |
|
|
|
|
29
|
|
|
*/ |
|
|
|
|
30
|
|
|
class Ga4 extends Component |
31
|
|
|
{ |
32
|
|
|
/** |
|
|
|
|
33
|
|
|
* @var ?Analytics |
34
|
|
|
*/ |
35
|
|
|
private $_analytics = null; |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
private $_pageViewSent = false; |
41
|
|
|
|
42
|
|
|
public function getAnalytics(): Analytics |
|
|
|
|
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
|
|
|
/** |
|
|
|
|
53
|
|
|
* Send a page view event |
54
|
|
|
*/ |
|
|
|
|
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( |
|
|
|
|
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 |
|
|
|
|
77
|
|
|
* @param string $eventName |
|
|
|
|
78
|
|
|
* @param array $params |
|
|
|
|
79
|
|
|
*/ |
|
|
|
|
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 |
|
|
|
|
102
|
|
|
* @param string $pageTitle |
|
|
|
|
103
|
|
|
* @return PageViewEvent |
|
|
|
|
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 |
|
|
|
|
126
|
|
|
* @return BaseEvent |
|
|
|
|
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
|
|
|
|