Spending money advertising with Google AdWords can make or break your e-commerce website, so it's very important that you have accurate and up to date conversion tracking in place. Many e-commerce website platforms and e-commerce content management systems make this easy to do, however with Magento, it requires a bit more work on the template side. In this tutorial we'll cover how you can quickly set that up with a couple of lines of simple PHP and no need for any extensions.
First, start by opening up your success page template, or the success.phtml template within your theme. This file will be located at /app/design/frontend/YOUR THEME/template/checkout/success.phtml. If you can't find the file there, look inside the base/default directory and you should be able to find it there.
Next, login to Google AdWords and under the "Tools" tab, you will click "Conversions". Find the conversion you are trying to track, for example a purchase, and open it up, you will then see your conversion code on the next page. Copy this conversion code and paste it in to your success.phtml template near the bottom of the page. This code should look like this:
[code]
<!-- Google Code for Purchase/Sale Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = XXXXXXXXXX;
var google_conversion_language = "en";
var google_conversion_format = "1";
var google_conversion_color = "666666";
var google_conversion_label = "XXXXXXXXX";
var google_conversion_value = 1.00;
var google_conversion_currency = "USD";
var google_remarketing_only = false;
/* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/XXXXXXXXX/?value= 1.00&currency_code=USD&label=XXXXXXXXX&guid=ON&script=0"/>
</div>
</noscript>
[/code]
You will notice that there are two sections that ask for a conversion value, but default to 1.00. This is what we will want to swap for dynamic code that will find the order subtotal and pass it to Google AdWords. To do this you'll first need to include this PHP snippet to create a variable called subtotal.
[php]
<?php
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$subtotal = $order->getSubtotal();
?>
[/php]
Once you have this setup, you will then need to go through your conversion tag and replace the conversion value default of 1.00 with the following:
[php]
<?php echo $subtotal ?>
[/php]
So when all is said and done, your final Google AdWords conversion tracking code should look like this:
[code]
<!-- Google Code for Purchase/Sale Conversion Page -->
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = XXXXXXXXXX;
var google_conversion_language = "en";
var google_conversion_format = "1";
var google_conversion_color = "666666";
var google_conversion_label = "XXXXXXXXX";
var google_conversion_value = <?php echo $subtotal ?>;
var google_conversion_currency = "USD";
var google_remarketing_only = false;
/* ]]> */
</script>
<script type="text/javascript" src="//www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//www.googleadservices.com/pagead/conversion/XXXXXXXXX/?value=<?php echo $subtotal ?>&currency_code=USD&label=XXXXXXXXX&guid=ON&script=0"/>
</div>
</noscript>
[/code]
Make sure to replace the default not only in the top section of the code, but also within the "img" tag at the bottom of the provided snippet. Once you have all of this in place, you can upload your success.phtml template to your FTP and then run a test by adding a product to cart, checking out, and verifying your purchase with Google Tag Manager.