Completed
Push — master ( 42db1a...85f3f0 )
by Bradley
10:34 queued 08:14
created

Analytics::trackEvent()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.2
cc 4
eloc 6
nc 8
nop 4
1
<?php namespace Cornford\Googlitics;
2
3
use Cornford\Googlitics\Contracts\AnalyticalInterface;
4
use Cornford\Googlitics\Exceptions\AnalyticsArgumentException;
5
6
class Analytics extends AnalyticsBase implements AnalyticalInterface {
7
8
	/**
9
	 * Renders and returns Google Analytics code.
10
	 *
11
	 * @return string
12
	 */
13
	public function render()
14
	{
15
		if (!$this->isEnabled()) {
16
			return;
17
		}
18
19
		if ($this->isAutomatic()) {
20
			$this->addItem("ga('send', '" . self::TYPE_PAGEVIEW . "');");
21
		}
22
23
		if ($this->isAnonymised()) {
24
			$this->addItem("ga('set', 'anonymizeIp', true);");
25
		}
26
27
		if ($this->application->environment() === 'dev') {
28
			$this->addItem("ga('create', '{$this->id}', { 'cookieDomain': 'none' });");
29
		} else {
30
			$this->addItem("ga('create', '{$this->id}', '{$this->domain}');");
31
		}
32
33
		return $this->view->make('googlitics::analytics')->withItems(array_reverse($this->getItems()))->render();
34
	}
35
36
	/**
37
	 * Track a page view.
38
	 *
39
	 * @param string $page
40
	 * @param string $title
41
	 * @param string $type
42
	 *
43
	 * @throws AnalyticsArgumentException
44
	 *
45
	 * @return void
46
	 */
47
	public function trackPage($page = null, $title = null, $type = self::TYPE_PAGEVIEW)
48
	{
49
		if (!defined('self::TYPE_'. strtoupper($type))) {
50
			throw new AnalyticsArgumentException('Type variable can\'t be of this type.');
51
		}
52
53
		$item = "ga('send', 'pageview');";
54
55
		if ($page !== null || $title !== null) {
56
			$page = ($page === null ? "window.location.href" : "'{$page}'");
57
			$title = ($title === null ? "document.title" : "'{$title}'");
58
			$item = "ga('send', { 'hitType': '{$type}', 'page': {$page}, 'title': {$title} });";
59
		}
60
61
		$this->addItem($item);
62
	}
63
64
	/**
65
	 * Track a screen view.
66
	 *
67
	 * @param string $name
68
	 *
69
	 * @throws AnalyticsArgumentException
70
	 *
71
	 * @return void
72
	 */
73
	public function trackScreen($name)
74
	{
75
		$item = "ga('send', '" . self::TYPE_SCREENVIEW . "', { 'screenName': '{$name}' });";
76
		$this->addItem($item);
77
	}
78
79
	/**
80
	 * Track an event.
81
	 *
82
	 * @param string  $category
83
	 * @param string  $action
84
	 * @param string  $label
85
	 * @param integer $value
86
	 *
87
	 * @return void
88
	 */
89
	public function trackEvent($category, $action, $label = null, $value = null)
90
	{
91
		$item = "ga('send', 'event', '{$category}', '{$action}'" .
92
			($label !== null ? ", '{$label}'" : '') .
93
			($value !== null && is_numeric($value) ? ", {$value}" : '') .
94
			");";
95
		$this->addItem($item);
96
	}
97
98
	/**
99
	 * Track a transaction.
100
	 *
101
	 * @param string $id
102
	 * @param array  $options (affiliation|revenue|shipping|tax)
103
	 *
104
	 * @return void
105
	 */
106
	public function trackTransaction($id, array $options = [])
107
	{
108
		$options['id'] = $id;
109
		$this->trackEcommerce(self::ECOMMERCE_TRANSACTION, $options);
110
	}
111
112
	/**
113
	 * Track a transaction item.
114
	 *
115
	 * @param string $id
116
	 * @param string $name
117
	 * @param array  $options (sku|category|price|quantity)
118
	 *
119
	 * @return void
120
	 */
121
	public function trackItem($id, $name, array $options = [])
122
	{
123
		$options['id'] = $id;
124
		$options['name'] = $name;
125
		$this->trackEcommerce(self::ECOMMERCE_ITEM, $options);
126
	}
127
128
	/**
129
	 * Track a metric.
130
	 *
131
	 * @param string $category
132
	 * @param array  $options
133
	 *
134
	 * @return void
135
	 */
136
	public function trackMetric($category, array $options = [])
137
	{
138
		$item = "ga('send', 'event', '{$category}'";
139
140
		if (!empty($options)) {
141
			$item .= ", 'action', { ";
142
143
			foreach ($options as $key => $value) {
144
				$item .= "'{$key}': {$value}, ";
145
			}
146
147
			$item = rtrim($item, ', ') . " }";
148
		}
149
150
		$item .= ");";
151
		$this->addItem($item);
152
	}
153
154
	/**
155
	 * Track an exception.
156
	 *
157
	 * @param string  $description
158
	 * @param boolean $fatal
159
	 *
160
	 * @return void
161
	 */
162
	public function trackException($description = null, $fatal = false)
163
	{
164
		$item = "ga('send', '" . self::TYPE_EXCEPTION . "'";
165
166
		if ($description !== null && is_bool($fatal)) {
167
			$item .= ", { " .
168
				"'exDescription': '{$description}', " .
169
				"'exFatal': " . ($fatal ? 'true' : 'false') .
170
				" }";
171
		}
172
173
		$item .= ");";
174
175
		$this->addItem($item);
176
	}
177
178
	/**
179
	 * Track a custom event.
180
	 *
181
	 * @param string $item
182
	 *
183
	 * @return void
184
	 */
185
	public function trackCustom($item)
186
	{
187
		$this->addItem($item);
188
	}
189
190
}
191