AnalyticsBase   A
last analyzed

Complexity

Total Complexity 29

Size/Duplication

Total Lines 348
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 1

Importance

Changes 0
Metric Value
wmc 29
lcom 4
cbo 1
dl 0
loc 348
rs 10
c 0
b 0
f 0

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 6
A setEnabled() 0 4 1
A getEnabled() 0 4 1
A setId() 0 4 1
A getId() 0 4 1
A setDomain() 0 4 1
A getDomain() 0 4 1
A setAnonymised() 0 4 1
A getAnonymised() 0 4 1
A setAutomatic() 0 4 1
A getAutomatic() 0 4 1
A addItem() 0 4 1
A setItems() 0 4 1
A getItems() 0 4 1
A isEnabled() 0 4 1
A enableTracking() 0 4 1
A disableTracking() 0 4 1
A isAnonymised() 0 3 1
A enableAnonymisedTracking() 0 4 1
A disableAnonymisedTracking() 0 4 1
A isAutomatic() 0 4 1
A enableAutomaticTracking() 0 4 1
A disableAutomaticTracking() 0 4 1
A trackEcommerce() 0 6 1
1
<?php namespace Cornford\Googlitics;
2
3
use Cornford\Googlitics\Contracts\AnalyticalBaseInterface;
4
use Cornford\Googlitics\Exceptions\AnalyticsArgumentException;
5
use Illuminate\Foundation\Application;
6
use Illuminate\View\Factory as View;
7
8
abstract class AnalyticsBase implements AnalyticalBaseInterface
9
{
10
11
	const TYPE_PAGEVIEW = 'pageview';
12
	const TYPE_APPVIEW = 'appview';
13
	const TYPE_SCREENVIEW = 'screenview';
14
	const TYPE_EVENT = 'event';
15
	const TYPE_TRANSACTION = 'transaction';
16
	const TYPE_ITEM = 'item';
17
	const TYPE_SOCIAL = 'social';
18
	const TYPE_EXCEPTION = 'exception';
19
	const TYPE_TIMING = 'timing';
20
21
	const ECOMMERCE_TRANSACTION = 'addTransaction';
22
	const ECOMMERCE_ITEM = 'addItem';
23
24
	/**
25
	 * App
26
	 *
27
	 * @var Application
28
	 */
29
	protected $application;
30
31
	/**
32
	 * View
33
	 *
34
	 * @var View
35
	 */
36
	protected $view;
37
38
	/**
39
	 * Tracking enabled
40
	 *
41
	 * @var boolean
42
	 */
43
	protected $enabled;
44
45
	/**
46
	 * Google Analytics tracking id
47
	 *
48
	 * @var string
49
	 */
50
	protected $id;
51
52
	/**
53
	 * Domain
54
	 *
55
	 * @var string
56
	 */
57
	protected $domain;
58
59
	/**
60
	 * Anonymised tracking
61
	 *
62
	 * @var boolean
63
	 */
64
	protected $anonymised;
65
66
	/**
67
	 * Automatically track
68
	 *
69
	 * @var boolean
70
	 */
71
	protected $automatic;
72
73
	/**
74
	 * Tracking items
75
	 *
76
	 * @var array
77
	 */
78
	public $items = [];
79
80
	/**
81
	 * Construct Googlitics
82
	 *
83
	 * @param Application $application
84
	 * @param View        $view
85
	 * @param array       $options
86
	 *
87
	 * @throws AnalyticsArgumentException
88
	 *
89
	 * @return self
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
90
	 */
91
	public function __construct(Application $application, View $view, array $options = [])
92
	{
93
		$this->application = $application;
94
		$this->view = $view;
95
96
		if (!isset($options['id'])) {
97
			throw new AnalyticsArgumentException('Google Analytics tracking id is required.');
98
		}
99
100
		$this->setEnabled(isset($options['enabled']) ? $options['enabled'] : true);
101
		$this->setId($options['id']);
102
		$this->setDomain(isset($options['domain']) ? $options['domain'] : 'auto');
103
		$this->setAnonymised(isset($options['anonymise']) ? $options['anonymise'] : false);
104
		$this->setAutomatic(isset($options['automatic']) ? $options['automatic'] : false);
105
	}
106
107
	/**
108
	 * Set enabled status
109
	 *
110
	 * @param boolean $value
111
	 *
112
	 * @return void
113
	 */
114
	protected function setEnabled($value)
115
	{
116
		$this->enabled = $value;
117
	}
118
119
	/**
120
	 * Get the enabled status
121
	 *
122
	 * @return boolean
123
	 */
124
	protected function getEnabled()
125
	{
126
		return $this->enabled;
127
	}
128
129
	/**
130
	 * Set the Google Analytics id
131
	 *
132
	 * @param string $value
133
	 *
134
	 * @return void
135
	 */
136
	protected function setId($value)
137
	{
138
		$this->id = $value;
139
	}
140
141
	/**
142
	 * Get the Google Analytics id
143
	 *
144
	 * @return string
145
	 */
146
	protected function getId()
147
	{
148
		return $this->id;
149
	}
150
151
	/**
152
	 * Set the tracking domain
153
	 *
154
	 * @param string $value
155
	 *
156
	 * @return void
157
	 */
158
	protected function setDomain($value)
159
	{
160
		$this->domain = $value;
161
	}
162
163
	/**
164
	 * Get the tracking domain
165
	 *
166
	 * @return string
167
	 */
168
	protected function getDomain()
169
	{
170
		return $this->domain;
171
	}
172
173
	/**
174
	 * Set anonymised tracking
175
	 *
176
	 * @param boolean $value
177
	 *
178
	 * @return void
179
	 */
180
	protected function setAnonymised($value)
181
	{
182
		$this->anonymised = $value;
183
	}
184
185
	/**
186
	 * Get anonymised tracking
187
	 *
188
	 * @return boolean
189
	 */
190
	protected function getAnonymised()
191
	{
192
		return $this->anonymised;
193
	}
194
195
	/**
196
	 * Set automatic tracking
197
	 *
198
	 * @param boolean $value
199
	 *
200
	 * @return void
201
	 */
202
	protected function setAutomatic($value)
203
	{
204
		$this->automatic = $value;
205
	}
206
207
	/**
208
	 * Get automatic tracking
209
	 *
210
	 * @return boolean
211
	 */
212
	protected function getAutomatic()
213
	{
214
		return $this->automatic;
215
	}
216
217
	/**
218
	 * Add tracking item
219
	 *
220
	 * @param string $value
221
	 *
222
	 * @return void
223
	 */
224
	protected function addItem($value)
225
	{
226
		$this->items[] = $value;
227
	}
228
229
	/**
230
	 * Set tracking items
231
	 *
232
	 * @param array $array
233
	 *
234
	 * @return void
235
	 */
236
	protected function setItems(array $array)
237
	{
238
		$this->items = $array;
239
	}
240
241
	/**
242
	 * Get the tracking items
243
	 *
244
	 * @return array
245
	 */
246
	public function getItems()
247
	{
248
		return $this->items;
249
	}
250
251
	/**
252
	 * Is tracking enabled?
253
	 *
254
	 * @return boolean
255
	 */
256
	public function isEnabled()
257
	{
258
		return $this->getEnabled();
259
	}
260
261
	/**
262
	 * Enable tracking.
263
	 *
264
	 * @return void
265
	 */
266
	public function enableTracking()
267
	{
268
		$this->setEnabled(true);
269
	}
270
271
	/**
272
	 * Disable tracking.
273
	 *
274
	 * @return void
275
	 */
276
	public function disableTracking()
277
	{
278
		$this->setEnabled(false);
279
	}
280
281
	/**
282
	 * Is anonymised tracking enabled?
283
	 *
284
	 * @return boolean
285
	 */
286
	public function isAnonymised() {
287
		return $this->getAnonymised();
288
	}
289
290
	/**
291
	 * Enable anonymised tracking.
292
	 *
293
	 * @return void
294
	 */
295
	public function enableAnonymisedTracking()
296
	{
297
		$this->setAnonymised(true);
298
	}
299
300
	/**
301
	 * Disable anonymised tracking.
302
	 *
303
	 * @return void
304
	 */
305
	public function disableAnonymisedTracking()
306
	{
307
		$this->setAnonymised(false);
308
	}
309
310
	/**
311
	 * Is automatic tracking enabled?
312
	 *
313
	 * @return boolean
314
	 */
315
	public function isAutomatic()
316
	{
317
		return $this->getAutomatic();
318
	}
319
320
	/**
321
	 * Enable automatic tracking.
322
	 *
323
	 * @return void
324
	 */
325
	public function enableAutomaticTracking()
326
	{
327
		$this->setAutomatic(true);
328
	}
329
330
	/**
331
	 * Disable automatic tracking.
332
	 *
333
	 * @return void
334
	 */
335
	public function disableAutomaticTracking()
336
	{
337
		$this->setAutomatic(false);
338
	}
339
340
    /**
341
     * Track an ecommerce item.
342
     *
343
     * @param string $type
344
     * @param array  $options
345
     *
346
     * @return void
347
     */
348
	protected function trackEcommerce($type = self::ECOMMERCE_TRANSACTION, array $options = [])
349
	{
350
        $this->addItem("ga('require', 'ecommerce');");
351
        $this->addItem("ga('ecommerce:{$type}', " . @json_encode($options) . ");");
352
        $this->addItem("ga('ecommerce:send');");
353
	}
354
355
}