This post was published in 2016 and may contain information, techniques or code examples that are no longer current. Please double-check official documentation and modern best practices before using anything from this article.
Simple single script to add Google’s conversion tracking to your Magento ecommerce website. The code should be added into the footer.phtml file within your current template. The code includes tracking of catalogue browsing, checkout and new orders.
Simply replace all the $googleConversionId with your actual Google Conversion ID.
<!-- Google Conversion Tracking -->
<?php $googleConversionId = ""; // Your conversion ID goes here ?>
<?php $page_type = Mage::app()->getFrontController()->getRequest()->getControllerName();
if($page_type == 'index') { ?>
<script type="text/javascript">
var google_tag_params = {
ecomm_pagetype: 'home',
ecomm_prodid: '',
ecomm_totalvalue: 0
};
</script>
<?php } elseif($page_type == 'category') { ?>
<script type="text/javascript">
var google_tag_params = {
ecomm_pagetype: 'category',
ecomm_prodid: '',
ecomm_totalvalue: 0
};
</script>
<?php } elseif($page_type == 'product') {
$product_id = Mage::registry('current_product')->getId();
$product_id = Mage::getModel('catalog/product')->load($product_id)->getSku();
$product_price = Mage::registry('current_product')->getPrice();
?>
<script type="text/javascript">
var google_tag_params = {
ecomm_pagetype: 'product',
ecomm_prodid: '<?php echo $product_id; ?>',
ecomm_totalvalue: parseFloat('<?php echo $product_price; ?>')
};
</script>
<?php } elseif($page_type == 'cart') { ?>
<script>
var id = new Array();
var price = new Array();
</script>
<?php
$cart = Mage::getModel('checkout/session')->getQuote();
foreach ($cart->getAllItems() as $item) {
$product_id = $item->getProductId();
$product_id = $item->getSku();
$product_id_all = $product_id_all.','.$product_id ;
$productPrice = $item->getProduct()->getPrice();
$productPrice_all = $productPrice_all.','.$productPrice;
?>
<Script>
id.push('<?php echo $product_id; ?>');
price.push(parseFloat('<?php echo $productPrice; ?>'));
</Script>
<?php
}
$product_id_all=substr($product_id_all, 1);
$productPrice_all=substr($productPrice_all, 1);
?>
<script type="text/javascript">
var google_tag_params = {
ecomm_pagetype: 'cart',
ecomm_prodid: id,
ecomm_totalvalue: price
};
</script>
<?php } elseif($page_type == 'onepage') { ?>
<script>
var id = new Array();
var price = new Array();
</script>
<?php
$cart = Mage::getModel('checkout/session')->getQuote();
foreach ($cart->getAllItems() as $item)
{
$product_id = $item->getProductId();
$product_id = $item->getSku();
$product_id_all = $product_id_all.','.$product_id ;
$productPrice = $item->getProduct()->getPrice();
$productPrice_all = $productPrice_all.','.$productPrice;
?>
<Script>
id.push('<?php echo $product_id; ?>');
price.push(parseFloat('<?php echo $productPrice; ?>'));
</Script>
<?php }
$product_id_all=substr($product_id_all, 1);
$productPrice_all=substr($productPrice_all, 1);
?>
<script type="text/javascript">
var google_tag_params = {
ecomm_pagetype: 'purchase',
ecomm_prodid: id,
ecomm_totalvalue: price
};
</script>
<?php }else{ ?>
<script type="text/javascript">
var google_tag_params = {
ecomm_pagetype: 'other',
ecomm_prodid: '',
ecomm_totalvalue: 0
};
</script>
<?php } ?>
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = <?php echo $googleConversionId; ?>;
var google_custom_params = window.google_tag_params;
var google_remarketing_only = true;
/* ]]> */
</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="//googleads.g.doubleclick.net/pagead/viewthroughconversion/<?php echo $googleConversionId; ?>/?value=0&guid=ON&script=0"/>
</div>
</noscript>
<!-- End Google Conversion Tracking -->More Magento posts
—
Magento database structure
The database structure of Magento is designed to store and manage various aspects of an e-commerce website, including products, orders, customers, and more. Understanding the Magento database structure is crucial…
—
Accessibility in ecommerce websites
In the digital age, where online shopping has become an integral part of our lives, ecommerce websites hold the key to business success. Amidst the rush to create visually appealing…
—
Using Magento to get small businesses online
For small businesses aiming to establish a formidable online presence and drive sales, Magento emerges as a powerful e-commerce platform offering a suite of features tailored to their needs. Scalability…
Continue reading "Using Magento to get small businesses online"
—
How can I speed-up my Magento website?
Speed optimisation is crucial for a Magento website as it directly impacts user experience, conversions, and search engine rankings. You should prepare your server so it is set-up to use…
More SEO posts
—
Building cookieless tracking without Google Analytics
The era of ubiquitous third-party cookies is drawing to a close, accelerated by stringent regulations like the GDPR in Europe and the CCPA in California, alongside browser initiatives such as…
Continue reading "Building cookieless tracking without Google Analytics "
—
Structured data for SEO
Adding structured data to a website is one of those steps that often gets overlooked, yet it can make a real difference to how a site performs in search engines.…
—
Simple Analytics: A privacy-focused alternative to Google Analytics
In an era where digital privacy concerns are at the forefront of online discourse, many organisations are reassessing their tools to ensure compliance with data protection laws and maintain user…
Continue reading "Simple Analytics: A privacy-focused alternative to Google Analytics"
—
Cookieless website tracking and analytics
Cookieless website tracking is a method of collecting analytics data and monitoring website behaviour without the need for traditional browser cookies. Traditionally, cookies have been a key component in tracking…
Continue reading "Cookieless website tracking and analytics"