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 (count($options)) { |
141
|
|
|
$item .= ", 'action', " . @json_encode($options); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$item .= ");"; |
145
|
|
|
$this->addItem($item); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Track an exception. |
150
|
|
|
* |
151
|
|
|
* @param string $description |
152
|
|
|
* @param boolean $fatal |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function trackException($description = null, $fatal = false) |
157
|
|
|
{ |
158
|
|
|
$item = "ga('send', '" . self::TYPE_EXCEPTION . "'"; |
159
|
|
|
|
160
|
|
|
if ($description !== null && is_bool($fatal)) { |
161
|
|
|
$item .= ", { " . |
162
|
|
|
"'exDescription': '{$description}', " . |
163
|
|
|
"'exFatal': " . ($fatal ? 'true' : 'false') . |
164
|
|
|
" }"; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
$item .= ");"; |
168
|
|
|
|
169
|
|
|
$this->addItem($item); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Track a custom event. |
174
|
|
|
* |
175
|
|
|
* @param string $item |
176
|
|
|
* |
177
|
|
|
* @return void |
178
|
|
|
*/ |
179
|
|
|
public function trackCustom($item) |
180
|
|
|
{ |
181
|
|
|
$this->addItem($item); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.