Magento Analytics Conversion Tracking

When you do e-commerce sites it’s pretty obvious how to track your conversions – they will appear in sales in the Dashboard! However, sometimes you also get asked to add Google Analytics conversion tracking code just because the client wants all their statistics in one place. So the analytics guy will send through a request asking you to place a specific piece of code on the successful transaction ‘Thank You’ page.

Very easy to do. You need to add an new before_body_end block in the one_page_checkout_success section of checkout.xml in your layouts folder.

    <checkout_onepage_success translate="label">
        <label>One Page Checkout Success</label>
        <reference name="root">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
        </reference>
        <reference name="content">
            <block type="checkout/onepage_success" name="checkout.success" template="checkout/success.phtml"/>
        </reference>
		<reference name="before_body_end">
			<block name="google_adwords_tracking" template="checkout/google_adwords_tracking.phtml" type="core/template" />
		</reference>
    </checkout_onepage_success>

And then create your file google_adwords_tracking.phtml in your templates/checkout folder. It contains the Google generated analytics conversion success code;

<script type="text/javascript">
<?php 
//-------------------------------------------
// START ADWORDS CONVERSION VALUE TRACKING CODE
//-------------------------------------------
$order_details = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
$adwords_saleamt = $order_details->subtotal; 
?>

/* <!&#91;CDATA&#91; */
var google_conversion_id = ****your_id****;
var google_conversion_language = "en";
var google_conversion_format = "3";
var google_conversion_color = "ffffff";
var google_conversion_label = "*****your_label******";
var google_conversion_value = <?php echo $adwords_saleamt; ?>;
/* &#93;&#93;> */

</script>

<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js"></script>

<noscript>

<div style="display:inline;">
	<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/****your_id****/?label=*****your_label******&amp;guid=ON&amp;script=0"/>
</div>

</noscript>

Leave a Comment