Affiliate Conversion Tracking With Multiple Networks

Last modified by granados on Fri, March 23, 2012 00:53
Source|Old Revisions  

This is an old revision of the document!


Introduction

Affiliate conversion tracking using a single affiliate network is simply a matter of updating the order success template file. Things are not this simple when using multiple affiliate networks. This is due to the possibility that a customer can follow multiple affiliate links to your site prior to a purchase. If conversion tracking fires for all networks you can end up with multiple affiliate networks claiming commissions on a singer order. The solution is to set a cookie storing the last affiliate network referral for each visitor and using this cookie to selective fire the correct affiliate tracking. The rest of this page covers a method of implemented this solution in Magento.

Outline of procedure:

  1. Create custom Magento module
  2. - To hook into Magneto controller front-end initiation event
  3. - Check for HTTP GET variable indicating affiliate network traffic
  4. - Set/Update cookie that tracks most recent referring network
  5. Update Magento checkout success template
  6. - Check for affiliate referral cookie
  7. - Get order data to report to affiliate network
  8. - Fire affiliate tracking based referral cookie

A good piece of advice: Disable caching before developing in Magento.

Set Cookie For Last Referring Network

First piece of the puzzle is to create custom module to set cookie to identify most recent referring network.

Note:

  • replace {NameSpace} with your name space or company name
  • replace {ModuleName} with the name of your custom module
  • the reference section at the bottom of this page links to examples used in creating this solution

Create custom Magento Module

Create app/etc/modules/{NameSpace}_{ModuleName}.xml

  1. <config>
  2.     <modules>
  3.         <{NameSpace}_{ModuleName}>
  4.             <active>true</active>
  5.             <codePool>local</codePool>
  6.         </{NameSpace}_{ModuleName}>
  7.     </modules>
  8. </config>

Create directories

  • app/code/local/{NameSpace}
  • app/code/local/{NameSpace}/{ModuleName}
  • app/code/local/{NameSpace}/{ModuleName}/etc
  • app/code/local/{NameSpace}/{ModuleName}/Model

Create app/code/local/{NameSpace}/{ModuleName}/etc/config.xml

  1. <config>
  2.     <modules>
  3.         <{NameSpace}_{ModuleName}>
  4.             <version>0.1.0</version>
  5.         </{NameSpace}_{ModuleName}>
  6.     </modules>
  7.     <global>     
  8.         <events>
  9.             <controller_front_init_routers>
  10.                 <observers>
  11.                     <my_controller_front_init_observer>
  12.                         <type>singleton</type>
  13.                         <class>{NameSpace}_{ModuleName}_Model_Observer</class>
  14.                         <method>inboundAffiliateTracking</method>
  15.                     </my_controller_front_init_observer>
  16.                 </observers>
  17.             </controller_front_init_routers>
  18.         </events>
  19.     </global>
  20. </config>

Create app/code/local/{NameSpace}/{ModuleName}/Model/Observer.php

  1. <?php
  2. class {NameSpace}_{ModuleName}_Model_Observer
  3. {
  4.     /**
  5.      * Check for inbound affiliate link data
  6.      *      To set cookie for selective tracking pixel display on success.phtml
  7.      * @param type $observer
  8.      */
  9.     public function inboundAffiliateTracking($observer)
  10.     {
  11.         $request = $observer->getEvent()->getData('front')->getRequest();
  12.         if ( isset($request->utm_source) ){
  13.             //using Google Analytics tracking source variable it identify affilaite network
  14.             if ( 'pepperjam' == $request->utm_source ){
  15.                 Mage::getModel('core/cookie')->set('affiliatenetwork', $request->utm_source, 86400*45);//45 day cookie
  16.             }elseif ( 'buyat' == $request->utm_source ){
  17.                 Mage::getModel('core/cookie')->set('affiliatenetwork', $request->utm_source, 86400*45);//45 day cookie
  18.             }
  19.         }
  20.     }
  21. //end class   
  22. }
  23. ?>

Note: This example uses the HTTP GET data utm_source to identify affiliate network but you could use any incoming variable.

Selective Firing Of Conversion Tracking

Edit app/design/frontend/{ThemeName}/{ViewName}/template/checkout/success.phtml

add the selective affiliate tacking to the bottom of this file

  1. <?php if ($this->getOrderId()): ?>
  2.     $_order_details = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId());
  3.     $_order_subtotal = $_order_details->discount_amount + $_order_details->subtotal;
  4.     $_order_subtotal = 0 < $_order_subtotal ? number_format($_order_subtotal ,2) : 0;
  5.     <?php if ( 'pepperjam' == Mage::getModel('core/cookie')->get('affiliatenetwork') ): ?>
  6.         <!-- PepperJam Affiliate Network - Conversion Tracking -->
  7.         <iframe src="https://t.pepperjamnetwork.com/track?PID=XXXX&AMOUNT=<?php echo $_order_subtotal; ?>&TYPE=1&OID=<?php echo $this->getOrderId(); ?>" height="1" width="1" frameborder="0"></iframe>
  8.         <!-- /PepperJam Affiliate Network - Conversion Tracking -->
  9.     <?php elseif ( 'buyat' == Mage::getModel('core/cookie')->get('affiliatenetwork')): ?>
  10.         <!-- Buy.At Affiliate Network - Conversion Tracking -->
  11.         <img alt="" src="https://www.perfiliate.com/brains/process.php?PROGID=XXXX&TYPE=sales&AMOUNT=<?php echo $_order_subtotal; ?>&TRANSID=<?php echo $this->getOrderId(); ?>&VCODE=" height="2" width="1" />
  12.         <!-- /Buy.At Affiliate Network - Conversion Tracking -->
  13.     <?php endif; ?>
  14. <?php endif; ?>

Note: Found details on getting order details in Magneto forum posts; however I’m still not convinced loading the order model via getLastRealOrderId() is the perfect method.

Reference and Thanks

Special thanks to the backs on whom I stood to create this solution




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs