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) ."^";
	}
}
?>


Related Posts:

  1. Google improves streetmaps
  2. The best marketing is none at all
  3. In a crowded market clarity doesn’t sell

7 comments

  1. 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?

  2. 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.

  3. 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

  4. 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.

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

  6. 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.

Leave a comment