Google e-commerce tracking modification for Tradingeye and UK2.net ecommerce package

November 16th, 2008

his modification is for Tradingeye accessible e-commerce system and the latest UK2.net E-commerce package to help you track visitor movement through checkout pages. To get better understanding and the benefit of this feature, please refer to these 2 article as below:

google ecommerce tracking code for Tradingeye and Uk2 ecommerce

1. Google Ecommerce Tracking Code

2. Analytics Talk

Now, please follow these steps

Step 1: Enabling E-Commerce Reporting in the Profile

The first step of tracking e-commerce transactions is to enable e-commerce reporting for your website’s profile. To enable e-commerce reporting, please follow these steps:

Log in to your account.
Click Edit next to the profile you’d like to enable.
On the Profile Settings page, click edit next to Main Website Profile Information.
Change the E-Commerce Website radio button from No to Yes.

Step 2: Receipt Page Format

Tradingeye and UK2.net payment process is slightly different from others. Once customer completed their transaction, he will be taken to an acknowlegdement (thank-you)  page rather than a receipt page. He then has to click on the link “view your receipt” to see his receipt. Thus, we need to modifify these following files:

/modules/ecom/templates/main/orderProcessed.tpl.htm
/modules/ecom/classes/main/receipt.php

Lets get the ball rolling!

2.1: Place this code below at the bottom of the file orderProcessed.tpl.htm

<pre lang=”html”><!– BEGIN TPL_GOOGLE_ECOMMERCE_TRACKING_BLOCK –>
<script type=”text/javascript”><!–

var gaJsHost = ((“https:” == document.location.protocol ) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));

// –></script>

<script type=”text/javascript”><!–
var pageTracker = _gat._getTracker(“UA-4440573-2″);
pageTracker._initData();
pageTracker._addTrans(
“{TPL_VAR_ORDER_ID}”, // order ID – required
“{TP_VAR_STORE_NAME}”, // affiliation or store name
“{TPL_VAR_TOTAL}”, // total – required
“{TPL_VAR_TAX_TOTAL}”, // tax
“{TPL_VAR_SHIPPING_COST}”, // shipping
“{TPL_VAR_CITY}”, // city
“{TPL_VAR_COUNTY}”, // state or province
“{TPL_VAR_COUNTRY}” // country
);

// add items might be called for every item in the shopping cart
<! BEGIN TPL_GOOGLE_ADDITEM_BLK –>
pageTracker._addItem(
“{TPL_VAR_ORDER_ID}”, // order ID – required
“{TPL_VAR_SKU_CODE}”, // SKU/code
“{TPL_VAR_PRODUCT_TITLE}”, // product name
“{TPL_VAR_CATEGORY}”, // category or variation
“{TPL_VAR_PRODUCT_PRICE}”, // unit price – required
“{TPL_VAR_QUANTITY}” // quantity – required
);
<!– END TPL_GOOGLE_ADDITEM_BLK –>
pageTracker._trackTrans(); //submits order info in hidden form after page load
// –></script>
<!– END TPL_GOOGLE_ECOMMERCE_TRACKING_BLOCK –></pre>

You need to replace this “UA-xxxxxx-xx” to your own Google Tracking ID

2.2:  Modify the receipt.php class.

Under the method (function) m_orderProcessed()

At Line 47, Look for:


<pre lang=”php”>

#DECLARE BLOCKS $this->ObTpl->set_block(“TPL_ORDER_FILE”,”TPL_ORDERSTATUS_BLK”,”orderstatus_blk”);

$this->ObTpl->set_block(“TPL_ORDER_FILE”,”TPL_BACKORDER_BLK”,”backorder_blk”);

<pre>

And add:

<pre lang=”php”>

$this->ObTpl->set_block(“TPL_ORDER_FILE”,”TPL_GOOGLE_ECOMMERCE_TRACKING_BLOCK”,”google_blk”);

$this->ObTpl->set_block(“TPL_GOOGLE_ECOMMERCE_TRACKING_BLOCK”,”TPL_GOOGLE_ADDITEM_BLK”,”item_blk”);

$this->ObTpl->set_var(“google_blk”,”");
$this->ObTpl->set_var(“item_blk”,”");

</pre>


Look for this comment

#FLAG TO INDICATE SEPERATE BACKORDER AND NORMAL ORDER

and replace all the code from there to

<pre lang=”php”>$this->ObTpl->set_var(“TPL_VAR_BACKORDERURL”,$backOrderUrl);</pre>

by the code below:

<pre lang=”php”>#FLAG TO INDICATE SEPERATE BACKORDER AND NORMAL ORDER
$_SESSION['backOrderSeperate']=$this->libFunc->ifSet($_SESSION,’backOrderSeperate’,'0′);
$this->obDb->query = “SELECT count(*) as cnt  FROM “.TEMPCART.” WHERE  vSessionId=’”.$this->sessionId.”‘”;
$rowCount=$this->obDb->fetchQuery();

if($_SESSION['backOrderSeperate']==1 && $rowCount[0]->cnt>0)
{
$this->ObTpl->parse(“backorder_blk”,”TPL_BACKORDER_BLK”);
}
else
{

#GETTING INVOICE DETAIL AND ADD TO GOOGLE ECOMMERCE API
$this->obDb->query = “SELECT tmOrderDate,vPayMethod,vShipDescription,fShipTotal,”;
$this->obDb->query.= “vFirstName,vLastName,vEmail,vAddress1,vAddress2,vCity,iInvoice,”;
$this->obDb->query.= “vState,vStateName,vCountry,vZip,vCompany,vPhone,vHomepage,”;
$this->obDb->query.= “vAltPhone,fCodCharge,fPromoValue,”;
$this->obDb->query.= “vDiscountCode,fDiscount,iGiftcert_FK,fGiftcertTotal,fMemberPoints,”;
$this->obDb->query.= “fTaxRate,fTaxPrice,tComments,vStatus,iPayStatus,fTotalPrice,iEarnedPoints,iCustomerid_FK”;
$this->obDb->query .= ” FROM “.ORDERS.” WHERE iOrderid_PK=’”.$this->request['mode'].”‘”;

$qryResult = $this->obDb->fetchQuery();
$rCount=$this->obDb->record_count;
if($rCount!=1)
{
$errrorUrl=SITE_URL.”index.php?action=error&mode=order”;
$this->libFunc->m_mosRedirect($this->libFunc->m_safeUrl($errrorUrl));
}

#PARSING VALUE TO TEMPLATE VARIABLE
$this->ObTpl->set_var(“TPL_VAR_ORDER_ID”,$this->libFunc->m_displayContent($qryResult[0]->iInvoice));
$this->ObTpl->set_var(“TP_VAR_STORE_NAME”,SITE_NAME);
$this->ObTpl->set_var(“TPL_VAR_TOTAL”,$qryResult[0]->fTotalPrice);
$this->ObTpl->set_var(“TPL_VAR_TAX_TOTAL”,0);
$this->ObTpl->set_var(“TPL_VAR_SHIPPING_COST”,$qryResult[0]->fShipTotal);
$this->ObTpl->set_var(“TPL_VAR_CITY”,$this->libFunc->m_displayContent($qryResult[0]->vCity));
$this->ObTpl->set_var(“TPL_VAR_COUNTY”,$this->libFunc->m_displayContent($qryResult[0]->vZip));
$this->ObTpl->set_var(“TPL_VAR_COUNTRY”,$this->libFunc->m_displayContent($qryResult[0]->vFirstName));

#GETTING PRODUCTS DETAIL AND ADD TO GOOGLE API
$this->obDb->query = “SELECT iOrderProductid_PK,iProductid_FK,iQty,OP.fPrice,”;
$this->obDb->query.= “fDiscount,OP.vTitle,OP.vSku,OP.iKit,OP.tShortDescription,OP.vSeoTitle, “;
$this->obDb->query.=”OP.iTaxable,OP.iFreeship,vPostageNotes,vDownloadablefile “;
$this->obDb->query .= ” FROM “.ORDERPRODUCTS.” OP INNER JOIN “.PRODUCTS.” P ON OP.iProductid_FK=P.iProdId_PK WHERE iOrderid_FK=’”.$this->request['mode'].”‘”;
$rsOrderProduct=$this->obDb->fetchQuery();
$rsOrderProductCount=$this->obDb->record_count;
if ($rsOrderProductCount >0){
for($i=0;$i< $rsOrderProductCount;$i++){

#PARSING VALUE TO TEMPLATE VARIABLE
$this->ObTpl->set_var(“TPL_VAR_PRODUCT_TITLE”,$this->libFunc->m_displayContent($rsOrderProduct[$i]->vTitle));
$this->ObTpl->set_var(“TPL_VAR_SKU_CODE”,$this->libFunc->m_displayContent($rsOrderProduct[$i]->vSku));
$this->ObTpl->set_var(“TPL_VAR_PRODUCT_PRICE”,number_format($rsOrderProduct[$i]->fPrice,2,’.',”));
$this->ObTpl->set_var(“TPL_VAR_QUANTITY”,$rsOrderProduct[$i]->iQty);
$this->ObTpl->set_var(“TPL_VAR_ORDER_ID”,$this->libFunc->m_displayContent($qryResult[0]->iInvoice));
$this->ObTpl->set_var(“TPL_VAR_CATEGORY”,”"); # INITILISE CATEGORY NAME

#GETTING CATEGORY NAME BASED ON PRODUCT ID
/*
PLEASE NOTE: A PRODUCT COULD BE ASSOCIATED TO MANY DEPARTMENTS OR HOMEPAGE THEREFORE WE WILL GET
THE CATEGORY WHICH THAT PRODUCT WAS INITIALLY CREATED FROM.
*/

$this->obDb->query  = ” SELECT fusionId, iOwner_FK, iOwner_FK, vtype, iState, vOwnerType “;
$this->obDb->query .= ” FROM ” .FUSIONS . ” WHERE iSubId_FK = ‘”.$rsOrderProduct[$i]->iProductid_FK .”‘”;
$this->obDb->query .= ” AND vOwnerType =’department’ AND iState =’1′ AND iOwner_FK >0 ORDER BY fusionId”;

$rsFusion=$this->obDb->fetchQuery();

if ($this->obDb->record_count > 0){
#ASSIGN THE FIRST RECORD TO CATEGORY ID
$deptId = $rsFusion[0]->iOwner_FK;

$this->obDb->query  = ” SELECT iDeptid_PK, vTitle FROM “. DEPARTMENTS . ” WHERE iDeptid_PK = ‘”. $deptId .”‘”;
$rsDept    =$this->obDb->fetchQuery();

$this->ObTpl->set_var(“TPL_VAR_CATEGORY”,$this->libFunc->m_displayContent($rsDept[0]->vTitle));
}#if

#PARSE PRODUCTS DETAIL TO GOOGLE E-COMMERCE API
$this->ObTpl->parse(“item_blk”,”TPL_GOOGLE_ADDITEM_BLK”,true);
}#for loop
}#if $rsOrderProductCount

$this->ObTpl->parse(“google_blk”,”TPL_GOOGLE_ECOMMERCE_TRACKING_BLOCK”);
$this->ObTpl->parse(“orderstatus_blk”,”TPL_ORDERSTATUS_BLK”);
}
$backOrderUrl=$this->libFunc->m_safeUrl(SITE_URL.”ecom/index.php?action=checkout.backorder”);
$this->ObTpl->set_var(“TPL_VAR_BACKORDERURL”,$backOrderUrl);

</pre>

Notice

1. If your shop is using Off-site payment gateway such as Paypal or GoogleCheckout, it is required your customer going back to the merchant site otherwise no record is tracked.

2. The modified m_orderProcessed() method is also availablet to download from the link at bottom of the post.

Enjoy!

Download google ecommerce tracking code for tradingeye and uk2 Version 1

Categories: E-commerce, Games, PHP, Tradingeye & UK2 Ecommerce, Viva, Web development | Tags: , , , , , , | 17 Comments