Magento and Perfect Audience

I recently worked on a project that used Perfect Audience with the Magento CMS platform. I searched far and wide for a pre-built plugin or some code that I could use for the integration. Unfortunately this was unsuccessful.

Perfect Audience typically has great support and that is why I use them. Unfortunately they won’t provide technical support on what they consider “custom” development.

Here are the steps needed to get Perfect Audience setup on the Magento platform. It is important to note that I didn’t complete the full setup for dynamic product ads. Feel free to comment at the bottom if you have the code for this and would like me to update this post with it.

1. Add Perfect Audience’s Site Tracking Tag

Locate the footer.phtml file for your Magento theme and add the following code. This file should be located in /magento/app/design/frontend/MYTEMPLATE/default/template/page/html. Make sure to change the site tracking tag (XXXXXXXXXXXXXXXXXXXXXXXX.js) to that of your own site.

<script type="text/javascript">// <![CDATA[
  (function() {
    window._pa = window._pa || {};
    // _pa.orderId = "myOrderId"; // OPTIONAL: attach unique conversion identifier to conversions
    // _pa.revenue = "19.99"; // OPTIONAL: attach dynamic purchase values to conversions
    // _pa.productId = "myProductId"; // OPTIONAL: Include product ID for use with dynamic ads
    var pa = document.createElement('script'); pa.type = 'text/javascript'; pa.async = true;
    pa.src = ('https:' == document.location.protocol ? 'https:' : 'http:') + "//tag.perfectaudience.com/serve/XXXXXXXXXXXXXXXXXXXXXXXX.js";
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(pa, s);
  })();
// ]]></script>

2. Add Checkout Conversion Tracking Code

In case you don’t already know, it is important that you don’t edit the base template files for Magento. This means you need to create a duplicate success.phtml file if you haven’t done so already. You’ll find this file under /magento/app/design/frontend/base/default/template/checkout/success.phtml.

Copy the success.phtml file to /magento/app/design/frontend/MYTEMPLATE/default/template/checkout/ and Magento will automatically start using that file instead of the original.

Now it’s time to edit the file you just created. Add the following code to the bottom of the success.phtml file.

<!--Perfect Audience-->
<?php
	$order = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
	$amount = number_format($order->getGrandTotal(),2);
?>

<script type="text/javascript">
  window._pq = window._pq || [];
  _pq.push(['track', 'PurchasedItem', {orderId: '<?php echo $this->getOrderId(); ?>', revenue: <?php echo $amount; ?>}]);
</script>
<!--End Perfect Audience-->

3. Setup Conversion Goal in the Perfect Audience Admin

Next, you’ll need to setup the conversion goal in the Perfect Audience admin. The script you just added to your site fires an event named “PurchasedItem” to Perfect Audience each time someone places an order.

In order for Perfect Audience to process the conversion, we’ll need to setup a conversion goal. Perfect Audience has written this support document to help you get down the basics. Select “When a user triggers a specific javascript event” as the conversion type and add PurchasedItem as the event name. You can leave the Revenue Value field blank because you’ll be sending the system those details. I would suggest selecting “Count once per order ID” from the Conversion Counting drop-down.

At this point you are ready to start building your campaigns. You’ll need to add this new conversion goal to your campaigns as you set them up. Good luck!

Posted on October 19, 2014 in Development

Share the Story

Responses (4)

  1. Vlad
    January 21, 2015 at 7:10 pm · Reply

    this is how I set up the dynamic parameters:

    (function() {
    window._pa = window._pa || {};
    if(document.location.href.indexOf(‘checkout/onepage/success’) != -1) {
    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 = “”;
    _pa.revenue = “”;
    }
    _pa.productId = “”;
    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);
    })();

    • Jeremy Biron
      March 3, 2015 at 7:20 pm · Reply

      Vlad, based on your code it appears that you are not sending Perfect Audience all of your order details. You haven’t included your variables for the revenue, product ID, or order ID.

  2. john
    August 14, 2015 at 2:33 pm · Reply

    Hi Jeremy,

    Did you guys make it work with dynamic ads?

    It’s been almost a year since I checked their website and Perfect Audience don’t seem to care if Magento users will be able to use their system.

    Please let me know if you have some news.

    Tks,

    John

  3. Pamela
    December 17, 2015 at 1:21 pm · Reply

    Hey Jeremy,
    Thanks for creating this article!
    “Locate the footer.phtml file for your Magento theme and add the following code. This file should be located in /magento/app/design/frontend/MYTEMPLATE/default/template/page/html” –> when I go to this URL, I get a 404 error. Do you know if they maybe updated it in the meanwhile? What could I have done wrong? Would be grateful for your insights.

Leave a reply

Your email address will not be published. Required fields are marked *

 
Back to Top