Passed
Push — v4 ( 140115...483dd2 )
by Andrew
26:45 queued 20:28
created

Settings::defineRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 35
rs 9.568
c 0
b 0
f 0
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\models;
12
13
use craft\base\Model;
14
use craft\behaviors\EnvAttributeParserBehavior;
15
use craft\validators\ArrayValidator;
16
use yii\behaviors\AttributeTypecastBehavior;
17
18
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
19
 * @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...
20
 * @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...
21
 * @since     1.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...
22
 */
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...
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
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
31
     */
32
    public string $googleAnalyticsMeasurementId = '';
33
34
    /**
35
     * The default Google Analytics measurement API secret used by GA4.
36
     * @var string
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
64
     */
65
    public bool $requireGaCookieClientId = true;
66
67
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
68
     * @var bool Should the GCLID cookie be created if it doesn't exist?
69
     */
70
    public bool $createGclidCookie = true;
71
72
    /**
73
     * The field in a Commerce Product Variant that should be used for the
74
     * category
75
     *
76
     * @var string
77
     */
78
    public string $productCategoryField = '';
79
80
    /**
81
     * The field in a Commerce Product Variant that should be used for the brand
82
     *
83
     * @var string
84
     */
85
    public string $productBrandField = '';
86
87
    /**
88
     * Whether add to cart events should be automatically sent
89
     *
90
     * @var bool
91
     */
92
    public bool $autoSendAddToCart = true;
93
94
    /**
95
     * Whether remove from cart events should be automatically sent
96
     *
97
     * @var bool
98
     */
99
    public bool $autoSendRemoveFromCart = true;
100
101
    /**
102
     * Whether purchase complete events should be automatically sent
103
     *
104
     * @var bool
105
     */
106
    public bool $autoSendPurchaseComplete = true;
107
108
    /**
109
     * Controls whether Instant Analytics will send analytics data.
110
     *
111
     * @var bool
112
     */
113
    public bool $sendAnalyticsData = true;
114
115
    /**
116
     * Controls whether Instant Analytics will send analytics data when
117
     * `devMode` is on.
118
     *
119
     * @var bool
120
     */
121
    public bool $sendAnalyticsInDevMode = true;
122
123
    /**
124
     * Controls whether we should filter out bot UserGents.
125
     *
126
     * @var bool
127
     */
128
    public bool $filterBotUserAgents = true;
129
130
    /**
131
     * Controls whether we should exclude users logged into an admin account
132
     * from Analytics tracking.
133
     *
134
     * @var bool
135
     */
136
    public bool $adminExclude = false;
137
138
    /**
139
     * Controls whether analytics that blocked from being sent should be logged
140
     * to storage/logs/web.log These are always logged if `devMode` is on
141
     *
142
     * @var bool
143
     */
144
    public bool $logExcludedAnalytics = true;
145
146
    /**
147
     * Contains an array of Craft user group handles to exclude from Analytics
148
     * tracking.  If there's a match for any of them, analytics data is not
149
     * sent.
150
     *
151
     * @var array
152
     */
153
    public array $groupExcludes = [
154
    ];
155
156
    /**
157
     * Contains an array of keys that correspond to $_SERVER[] super-global
158
     * array keys to test against. Each item in the sub-array is tested against
159
     * the $_SERVER[] super-global key via RegEx; if there's a match for any of
160
     * them, analytics data is not sent.  This allows you to filter based on
161
     * whatever information you want. Reference:
162
     * http://php.net/manual/en/reserved.variables.server.php RegEx tester:
163
     * http://regexr.com
164
     *
165
     * @var array
166
     */
167
    public array $serverExcludes = [
168
        'REMOTE_ADDR' => [
169
            '/^localhost$|^127(?:\.[0-9]+){0,2}\.[0-9]+$|^(?:0*\:)*?:?0*1$/',
170
        ],
171
    ];
172
173
    // Public Methods
174
    // =========================================================================
175
176
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
177
     * @return array
178
     */
179
    public function defineRules(): array
180
    {
181
        return [
182
            [
183
                [
184
                    'stripQueryString',
185
                    'autoSendPageView',
186
                    'requireGaCookieClientId',
187
                    'createGclidCookie',
188
                    'autoSendAddToCart',
189
                    'autoSendRemoveFromCart',
190
                    'autoSendPurchaseComplete',
191
                    'sendAnalyticsData',
192
                    'sendAnalyticsInDevMode',
193
                    'filterBotUserAgents',
194
                    'adminExclude',
195
                    'logExcludedAnalytics',
196
                ],
197
                'boolean',
198
            ],
199
            [
200
                [
201
                    'googleAnalyticsTracking',
202
                    'productCategoryField',
203
                    'productBrandField',
204
                    'googleAnalyticsTracking',
205
                ],
206
                'string',
207
            ],
208
            [
209
                [
210
                    'groupExcludes',
211
                    'serverExcludes',
212
                ],
213
                ArrayValidator::class,
214
            ],
215
        ];
216
    }
217
218
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
219
     * @return array
220
     */
221
    public function behaviors(): array
222
    {
223
        return [
224
            'typecast' => [
225
                'class' => AttributeTypecastBehavior::class,
226
                // 'attributeTypes' will be composed automatically according to `rules()`
227
            ],
228
            'parser' => [
229
                'class' => EnvAttributeParserBehavior::class,
230
                'attributes' => [
231
                    'googleAnalyticsTracking',
232
                ],
233
            ],
234
        ];
235
    }
236
}
237