Skip to content

Instantly share code, notes, and snippets.

@zaneclaes
Last active August 29, 2015 14:08
Show Gist options
  • Save zaneclaes/06e9efdf7b5f2e3533b8 to your computer and use it in GitHub Desktop.
Save zaneclaes/06e9efdf7b5f2e3533b8 to your computer and use it in GitHub Desktop.
<?php
class GAN {
function __construct($id) {
$this->id = $id;
}
protected function gaParseCookie() {
if (isset($_COOKIE['_ga'])) {
list($version,$domainDepth, $cid1, $cid2) = preg_split('[\.]', $_COOKIE["_ga"],4);
$contents = array('version' => $version, 'domainDepth' => $domainDepth, 'cid' => $cid1.'.'.$cid2);
$cid = $contents['cid'];
}
else if(isset($_REQUEST['uuid'])) {
$cid = $_REQUEST['uuid'];
}
else $cid = $this->gaGenUUID();
return $cid;
}
// Generate UUID v4 function - needed to generate a CID when one isn't available
protected function gaGenUUID() {
return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),
// 16 bits for "time_mid"
mt_rand( 0, 0xffff ),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand( 0, 0x0fff ) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand( 0, 0x3fff ) | 0x8000,
// 48 bits for "node"
mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
);
}
// See https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide
protected function gaFireHit( $data = null, $campaign = null, $uid = null ) {
// Standard params
$data['v'] = 1;
$data['tid'] = $this->id; // Put your own Analytics ID in here
$data['cid'] = $this->gaParseCookie();
if($uid) {
$data['uid'] = $uid;
}
if($campaign) {
if($campaign['campaign']) {
$data['cn'] = $campaign['campaign'];
}
if($campaign['name']) {
$data['cn'] = $campaign['name'];
}
if($campaign['source']) {
$data['cs'] = $campaign['source'];
}
if($campaign['medium']) {
$data['cm'] = $campaign['medium'];
}
if($campaign['keyword']) {
$data['ck'] = $campaign['keyword'];
}
if($campaign['content']) {
$data['cc'] = $campaign['content'];
}
if($campaign['id']) {
$data['ci'] = $campaign['id'];
}
}
$getString = 'http://www.google-analytics.com/collect';
$getString .= '?payload_data&';
$getString .= http_build_query($data);
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, $getString);
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// $output contains the output string
$result = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
return $result;
}
//
// PAGEVIEW
//
public function pageview($params, $campaign=null, $uid = null) {
return $this->gaFireHit(array(
't' => 'pageview',
'dt' => isset($params['title']) ? $params['title'] : "",
'dp' => $params['page']
), $campaign, $uid);
}
//
// EVENT
//
public function event($params, $campaign=null, $uid = null) {
return $this->gaFireHit(array(
't' => 'event',
'ec' => $params['category'],
'ea' => $params['action'],
'el' => isset($params['label']) ? $params['label'] : "",
'ev' => isset($params['value']) ? $params['value'] : ""
), $campaign, $uid);
}
//
// TIMING
//
public function timing($params, $campaign=null, $uid = null) {
return $this->gaFireHit(array(
't' => 'timing',
'utc' => $params['category'],
'utl' => isset($params['label']) ? $params['label'] : "",
'utt' => isset($params['time']) ? $params['time'] : 0,
'utv' => isset($params['variable']) ? $params['variable'] : ""
), $campaign, $uid);
}
//
// TRANSACTION
//
public function transaction($params, $campaign=null, $uid = null) {
return $this->gaFireHit(array(
't' => 'transaction',
'ti' => $params['transactionID'],
'ta' => isset($params['affiliation']) ? $params['affiliation'] : "",
'tr' => isset($params['revenue']) ? $params['revenue'] : 0,
'cu' => isset($params['currency']) ? $params['currency'] : "USD"
), $campaign, $uid);
}
//
// ECOMMERCE
//
public function ecommerce() {
// Set up Transaction params
$ti = uniqid(); // Transaction ID
$ta = 'SI';
$tr = $info['price']; // transaction value (native currency)
$cu = $info['cc']; // currency code
// Send Transaction hit
$data = array(
't' => 'transaction',
'ti' => $ti,
'ta' => $ta,
'tr' => $tr,
'cu' => $cu
);
$this->gaFireHit($data);
// Set up Item params
$in = urlencode($info['info']->product_name); // item name;
$ip = $tr;
$iq = 1;
$ic = urlencode($info['info']->product_id); // item SKU
$iv = urlencode('SI'); // Product Category - we use 'SI' in all cases, you may not want to
// Send Item hit
$data = array(
't' => 'item',
'ti' => $ti,
'in' => $in,
'ip' => $ip,
'iq' => $iq,
'ic' => $ic,
'iv' => $iv,
'cu' => $cu
);
return $this->gaFireHit($data);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment