how to create module to track affiliate conversions?

16
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

Upload: semaphore-software

Post on 29-Sep-2015

4 views

Category:

Documents


2 download

DESCRIPTION

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.

TRANSCRIPT

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. Thats 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

Heres a code that will help you create this structure

app

- code

- community

- Affiliate

- Block

- Conversion.php

- Model

- Observer.php

- etc

- config.xml

- design

- frontend

- base

- default

- layout

- Example_affiliate.xml

- template

- Example_affiliate

- conversion.phtml

- etc

- modules

- Example_Affiliate.xmlTo add content for the example module, here is the code

true

community

Steps to Track the Affiliate ReferralWhen 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 madeExample.com/?utm_source=some_affiliate_idThe 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. Thats 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

Lets create a config.xml along with an observer for this particular event

0.0.1

Example_Affiliate_Model

Example_affiliate/observer

captureReferral

singleton

To capture the referral the following function will be called

Example_Affiliate_Model_Observer::captureReferral()It is important to add content to observer.php that you have just created so that the referral can fetch some data

$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

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

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 linkNow, 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

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 dont 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 ConversionThe above processes have allowed us to include the affiliate ID in the cookie. Lets say the customer who has been referenced to your website has successfully checked out from your 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

0.0.1

Example_Affiliate_Block

Example_Affiliate_Model

Example_affiliate/observer

captureReferral

singleton

Example_affiliate.xml

Adding New Layout with Template Files and Custom BlocksYou will need to modify the layout handle onepage_checkout_success. For this you will need to update the layout file Example_affiliate.xml

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

Now, you need to create a custom block to enter the dataget(

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 instanceConfiguring a New System Configuration TabFor 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

Example Magazine

100

Now, create a new file app/code/community/Example/Affiliate/etc/adminhtml.xml to configure ACL settings

Example Magazine

Affiliate

Add the items required for new system configuration

Example Magazine

100

Affiliate Tracking

Example

10

1

1

1

General Settings

10

1

1

1

Enabled

select

adminhtml/system_config_source_enabledisable

10

1

1

0

Merchant ID

text

20

1

1

1

Cookie Settings

20

1

1

1

Cookie Timeout

text

10

1

1

1

This is the amount of time

an affiliate cookie will last, in days

]]>

Define the default admin settings using the following code

0.0.1

Example_Affiliate_Block

Example_Affiliate_Model

Example_affiliate/observer

captureReferral

singleton

Example_affiliate.xml

30

Now, most of things required by your layout have been configured. Access the system configuration values using the following snippet

Make sure you change the default system configuration path with the new one

You can now replace the hard coded cookie lifetime value with the admin panel system configuration value

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;

}

}Its time to call the merchant ID of the affiliate to the layout defined

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

getIsActive()): ?>

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: Dont 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