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\models; |
12
|
|
|
|
13
|
|
|
use craft\base\Model; |
14
|
|
|
use craft\behaviors\EnvAttributeParserBehavior; |
15
|
|
|
use craft\validators\ArrayValidator; |
16
|
|
|
use yii\behaviors\AttributeTypecastBehavior; |
17
|
|
|
|
18
|
|
|
/** |
|
|
|
|
19
|
|
|
* @author nystudio107 |
|
|
|
|
20
|
|
|
* @package InstantAnalytics |
|
|
|
|
21
|
|
|
* @since 1.0.0 |
|
|
|
|
22
|
|
|
*/ |
|
|
|
|
23
|
|
|
class Settings extends Model |
24
|
|
|
{ |
25
|
|
|
// Public Properties |
26
|
|
|
// ========================================================================= |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The default Google Analytics measurement ID used by GA4. |
30
|
|
|
* @var string |
|
|
|
|
31
|
|
|
*/ |
32
|
|
|
public string $googleAnalyticsMeasurementId = ''; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The default Google Analytics measurement API secret used by GA4. |
36
|
|
|
* @var string |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public string $googleAnalyticsMeasurementApiSecret = ''; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* The default Google Analytics tracking ID |
42
|
|
|
* |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
public string $googleAnalyticsTracking = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Should the query string be stripped from the page tracking URL? |
49
|
|
|
* |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
public bool $stripQueryString = true; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Should page views be sent automatically when a page view happens? |
56
|
|
|
* |
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
public bool $autoSendPageView = true; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* If you plan to use Instant Analytics in conjunction with frontend JavaScript, this setting should be on, so that Instant Analytics requires a `clientId` from the frontend-set GA cookie before it will send analytics data. |
63
|
|
|
* @var bool |
|
|
|
|
64
|
|
|
*/ |
65
|
|
|
public bool $requireGaCookieClientId = true; |
66
|
|
|
|
67
|
|
|
/** |
|
|
|
|
68
|
|
|
* @var bool Should the GCLID cookie be created if it doesn't exist? |
69
|
|
|
*/ |
70
|
|
|
public bool $createGclidCookie = true; |
71
|
|
|
|
72
|
|
|
/** |
|
|
|
|
73
|
|
|
* @var bool Should the user id property be set on events, if user is logged in. |
74
|
|
|
*/ |
75
|
|
|
public $sendUserId = true; |
76
|
|
|
|
77
|
|
|
/** |
|
|
|
|
78
|
|
|
* @var int The session duration as set in GA4 admin settings. |
79
|
|
|
*/ |
80
|
|
|
public $sessionDuration = 30; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* The field in a Commerce Product Variant that should be used for the |
84
|
|
|
* category |
85
|
|
|
* |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
public string $productCategoryField = ''; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* The field in a Commerce Product Variant that should be used for the brand |
92
|
|
|
* |
93
|
|
|
* @var string |
94
|
|
|
*/ |
95
|
|
|
public string $productBrandField = ''; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Whether add to cart events should be automatically sent |
99
|
|
|
* |
100
|
|
|
* @var bool |
101
|
|
|
*/ |
102
|
|
|
public bool $autoSendAddToCart = true; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Whether remove from cart events should be automatically sent |
106
|
|
|
* |
107
|
|
|
* @var bool |
108
|
|
|
*/ |
109
|
|
|
public bool $autoSendRemoveFromCart = true; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Whether purchase complete events should be automatically sent |
113
|
|
|
* |
114
|
|
|
* @var bool |
115
|
|
|
*/ |
116
|
|
|
public bool $autoSendPurchaseComplete = true; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Controls whether Instant Analytics will send analytics data. |
120
|
|
|
* |
121
|
|
|
* @var bool |
122
|
|
|
*/ |
123
|
|
|
public bool $sendAnalyticsData = true; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Controls whether Instant Analytics will send analytics data when |
127
|
|
|
* `devMode` is on. |
128
|
|
|
* |
129
|
|
|
* @var bool |
130
|
|
|
*/ |
131
|
|
|
public bool $sendAnalyticsInDevMode = true; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Controls whether we should filter out bot UserGents. |
135
|
|
|
* |
136
|
|
|
* @var bool |
137
|
|
|
*/ |
138
|
|
|
public bool $filterBotUserAgents = true; |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Controls whether we should exclude users logged into an admin account |
142
|
|
|
* from Analytics tracking. |
143
|
|
|
* |
144
|
|
|
* @var bool |
145
|
|
|
*/ |
146
|
|
|
public bool $adminExclude = false; |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Controls whether analytics that blocked from being sent should be logged |
150
|
|
|
* to storage/logs/web.log These are always logged if `devMode` is on |
151
|
|
|
* |
152
|
|
|
* @var bool |
153
|
|
|
*/ |
154
|
|
|
public bool $logExcludedAnalytics = true; |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Contains an array of Craft user group handles to exclude from Analytics |
158
|
|
|
* tracking. If there's a match for any of them, analytics data is not |
159
|
|
|
* sent. |
160
|
|
|
* |
161
|
|
|
* @var array |
162
|
|
|
*/ |
163
|
|
|
public array $groupExcludes = [ |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Contains an array of keys that correspond to $_SERVER[] super-global |
168
|
|
|
* array keys to test against. Each item in the sub-array is tested against |
169
|
|
|
* the $_SERVER[] super-global key via RegEx; if there's a match for any of |
170
|
|
|
* them, analytics data is not sent. This allows you to filter based on |
171
|
|
|
* whatever information you want. Reference: |
172
|
|
|
* http://php.net/manual/en/reserved.variables.server.php RegEx tester: |
173
|
|
|
* http://regexr.com |
174
|
|
|
* |
175
|
|
|
* @var array |
176
|
|
|
*/ |
177
|
|
|
public array $serverExcludes = [ |
178
|
|
|
'REMOTE_ADDR' => [ |
179
|
|
|
'/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/', |
180
|
|
|
], |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
// Public Methods |
184
|
|
|
// ========================================================================= |
185
|
|
|
|
186
|
|
|
/** |
|
|
|
|
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
|
public function defineRules(): array |
190
|
|
|
{ |
191
|
|
|
return [ |
192
|
|
|
[ |
193
|
|
|
[ |
194
|
|
|
'stripQueryString', |
195
|
|
|
'autoSendPageView', |
196
|
|
|
'requireGaCookieClientId', |
197
|
|
|
'createGclidCookie', |
198
|
|
|
'autoSendAddToCart', |
199
|
|
|
'autoSendRemoveFromCart', |
200
|
|
|
'autoSendPurchaseComplete', |
201
|
|
|
'sendAnalyticsData', |
202
|
|
|
'sendAnalyticsInDevMode', |
203
|
|
|
'filterBotUserAgents', |
204
|
|
|
'adminExclude', |
205
|
|
|
'logExcludedAnalytics', |
206
|
|
|
], |
207
|
|
|
'boolean', |
208
|
|
|
], |
209
|
|
|
[ |
210
|
|
|
[ |
211
|
|
|
'googleAnalyticsTracking', |
212
|
|
|
'productCategoryField', |
213
|
|
|
'productBrandField', |
214
|
|
|
'googleAnalyticsTracking', |
215
|
|
|
], |
216
|
|
|
'string', |
217
|
|
|
], |
218
|
|
|
[ |
219
|
|
|
[ |
220
|
|
|
'groupExcludes', |
221
|
|
|
'serverExcludes', |
222
|
|
|
], |
223
|
|
|
ArrayValidator::class, |
224
|
|
|
], |
225
|
|
|
]; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
|
|
|
|
229
|
|
|
* @return array |
230
|
|
|
*/ |
231
|
|
|
public function behaviors(): array |
232
|
|
|
{ |
233
|
|
|
return [ |
234
|
|
|
'typecast' => [ |
235
|
|
|
'class' => AttributeTypecastBehavior::class, |
236
|
|
|
// 'attributeTypes' will be composed automatically according to `rules()` |
237
|
|
|
], |
238
|
|
|
'parser' => [ |
239
|
|
|
'class' => EnvAttributeParserBehavior::class, |
240
|
|
|
'attributes' => [ |
241
|
|
|
'googleAnalyticsTracking', |
242
|
|
|
], |
243
|
|
|
], |
244
|
|
|
]; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|