Do you or the affiliate marketer know which conversions have been made by you, and which conversions are made through the affiliate referrals? Do you inform your affiliate whenever a conversion is made through their referral? Instead of keeping track manually, Magento allows you to create a module that will track affiliate referrals and notify whenever the referral is converted.
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
How to Create Module to Track Affiliate Conversions?
1. How to Create Module to Track Affiliate Conversions?
Do you or the affiliate marketer know which conversions have been made by you, and which
conversions are made through the affiliate referrals? Do you inform your affiliate whenever a
conversion is made through their referral? Instead of keeping track manually, Magento allows you
to create a module that will track affiliate referrals and notify whenever the referral is converted.
The Pre-requisites
By now you must be aware with the basics of Magento and coding. You must have familiarized
yourself with creating Magento modules.
Whenever you are making certain website specific settings, you would add them as configurable
options in the admin panel. That’s why the module you are coding for affiliate conversions will
become a community module and will be stored in the following path app/code/community. When
you create a community module or code, you are actually allowing future use of the module, as it is,
without any modifications.
To create a module to track affiliate conversions, you will need to create empty file structures
containing empty files
Here’s a code that will help you create this structure
app
- code
- community
- Affiliate
- Block
- Conversion.php
- Model
- Observer.php
- etc
2. - config.xml
- design
- frontend
- base
- default
- layout
- Example_affiliate.xml
- template
- Example_affiliate
- conversion.phtml
- etc
- modules
- Example_Affiliate.xml
To add content for the example module, here is the code
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Example_Affiliate>
<active>true</active>
<codePool>community</codePool>
</Example_Affiliate>
</modules>
</config>
Steps to Track the Affiliate Referral
When you set out to track affiliates, the first thing you track is the content that referred your
website. Was it a website, a newsletter, or similar kind of content that included your website
details? You will also need to know the affiliate ID to reward the affiliate once the conversion has
been made
Example.com/?utm_source=some_affiliate_id
The Event Observer
$_GET parameter is used to fetch a particular URL and use it for affiliate marketing. It is necessary
for you, as a store to include this parameter throughout your website, so that affiliates can link the
page containing the product/service of interest to their readers or users.
You should not bring any changes to the core. That’s why it is necessary to use event_observer that
will connect with the pages and fetch the required data
The single event that will be dispatched across the different parts of the website would be
controller_front_init_before
Let’s create a config.xml along with an observer for this particular event
<?xml version="1.0" encoding="UTF-8"?>
<config>
4. array('front' => $this)
);
If you want to gain access to the core file defined by Mage_Core_Controller_Varien_Front you will
need to paste the code given below to your event_observer module
<?php
class Example_Affiliate_Model_Observer
{
public function captureReferral(Varien_Event_Observer $observer)
{
$frontController = $observer->getEvent()->getFront();
}
}
This controller checks and analyzes the different URLs that are passed through it. Through this, you
are able to access the object. Now, you need to check for $_GET parameter within this object. The
following code will help you perform the necessary action
<?php
class Example_Affiliate_Model_Observer
{
public function captureReferral(Varien_Event_Observer $observer)
{
$frontController = $observer->getEvent()->getFront();
$utmSource = $frontController->getRequest()
->getParam('utm_source', false);
if ($utmSource) {
// here we will save the referrer affiliate ID
}
}
}
Here the getRequest() is used to regain the contents of Mage_Core_Controller_Request_Http
instance present within the controller. This instance will possess all the requisite details related to
the URL including the $_Post and $_Get parameters. Using the getParam() method, you can easily
retrieve the required details. $utmsource will contain the affiliate ID referring the link
Now, you know the affiliate ID. You will need to save the details so that you can reward the affiliate
in case the user who has clicked the link gets converted.
As you are aware, Magento has a core functionality of dealing with Cookies. You can easily use
$_COOKIE to save the $utmsource for the affiliate
<?php
class Example_Affiliate_Model_Observer
{
const COOKIE_KEY_SOURCE = 'Example_affiliate_source';
5. public function captureReferral(Varien_Event_Observer $observer)
{
$frontController = $observer->getEvent()->getFront();
$utmSource = $frontController->getRequest()
->getParam('utm_source', false);
if ($utmSource) {
Mage::getModel('core/cookie')->set(
self::COOKIE_KEY_SOURCE,
$utmSource,
$this->_getCookieLifetime()
);
}
}
protected function _getCookieLifetime()
{
$days = 30;
// convert to seconds
return (int)86400 * $days;
}
}
Constants are truly useful when you need to use the same value across locations. In this code
constant COOKIE_KEY_SOURCE has been defined. With a constant, you don’t need to keep changing
the code with change in values. The code _getcookielifetime() has been defined to retrieve the
cookie for a lifetime. You can always define your lifetime. In this case, the number of days has been
set to 30 days.
Notifying the Affiliate on Conversion
The above processes have allowed us to include the affiliate ID in the cookie. Let’s say the customer
who has been referenced to your website has successfully checked out fromyour website. Now, you
need to notify the affiliate about the conversion, and reward them.
You can create event_observer and notify using the server side API. In this case, Magento layout and
introduce the necessary HTML code at the bottom of the page declaring order success
In order to perform this, you will need to update config.xml to introduce the different blocks and
layout created for this purpose
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Example_Affiliate>
<version>0.0.1</version>
</Example_Affiliate>
</modules>
<global>
7. Using the below code you can define a new template file. Contents for conversion.phtml will be
affiliate specific. For the sake of simplicity, the code being considered here is generic
<img
src="http://media.Example.com/themes/Examplev4/images/logo.png
?merchant_id=<?php echo $this->getMerchantId() ?>
&affiliate_id=<?php echo $this->getAffiliateId() ?>"
width="1"
height="1"
/>
Now, you need to create a custom block to enter the data
<?php
class Example_Affiliate_Block_Conversion
extends Mage_Core_Block_Template
{
public function getMerchantId()
{
return '12345';
}
public function getAffiliateId()
{
return Mage::getModel('core/cookie')->get(
Example_Affiliate_Model_Observer::COOKIE_KEY_SOURCE
);
}
}
Next go to System>Configuration and start configuring the different elements that would be needed
for each Magento instance
Configuring a New System Configuration Tab
For the purpose of notification, you will create a new system configuration tab.
app/code/community/Example/Affiliate/etc/system.xml
Paste the following code in this path
<?xml version="1.0" encoding="UTF-8"?>
<config>
<!-- we are defining a new tab -->
<tabs>
<!-- our tab unique short name -->
<Example>
<!-- the title of our tab in the admin panel sidebar -->
<label>Example Magazine</label>
<!-- the order our tab should appear on the sidebar -->
<sort_order>100</sort_order>
8. </Example>
</tabs>
</config>
Now, create a new file app/code/community/Example/Affiliate/etc/adminhtml.xml to configure
ACL settings
<?xml version="1.0" encoding="UTF-8"?>
<adminhtml>
<acl>
<resources>
<admin>
<children>
<system>
<children>
<config>
<children>
<Example_affiliate>
<title>Example Magazine
Affiliate</title>
</Example_affiliate>
</children>
</config>
</children>
</system>
</children>
</admin>
</resources>
</acl>
</adminhtml>
Add the items required for new system configuration
<?xml version="1.0" encoding="UTF-8"?>
<config>
<tabs>
<Example>
<label>Example Magazine</label>
<sort_order>100</sort_order>
</Example>
</tabs>
<!-- we are adding a new section to our tab -->
<sections>
<!-- unique shortname for our section -->
<Example_affiliate>
<!-- the title of our section in the sidebar -->
<label>Affiliate Tracking</label>
<!-- the tab under which we want our section to appear -->
<tab>Example</tab>
9. <!-- order of section relative to our tab -->
<sort_order>10</sort_order>
<!-- visibility of our section -->
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<!-- we are adding some new groups to our section -->
<groups>
<!-- the unique short code for our group -->
<general>
<!-- the title of our group -->
<label>General Settings</label>
<!-- order of group relative to the section -->
<sort_order>10</sort_order>
<!-- visibility of our group -->
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<!-- we are adding some new fields to our group -->
<fields>
<!-- the unique short code for our field -->
<status>
<!-- the label of our field -->
<label>Enabled</label>
<!-- the type of our field -->
<frontend_type>select</frontend_type>
<!-- the source of our 'select' type -->
<source_model>
adminhtml/system_config_source_enabledisable
</source_model>
<!-- order relative to the group -->
<sort_order>10</sort_order>
<!-- visibility of our field -->
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
</status>
<merchant_id>
<label>Merchant ID</label>
<frontend_type>text</frontend_type>
<sort_order>20</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</merchant_id>
</fields>
</general>
12. You can now replace the hard coded cookie lifetime value with the admin panel system
configuration value
<?php
class Example_Affiliate_Model_Observer
{
const COOKIE_KEY_SOURCE = 'Example_affiliate_source';
public function captureReferral(Varien_Event_Observer $observer)
{
$frontController = $observer->getEvent()->getFront();
$utmSource = $frontController->getRequest()
->getParam('utm_source', false);
if ($utmSource) {
Mage::getModel('core/cookie')->set(
self::COOKIE_KEY_SOURCE,
$utmSource,
$this->_getCookieLifetime()
);
}
}
protected function _getCookieLifetime()
{
$days = Mage::getStoreConfig(
'Example_affiliate/cookie/timeout'
);
// convert to seconds
return (int)86400 * $days;
}
}
It’s time to call the merchant ID of the affiliate to the layout defined
<?php
class Example_Affiliate_Block_Conversion
extends Mage_Core_Block_Template
{
public function getIsActive()
{
return Mage::getStoreConfig(
'Example_affiliate/general/status'
) ? true : false;
}
public function getMerchantId()
{
return Mage::getStoreConfig(
13. 'Example_affiliate/general/merchant_id'
);
}
public function getAffiliateId()
{
return Mage::getModel('core/cookie')->get(
Example_Affiliate_Model_Observer::COOKIE_KEY_SOURCE
);
}
}
You can easily enable or disable this new configuration system tab conversion.phtml with this code.
This way it will be called only when the checkout has been a success
<?php if ($this->getIsActive()): ?>
<img
src="http://media.Example.com/themes/Examplev4/images/logo.png
?merchant_id=<?php echo $this->getMerchantId() ?>
&affiliate_id=<?php echo $this->getAffiliateId() ?>"
width="1"
height="1"
/>
<?php endif; ?>
Conclusion
It is important to create and structure community modules in the admin panel so that you can
easily use it across websites. To customize, you just need to fine tune the code to suit requirements.
Note: Don’t forget to take a backup of your system configuration and other existing files before you
create the module using this code
Original Source:
https://medium.com/@semaphoresoftware/how-to-create-module-to-track-affiliate-conversions-
20749a696ced