« Guy mountain bikes down Whistler with a helmet cam | Main | How to Jailbreak and Unlock Your iPhone 3G, 3GS Using BlackSn0w [ mac instructions ] »
Thursday
Nov122009

How to install Google Affiliate pixel tracking code in Magento

Took me a day of scouring the web to figure out how to get this to work. After adding this code do a test order and view source and confirm it worked.


#copy and paste to the bottom of
#app/design/frontend/default/theme_name/template/checkout/success.phtml
$companyID = "K123456"; #provided by Google
$category = urlencode("Your product category");
$lastorderid = Mage::getSingleton('checkout/session')->getLastOrderId();
$_order = Mage::getModel('sales/order')->load($lastorderid);
$orderID = $this->getOrderId(); #get the order ID
$aProduct = array();
$totalAmt = 0;
$prdsku = "";
$prdnm = "";
$prdqn = "";
$prdpr = "";
$prcatid = "";

###########store order data in array of objects###########
foreach ($_order->getAllItems() as $item) {
$oProduct = new stdClass();
$oProduct->sku = $item->getSku(); #sku
$oProduct->price = $item->getPrice(); #price
$totalAmt = $totalAmt + ($oProduct->price * $item->getQtyOrdered()); #total amt (excl.tax/shipping)
$oProduct->qty = $item->getQtyOrdered(); #quantity
$oProduct->name = $item->getName(); #product name
$oProduct->category = $category;
array_push($aProduct, $oProduct);
$oProduct = NULL; #nuke it
}
###########build query string variables###########
for ($i=0; $i < count($aProduct); $i++) {
if ($i == count($aProduct)-1) {
$prdsku .= urlencode($aProduct[$i]->sku);
$prdnm .= urlencode($aProduct[$i]->name);
$prdqn .= urlencode($aProduct[$i]->qty);
$prdpr .= urlencode($aProduct[$i]->price);
$prcatid .= urlencode($aProduct[$i]->category);
} else {
$prdsku .= urlencode($aProduct[$i]->sku) . "^";
$prdnm .= urlencode($aProduct[$i]->name) . "^";
$prdqn .= urlencode($aProduct[$i]->qty) . "^";
$prdpr .= urlencode($aProduct[$i]->price) . "^";
$prcatid .= urlencode($aProduct[$i]->category) ."^";
}
}
?>


Reader Comments (8)

Magento is tests your patience but overall they seem to be the best free cart out there.

Do you have experience with any other cart bundles like this, and this is our favorite? Or is this the first one you're trying?

December 16, 2009 | Unregistered CommenterJay Martin

I love 6am grammar

December 16, 2009 | Unregistered CommenterJay Martin

To be honest before we settled on Magento we didn't really try anything else. One of the other guys had used OSCommerce and i looked at it briefly, but it seemed neglected. Magento appears to have a vibrant community of developers, like Wordpress or Drupal.

There are some things about Magento that drive me crazy though. The "plumbing" has a steep learning curve, and there seems to be a few problems with their checkout in IE 6.0 that are stumping us (that problem should get better over time). I also wish it was easier to insert my own custom PHP / database calls - have been using iFrames to get things done fast (lame i know)...If i'd give Wordpress a 9/10 overall, i'd give Magento a solid 6.5/10.

December 16, 2009 | Unregistered CommenterDavid Schultz

Hi david,
I try to in put the as listed above only difference being the $companyID = "K123456" to my id.

I am very newbie at this so please understand.

I copy/pasted exactly as listed above to my success.phtml and the entire code above is actually displaying on the success page, and also when i see the source for the success page it is not showing any values but just showing exactly as shown above. it is displaying as if its just text and not some kind of script or code.

What am i doing wrong?

Thanks,
Lee

February 13, 2010 | Unregistered CommenterMecute

Hey Mecute - maybe check how your web server PHP config is set to handle <? vs <?php since it sounds like it is having trouble knowing when to interpret PHP code.

February 16, 2010 | Unregistered CommenterDavid Schultz

If not a big deal could you please email me the same codes above production ready? thank you so much!

February 19, 2010 | Unregistered CommenterMecute

Thanks for this chunk of code. Saved me a bigger headache than I already have!!

Mecute, make sure you add the php opening tag "<?php" before this chunk of code.

David,
I suggest you take a look at UberCart. I operate both Magento installs and UberCart installs for my customers. While it's not as powerful as Magento overall, I find it meets all of the needs of 90% of my customers. As for OS Commerce, you're right, it's been all but abandoned. It's big brother, Zen Cart is much more supported, but has no where near the community support as Magento or UberCart.

April 8, 2010 | Unregistered CommenterDan Myers

Very Good Article.

Thanks
http://www.madhursangam.com/matrimonials/hindi-matrimony.html' title='bharatmatrimony' rel="nofollow">BharatMatrimony

April 6, 2011 | Unregistered CommenterRavi

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>