Affiliate Conversion Tracking With Multiple Networks
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:
- Create custom Magento module
- - To hook into Magneto controller front-end initiation event
- - Check for HTTP GET variable indicating affiliate network traffic
- - Set/Update cookie that tracks most recent referring network
- Update Magento checkout success template
- - Check for affiliate referral cookie
- - Get order data to report to affiliate network
- - 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
- <config>
- <modules>
- <{NameSpace}_{ModuleName}>
- <active>true</active>
- <codePool>local</codePool>
- </{NameSpace}_{ModuleName}>
- </modules>
- </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
- <config>
- <modules>
- <{NameSpace}_{ModuleName}>
- <version>0.1.0</version>
- </{NameSpace}_{ModuleName}>
- </modules>
- <global>
- <events>
- <controller_front_init_routers>
- <observers>
- <my_controller_front_init_observer>
- <type>singleton</type>
- <class>{NameSpace}_{ModuleName}_Model_Observer</class>
- <method>inboundAffiliateTracking</method>
- </my_controller_front_init_observer>
- </observers>
- </controller_front_init_routers>
- </events>
- </global>
- </config>
Create app/code/local/{NameSpace}/{ModuleName}/Model/Observer.php
- <?php
- class {NameSpace}_{ModuleName}_Model_Observer
- {
- /**
- * Check for inbound affiliate link data
- * To set cookie for selective tracking pixel display on success.phtml
- * @param type $observer
- */
- public function inboundAffiliateTracking($observer)
- {
- $request = $observer->getEvent()->getData('front')->getRequest();
- if ( isset($request->utm_source) ){
- //using Google Analytics tracking source variable it identify affilaite network
- if ( 'pepperjam' == $request->utm_source ){
- Mage::getModel('core/cookie')->set('affiliatenetwork', $request->utm_source, 86400*45);//45 day cookie
- }elseif ( 'buyat' == $request->utm_source ){
- Mage::getModel('core/cookie')->set('affiliatenetwork', $request->utm_source, 86400*45);//45 day cookie
- }
- }
- }
- //end class
- }
- ?>
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
- <?php if ($this->getOrderId()): ?>
- <?php $_order_details = Mage::getModel('sales/order')->loadByIncrementId(Mage::getSingleton('checkout/session')->getLastRealOrderId()); ?>
- <?php $_order_subtotal = $_order_details->discount_amount + $_order_details->subtotal; ?>
- <?php $_order_subtotal = 0 < $_order_subtotal ? number_format($_order_subtotal ,2) : 0; ?>
- <?php if ( 'pepperjam' == Mage::getModel('core/cookie')->get('affiliatenetwork') ): ?>
- <!-- PepperJam Affiliate Network - Conversion Tracking -->
- <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>
- <!-- /PepperJam Affiliate Network - Conversion Tracking -->
- <?php elseif ( 'buyat' == Mage::getModel('core/cookie')->get('affiliatenetwork')): ?>
- <!-- Buy.At Affiliate Network - Conversion Tracking -->
- <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" />
- <!-- /Buy.At Affiliate Network - Conversion Tracking -->
- <?php endif; ?>
- <?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
- Alan Storm for his invaluable tutorials on Magento custom module development. http://alanstorm.com/category/magento
- James Rowe for his blog post on reading URL parameters in Magento. http://www.jimcode.org/2011/02/read-url-parameters-magento-for-tracking/


