These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Apix\Log; |
||
4 | |||
5 | use Apix\Log\Emitter\EmitterInterface as LogEmitter; |
||
6 | use Psr\Log\InvalidArgumentException; |
||
7 | |||
8 | /* |
||
9 | TODO: |
||
10 | A maximum of 20 hits can be specified per request. |
||
11 | The total size of all hit payloads cannot be greater than 16K bytes. |
||
12 | No single hit payload can be greater than 8K bytes. |
||
13 | */ |
||
14 | |||
15 | /** |
||
16 | * Google Analytics logger for Apix Log. |
||
17 | * |
||
18 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide |
||
19 | * @see https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters |
||
20 | */ |
||
21 | class GoogleAnalytics extends AbstractTracker |
||
22 | { |
||
23 | const TRACKER_URL_ONE = 'https://www.google-analytics.com/collect'; |
||
24 | |||
25 | const TRACKER_URL_MANY = 'https://www.google-analytics.com/batch'; |
||
26 | |||
27 | //const DEFAULT_PARAMS = array( |
||
0 ignored issues
–
show
|
|||
28 | private $DEFAULT_PARAMS = array( |
||
29 | 'v' => 1, // API Version |
||
30 | 'tid' => null, // Tracking/Property (required) ID e.g. UA-XX-XX |
||
31 | 'cid' => null, // Anonymous Client ID UUIDv4 |
||
32 | // see http://www.ietf.org/rfc/rfc4122.txt |
||
33 | 'ds' => __NAMESPACE__, // Data Source |
||
34 | 't' => null, // Hit type (required) |
||
35 | ); |
||
36 | |||
37 | /** |
||
38 | * Constructor. |
||
39 | * |
||
40 | * @param array $params Array of Google Analytics parameters |
||
41 | */ |
||
42 | 44 | public function __construct( |
|
0 ignored issues
–
show
__construct uses the super-global variable $_SERVER which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
Loading history...
|
|||
43 | array $params, LogEmitter $emitter = null, LogFormatter $formatter = null |
||
44 | ) { |
||
45 | 44 | if (!isset($params['tid'])) { |
|
46 | 4 | throw new InvalidArgumentException(sprintf( |
|
47 | 4 | '%s expects `tid` to bet provided, got: %s.', |
|
48 | 4 | __CLASS__, json_encode($params) |
|
49 | 3 | )); |
|
50 | } |
||
51 | |||
52 | 44 | if (!isset($params['cid'])) { |
|
53 | $params['cid'] = self::generateUuid(); |
||
54 | } |
||
55 | 44 | $this->uuid = $params['cid']; |
|
56 | |||
57 | 44 | $this->setemitter( |
|
58 | 44 | $emitter ? $emitter : new Emitter\Async(), |
|
59 | 44 | $formatter ? $formatter : new LogFormatter\QueryString() |
|
60 | 33 | ); |
|
61 | 44 | $this->emitter->setParams($this->DEFAULT_PARAMS); |
|
62 | |||
63 | 44 | if (isset($_SERVER['HTTP_USER_AGENT']) && !isset($params['ua'])) { |
|
64 | 4 | $params['ua'] = $_SERVER['HTTP_USER_AGENT']; |
|
65 | 3 | } |
|
66 | |||
67 | 44 | if (isset($_SERVER['HTTP_REFERER']) && !isset($params['dr'])) { |
|
68 | 4 | $params['dr'] = $_SERVER['HTTP_REFERER']; |
|
69 | 3 | } |
|
70 | |||
71 | 44 | if (isset($_SERVER['REMOTE_ADDR']) && !isset($params['uip'])) { |
|
72 | $params['uip'] = $_SERVER['REMOTE_ADDR']; |
||
73 | } |
||
74 | |||
75 | 44 | $this->emitter->addParams($params); |
|
76 | 44 | } |
|
77 | |||
78 | /** |
||
79 | * Returns a Page Tracking dataset. |
||
80 | * |
||
81 | * @param string $url The full URL for ht page document |
||
82 | * @param string $title The title of the page / document |
||
83 | * @param string $location Document location URL |
||
84 | * |
||
85 | * @return array |
||
86 | */ |
||
87 | 12 | public function getPage($url, $title = null, $location = null) |
|
88 | { |
||
89 | 12 | $params = array(); |
|
90 | |||
91 | 12 | if (0 != strpos($url, '/')) { |
|
92 | 12 | $_ = parse_url($url); |
|
93 | |||
94 | // Document hostname |
||
95 | 12 | if (isset($_['host'])) { |
|
96 | 12 | $params['dh'] = $_['host']; |
|
97 | 9 | } |
|
98 | |||
99 | // Page |
||
100 | 12 | $params['dp'] = $_['path']; |
|
101 | 9 | } |
|
102 | |||
103 | // Page title |
||
104 | 12 | if ($title) { |
|
105 | 12 | $params['dt'] = $title; |
|
106 | 9 | } |
|
107 | |||
108 | // Document location URL |
||
109 | 12 | $params['dl'] = $location ? $location : $url; |
|
110 | |||
111 | 12 | return $this->get('pageview', $params); |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * Returns an Event Tracking dataset. |
||
116 | * |
||
117 | * @param string $category |
||
118 | * @param string $action |
||
119 | * @param string $label |
||
120 | * @param string $value |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | 12 | public function getEvent($category, $action, $label = null, $value = null) |
|
125 | { |
||
126 | $params = array( |
||
127 | 12 | 'ec' => $category, // Event Category. Required. |
|
128 | 12 | 'ea' => $action, // Event Action. Required. |
|
129 | 9 | ); |
|
130 | |||
131 | // Event label |
||
132 | 12 | if ($label) { |
|
133 | 12 | $params['el'] = (string) $label; |
|
134 | 9 | } |
|
135 | |||
136 | // Event value |
||
137 | 12 | if ($value) { |
|
138 | 12 | $params['ev'] = (int) $value; // GA does not allow float! |
|
139 | 9 | } |
|
140 | |||
141 | 12 | return $this->get('event', $params); |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Returns a Social Interactions dataset. |
||
146 | * |
||
147 | * @param string $action Social Action (e.g. like) |
||
148 | * @param string $label Social Network (e.g. facebook) |
||
149 | * @param string $value Social Target. (e.g. /home) |
||
150 | * |
||
151 | * @return array |
||
152 | */ |
||
153 | 4 | public function getSocial($action, $network, $target) |
|
154 | { |
||
155 | $params = array( |
||
156 | 4 | 'sa' => (string) $action, |
|
157 | 4 | 'sn' => (string) $network, |
|
158 | 4 | 'st' => (string) $target, |
|
159 | 3 | ); |
|
160 | |||
161 | 4 | return $this->get('social', $params); |
|
162 | } |
||
163 | |||
164 | /** |
||
165 | * Returns an Exception Tracking dataset. |
||
166 | * |
||
167 | * @param string $description Exception description |
||
168 | * @param string $isFatal Specifies whether the exception was fatal |
||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | 4 | public function getException($description, $isFatal = true) |
|
173 | { |
||
174 | $params = array( |
||
175 | 4 | 'exd' => (string) $description, |
|
176 | 4 | 'exf' => $isFatal ? '1' : '0', |
|
177 | 3 | ); |
|
178 | |||
179 | 4 | return $this->get('exception', $params); |
|
180 | } |
||
181 | |||
182 | /** |
||
183 | * Returns an App / Screen Tracking dataset. |
||
184 | * |
||
185 | * @param string $name App name |
||
186 | * @param string $version App version |
||
187 | * @param string $id App Id |
||
188 | * @param string $iid App Installer Id |
||
189 | * |
||
190 | * @return array |
||
191 | */ |
||
192 | 4 | public function getApp($name, $version = null, $id = null, $iid = null) |
|
193 | { |
||
194 | $params = array( |
||
195 | 4 | 'an' => (string) $name, |
|
196 | 4 | 'av' => (string) $version, |
|
197 | 4 | 'aid' => (string) $id, |
|
198 | 4 | 'aiid' => (string) $iid, |
|
199 | 3 | ); |
|
200 | |||
201 | 4 | return $this->get('screenview', $params); |
|
202 | } |
||
203 | |||
204 | /** |
||
205 | * Returns the named tracking dataset. |
||
206 | * |
||
207 | * @return array |
||
208 | */ |
||
209 | 32 | public function get($type, array $params) |
|
210 | { |
||
211 | 32 | $this->emitter->setParam('t', $type); |
|
212 | 32 | $this->emitter->setUrl( |
|
213 | 32 | $this->deferred ? self::TRACKER_URL_MANY : self::TRACKER_URL_ONE |
|
214 | 24 | ); |
|
215 | |||
216 | 32 | return array_merge($this->emitter->getParams(), $params); |
|
217 | } |
||
218 | } |
||
219 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.