Magento Analytics Conversion Tracking

Photo of author
Written By Ryan Rana





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 name="content">
            <block type="checkout/onepage_success" name="checkout.success" template="checkout/success.phtml"/>
		<reference name="before_body_end">
			<block name="google_adwords_tracking" template="checkout/google_adwords_tracking.phtml" type="core/template" />

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">
$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 type="text/javascript" src=""></script>


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


Leave a Comment