Track full order datails with Google Analytics and eSellerate (or Plimus, BMT, etc.)
13 11 2007You can have Google Analytics anabled and working great, but if your e-commerce service is located on 3rd party domain, then you lose a lot of important informations, such as referral site, visits to purchase, days to purchase, etc. If you use the trick described below you will be able to track all this useful informations. I made it to work with eSellerate, but I am sure it can be done with Plimus, BMT and other e-commerce providers.
First of all, all your buy now links have to call a script that will save Google Analytics cookies to database. To match visitors with cookies we will use visitor IP. The script that will set the cookies on e-commerce domain will delete database entry right after retrieving it. It is not a perfect solution, but it will work in 99.999% (how many orders do you expect placed within few seconds from the same IP?). So instaed of calling:
store.esellerate.net/s.asp?s=STR4587075495&Cmd=BUY&SKURefnum=SKU41198308198
you call
www.yourdamain.com/buy/mygame.php
Mygame.php has to include save_GA_cookies.php script and then redirect to real e-commerce address. save_GA_cookies.php script looks like this:
<?
// this script should be called before you redirect to e-commerce shopping cart
// it saves Google Analytics cookies to database along with User IP// include your database engine
include(“db.php”)$dbga = new clsDBAnawiki();$IP = $_ENV[‘REMOTE_ADDR’];
$utma = mysql_escape_string($_COOKIE[‘__utma’]);
$utmb = mysql_escape_string($_COOKIE[‘__utmb’]);
$utmc = mysql_escape_string($_COOKIE[‘__utmc’]);
$utmx = mysql_escape_string($_COOKIE[‘__utmx’]);
$utmz = mysql_escape_string($_COOKIE[‘__utmz’]);
$utmv = mysql_escape_string($_COOKIE[‘__utmv’]);
$utmk = mysql_escape_string($_COOKIE[‘__utmk’]);$dbga->query(“INSERT INTO an_ga_cookies(ga_ip, ga_utma, ga_utmb, ga_utmc, ga_utmx, ga_utmz, ga_utmv, ga_utmk) VALUES(‘$IP’, ‘$utma’, ‘$utmb’, ‘$utmc’, ‘$utmx’, ‘$utmz’, ‘$utmv’, ‘$utmk’)”);
$dbga->close();?>
Then you need to insert into your e-commerce html template following line:
<script TYPE=”text/javascript” src=”https://www.yourSSLdomain.com/buy/set_GA_cookies.php” mce_src=”https://www.yourSSLdomain.com/buy/set_GA_cookies.php”>
</script>
set_GA_cookies.php reads GA cookies from database and sets them on e-commerce domain via JavaScript. The problem is that you need a SSL domain address otherwise your shopping cart will show unsecure site message. The good news is that this domain doesn’t have to be on your server or your own domain, though you will need to tweak my code to make it work on 3rd party server.
Here is the code for set_GA_cookies.php:
<?
// script generates JavaScript code that sets Google Analytic cookies on eSellerate.net// include your own database library
include(“db.php”);$IP = $_ENV[‘REMOTE_ADDR’];
$db = new DB();$db->query(“SELECT * FROM an_ga_cookies WHERE ga_ip=’$IP’ LIMIT 0, 1″);
$jscode = ”;
while ($db->next_record()) {
$utma = $db->f(‘ga_utma’);
$utmb = $db->f(‘ga_utmb’);
$utmc = $db->f(‘ga_utmc’);
$utmx = $db->f(‘ga_utmx’);
$utmz = $db->f(‘ga_utmz’);
$utmv = $db->f(‘ga_utmv’);
$utmk = $db->f(‘ga_utmk’);
$id = $db->f(‘ga_id’);$jscode = “setCookie(‘__utma’, ‘”.htmlspecialchars($utma).”‘, 30, ‘/’, ‘.esellerate.net’, false) \n”;
$jscode .= “setCookie(‘__utmb’, ‘”.htmlspecialchars($utmb).”‘, 30, ‘/’, ‘.esellerate.net’, false) \n”;
$jscode .= “setCookie(‘__utmc’, ‘”.htmlspecialchars($utmc).”‘, 30, ‘/’, ‘.esellerate.net’, false) \n”;
$jscode .= “setCookie(‘__utmx’, ‘”.htmlspecialchars($utmx).”‘, 30, ‘/’, ‘.esellerate.net’, false) \n”;
$jscode .= “setCookie(‘__utmz’, ‘”.htmlspecialchars($utmz).”‘, 30, ‘/’, ‘.esellerate.net’, false) \n”;
$jscode .= “setCookie(‘__utmv’, ‘”.htmlspecialchars($utmv).”‘, 30, ‘/’, ‘.esellerate.net’, false) \n”;
$jscode .= “setCookie(‘__utmk’, ‘”.htmlspecialchars($utmk).”‘, 30, ‘/’, ‘.esellerate.net’, false) \n”;}
if (!empty($id)) $db->query(“DELETE FROM an_ga_cookies WHERE ga_id=$id”);
?>
function setCookie(name, value, expires, path, domain, secure){
document.cookie=name+’=’+unescape(value||”)+
(expires?';expires=’+new Date(+new Date()+expires*864e5).toGMTString():”)+
(path?';path=’+path:”)+
(domain?';domain=’+domain:”)+
(secure?';secure':”);
}
<? echo $jscode; ?>
That’s it. I used PHP, but you can use any other web language to do that. Commands are self-explanatory, so I am sure you’ll be fine. Just in case you don’t want to copy the code from this page you can download zipped scripts.
Please rate my article: