-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssga.class.php
233 lines (175 loc) · 5.17 KB
/
ssga.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
class ssga {
const GA_URL = 'https://www.google-analytics.com/collect';
private $data = array(
'v' => 1,
'tid' => '', //Tracking ID / Property ID.
'aip' => true, //boolean, anonymize ip
'ds' => '', //data source, text
'z' => null //Cache Buster, number
'cid' => null, //Client ID, text
'uid' => null, //User ID, text
't' => 'pageview',
'dp' => 'home.html'
);
private $tracking;
public function __construct( $UA = null, $aip = true ) {
$this->data['tid'] = $UA;
$this->data['aip'] = $aip;
$this->data['z'] = rand( 1000000000, 9999999999 );
$this->data['cid'] = $this->gen_uuid();
}
/**
* Generate client id
**/
function gen_uuid() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
/**
* Use CURL
* @return array|null
*/
private function send() {
$content = http_build_query($this->data);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'http://www.google-analytics.com/collect?'.$content,
CURLOPT_USERAGENT => 'Vanity-URL-Tracker',
));
curl_exec( $ch );
curl_close( $ch );
return;
}
/////////////
// Product //
/////////////
public function set_product_code($index = 1, $var = null ) {
return $this->data['pr'.$index.'id'] = $var;
}
public function set_product_name($index = 1, $var = null ) {
return $this->data['pr'.$index.'nm'] = $var;
}
public function set_unit_price($index = 1, $var = null ) {
return $this->data['pr'.$index.'pr'] = $var;
}
public function set_qty($index = 1, $var = null ) {
return $this->data['pr'.$index.'qt'] = $var;
}
public function set_variation($index = 1, $var = null ) {
return $this->data['pr'.$index.'va'] = $var;
}
//////////
// Misc //
//////////
public function set_java( $var = null ) {
return $this->data['je'] = $var;
}
public function set_encode_type( $var = null ) {
return $this->data['de'] = $var;
}
public function set_flash_version( $var = null ) {
return $this->data['fl'] = $var;
}
public function set_host( $var = null ) {
return $this->data['hn'] = $var;
}
public function set_screen_depth( $var = null ) {
return $this->data['sc'] = $var;
}
public function set_screen_resolution( $var = null ) {
return $this->data['sr'] = $var;
}
public function set_lang( $var = null ) {
return $this->data['ul'] = $var;
}
public function set_ga_version( $var = null ) {
return $this->data['wv'] = isset( $var ) ? $var : $this->data['wv'];
}
//////////
// Page //
//////////
public function set_page( $var = null ) {
return $this->data['dp'] = $var;
}
public function set_page_title( $var = null ) {
return $this->data['dt'] = $var;
}
public function set_doc_url( $var=null ) {
return $this->data['dl'] = $var;
}
public function set_host_name( $var=null ) {
return $this->data['dh'] = $var;
}
public function set_title( $var = null ) {
return $this->data['dt'] = $var;
}
////////////
// Events //
////////////
public function send_event( $category, $action, $label = '', $value = '') {
$event_category = (string) $category;
$event_action = (string) $action;
$this->data['ec']=$event_category // Event Category. Required.
$this->data['ea']=$event_action // Event Action. Required.
$this->data['el']=$label // Event label.
$this->data['ev']=$value // Event value.
$this->data['t'] = 'event';
$this->send();
return $this;
}
////////////////////////
// Ecommerce Tracking //
////////////////////////
private static $requests_for_this_session = 0;
/**
* Create and send a transaction object
*
* Parameter order from https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#ecom
*/
public function send_transaction($transaction_id, $affiliation, $total, $tax, $shipping, $curency) {
$this->data['utmt'] = 'transaction';
$this->data['ti'] = $transaction_id;
$this->data['ta'] = $affiliation;
$this->data['tr'] = $total;
$this->data['tt'] = $tax;
$this->data['ts'] = $shipping;
$this->data['cu'] = $curency;
$this->send();
return $this;
}
/**
* Add item to the created $transaction_id
*
* Parameter order from https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#ecom
*/
public function send_item($transaction_id, $sku, $product_name, $variation, $unit_price, $quantity) {
$this->data['utmt'] = 'item';
$this->data['ti'] = $transaction_id;
$this->data['ic'] = $sku;
$this->data['in'] = $product_name;
$this->data['iv'] = $variation;
$this->data['ip'] = $unit_price;
$this->data['iq'] = $quantity;
$this->send();
return $this;
}
}
/**
* Instantiate new class and push data
* @param string $UA The UA string of the GA account to use
* @param string $domain domain
* @param string $page the page to set the pageview
* @return null
*/
function ssga_track( $UA = null, $domain = null, $page = null ) {
$ssga = new ssga( $UA, $domain );
$ssga->set_page( $page );
$ssga->send();
return $ssga;
}