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