Skip to content

Instantly share code, notes, and snippets.

@vdite
Last active April 19, 2017 22:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdite/ccd50d12ce3c22af9094e54d0a58d5ad to your computer and use it in GitHub Desktop.
Save vdite/ccd50d12ce3c22af9094e54d0a58d5ad to your computer and use it in GitHub Desktop.
This Script ist for Offline Conversion Tracking in Google Anylytics. Contact: http://mizine.de
<?php
/**
This Script ist for Offline Conversion Tracking in Google Anylytics
Author: Viktor Dite
Contact: http://mizine.de
Version: v0.2
**/
$uaid = 'UA-XXXXXXX-X'; // Your Analytics UAID
$amount = $_GET['amount']; // Conversion Value
$uid = $_GET['uid']; // Unique Transaction ID (i.e. Invoice Nr.)
$lstnm = $_GET['LocalStoreName']; // Which Store did the Conversion (Name)
$tcc = $_GET['tcc']; // Coupon Code
//PHP triggered Conversion in Google Analytics
$conversion = exec_url('https://www.google-analytics.com/collect?v=1&t=transaction&tid='.$uaid.'&cid='.gen_uuid().'&ti='.$uid.'&ta=LocalStore&tr='.$amount.'&tcc='.$tcc.'&cu=EUR&dh=mizine.de');
//PHP triggered Event in Google Analytics
$event = exec_url('https://www.google-analytics.com/collect?v=1&t=event&tid='.$uaid.'&cid='.gen_uuid().'&ti='.$uid.'&ec=LocalConversion&ea=Purchase&el='.$lstnm.'&ev='.$amount);
function exec_url($url){
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_USERAGENT => 'MiZineTracker/1.0 (http://mizine.de/)',
CURLOPT_HTTPHEADER => array('Content-type: application/x-www-form-urlencoded'),
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
));
$resp = curl_exec($curl);
curl_close($curl);
return $resp;
}
// Generates a UUID. A UUID is required for the measurement protocol.
// http://php.net/manual/de/function.uniqid.php#94959
function gen_uuid() {
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 )
);
}
?>
@vdite
Copy link
Author

vdite commented Apr 19, 2017

@vdite
Copy link
Author

vdite commented Apr 19, 2017

v.0.2
Added tcc Parameter for Tracking Coupon Codes from i.e. Google Adwords

http://your-domain/atrack.php?LocalStoreName=MyStoreInNY&uid=346542&amount=2500&tcc=SUMMER2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment