Skip to content

Instantly share code, notes, and snippets.

@shinesoftware
Last active August 29, 2015 14:07
Show Gist options
  • Save shinesoftware/945650f49162920d66d2 to your computer and use it in GitHub Desktop.
Save shinesoftware/945650f49162920d66d2 to your computer and use it in GitHub Desktop.
This code helps you to track on a Magento Store visitors and the conversions using the Perfect Audience service.
/*
This code should be inserted to the bottom of this file: /app/design/frontend/default/default/template/page/html/footer.phtml
Make sure to update your perfect audience tracking info (line 34).
Tested with Magento 1.9.0.1
Thanks to Sam327
*/
<!-- Start of Perfect Audience Script -->
<script type="text/javascript">
(function() {
window._pa = window._pa || {};
if(document.location.href.indexOf('checkout/onepage/success') != -1) {
<?php
$_tmp_amount = 0.00;
$product_id = "";
$lastOrderId = Mage::getSingleton('checkout/session')->getLastRealOrderId();
if($lastOrderId) {
$_tmp_amount = Mage::getModel('sales/order')->loadByIncrementId($lastOrderId)->getBaseGrandTotal();
}
if(Mage::registry('current_product')){
$product_id = Mage::registry('current_product')->getSku();
}
?>
_pa.orderId = "<?php echo $lastOrderId; ?>";
_pa.revenue = "<?php echo $_tmp_amount; ?>";
}
_pa.productId = "<?php echo $product_id; ?>";
var pa = document.createElement('script'); pa.type = 'text/javascript'; pa.async = true;
pa.src = ('https:' == document.location.protocol ? 'https:' : 'http:') + "//tag.perfectaudience.com/serve/542e77d51230da945700000d.js";
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(pa, s);
})();
</script>
<!-- End of Perfect Audience Script -->
@anthonycreek
Copy link

Have you disabled caching of your footer.phtml?

@forecastly
Copy link

This is extremely helpful! Thank you for sharing this code.

What do you add as your conversion goal in Perfect Audience?

@ELundby45
Copy link

Does this assume every page is a product page? What would _pa.productId on line 27 be populated with, on for example the account page?

@sam327
Copy link

sam327 commented Nov 18, 2014

I had a problem putting this into my footer.phtml - when I did so, the product id would get cached. No matter which page I went to, _ pa.productID was always the same. So I put the code in view.phtml. Then the productID is different on each product page.

Are you seeing a similar behavior? Or did you find a way around it?

@anthonycreek
Copy link

footer.phtml is cached (if you have caching enabled) so you can't put the code in there. I ended up creating a block containing the PA code and calling it via the local.xml file and disabling the cache on just that block.

@puntable
Copy link

anthonycreek is correct. You need to disable caching in this specific block, else it wont work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment