How are people doing Affilate Stuff with Ubercart

Posts: 79
Joined: 10/29/2007
Bug Finder

I've searched and read all the affiliate stuff on the site, and it seems that lots of people are wanting to do something with affiliate tracking, and some are doing something with cartlinks, etc, but I can't find any definate examples of Ubercart AFfiliate work.

I have an affiliate program and I need to setup support for it, so I'd like to get a couple of idea from you experts....

All of my affiliates have to have a Drupal Account, so I basically have an affilate module that lets my users build text links, graphic links, and it basically creates a URL with their UID embedded. Currently, I store this when a link is clicked, and if someone joins my site as a result of a referrral, I link the new person to the affiliate, so they always get paid for repeat sales.I've been passing it to Paypal as a custom field, and using IPT to write my transactions into a database that tracks my commissions.

Now that I'm switching to Ubercart, I have to change my tracking logic. For my purposes, all I really need to do is to store the UID of the affiliate with the order somewhere in the database.

Here are a couple of possiblities that I see, but I'd like some input.

1. When order completes write a record to uc_order_log or uc_order_admin_contents with the order_id and the uid of my referring affiliate.

2. Create a new uc_affiliate table and write order_id, uid or affiliate to a new table.

3. Try to re-word the current drupal affiliate module to work with Ubercat (seems like this would be more difficult).

4. Others...

Thouguts.

1. Google Analystics is not an option, I need to have detailed record tracking every order to an affilite.

2. I don't know how someone would use CartLinks to track affiliates, if you can
explain this, I'd like to know more.

3. I'm assuminmg that SOMEONE on here has done something like this already, if so, please share.

Thanks for all the help.
Jim

Posts: 60
Joined: 09/04/2007

I was thinking about the same situation.
Trying to adapt Drupal's Affiliate module for E-commerce seems very complicated.
I thought of creating a role for affiliates, so that they could login and enter the orders manually (from the customers they get). However, as I mentioned on other thread, when an affiliate was granted this role, they could also modify the orders created through our own site. Ryan said that once an order is created, it goes into edit mode. So I don't think of handling affiliates through roles.

I would really like to see if someone comes up with any other idea.

Posts: 79
Joined: 10/29/2007
Bug Finder

I didn't get much response when I posted this thread, so I've written a pretty cool Affiliate system over the last couple of days. This one cannot be too easily turned into a Drupal module, but I thougt I'd share what I did so others could learn from my experience.

To follow along with this thread,visit www.ipodfitnesscenter.com and login with u:demo and p:demo

To become an affilaite, you enter your info in a pretty standard drupal form, it mainly makes sure we have your mailing address for your check, and your tax id number. Sign up form is here.
http://www.ipodfitnesscenter.com/affiliate

Once you join the program, you can use our site to build image or text links to your website.

http://www.ipodfitnesscenter.com/affiliate/imagelink - To build Image links (cool)
http://www.ipodfitnesscenter.com/affiliate/textlink - to build text links

You'll notice that these both create links that include your affiliate id (which is your drupal uid) and all other info you need to place these on your website.

The commissions tab is next, and this is where you can see commission that you earn on the site. These commissions are written using Ubercart during the checkout process.
http://www.ipodfitnesscenter.com/affiliate/commission - to see commissions earnd

My site has a 2 tier affiliate program, I pay 20% commission to the affiliate, and 5% to the person that referred the affilate to the site. Here is the code I use in my affiliate module to write the commissions using ubercarts hook_order

<?php
function affiliate_order($op, $order, $status) // hoook_order for ubercart

{
global
$user;

if (
$op == 'update' && $status == 'pending' && uc_payment_balance($order) <= 1) {
// sale has been made write commision entries
// make sure my afid are set
   
if (empty($_SESSION[afid])) {
       
$_SESSION[afid] = getaffiliateforuser($user->uid, false);
       
$_SESSION[afidup] = getaffiliateforuser($user->uid, true);
    }
   
// get total of commissionalbe products
   
$comm_total = 0;
    foreach (
$order->products as $product) {
       
$x = node_load($product->nid);
        if (
$x->type == "supplements")     $comm_total += $product->price;
    }
    if (
$_SESSION[afid]) {
   
// write primany commission record
       
db_query("insert into {uc_affiliate_pay} (order_id, afid, commission, commission_notes) values ($order->order_id, $_SESSION[afid], $comm_total * .2, '20% commission on $comm_total sale  to $order->billing_first_name $order->billing_last_name')");   
    }
    if (
$_SESSION[afidup]) {
   
// write secondary commission record
           
db_query("insert into {uc_affiliate_pay} (order_id, afid, commission, commission_notes) values ($order->order_id, $_SESSION[afidup], $comm_total * .05, '5% commission on $comm_total sale to $order->billing_first_name $order->billing_last_name')");   

    }
  }
}
?>

The first couple of lines makes sure that my affiliate and affiliate sponsor are already set, they are set at login, when you click and affiliate link, or in hook_menu, this is one final check to make sure we have this set.

<?php
foreach ($order->products as $product) {
       
$x = node_load($product->nid);
        if (
$x->type == "supplements")     $comm_total += $product->price;
    }
?>

This goes through the products in the order and calcs volume for products I pay commission on. I only pay 20% on the supplements product type. (Note - Ryan, it would be cool if the product type was stored in the $order object somewhere so I would not have to reload all the product nodes.)

Once I have the total, I write a 20% and a 5% commmission to a new table I created called uc_affiliate_pay. (I need to delete coupon discounts from this total still) It has a simple structure with the following structure.

CREATE TABLE `uc_affiliate_pay` (
`key` bigint(20) NOT NULL auto_increment,
`order_id` bigint(20) default NULL,
`afid` bigint(20) default NULL,
`commission` double default NULL,
`commission_notes` varchar(60) default NULL,
PRIMARY KEY (`key`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=latin1;

The next affiliate tab is for clicks which keeps track of every click on an affilate ad.
http://www.ipodfitnesscenter.com/affiliate/clicks

My affiliate code to track click and route the affiliate links to the correct pages was super simple.

<?php
function affiliate_start(){
$_SESSION[afid] = arg(4);
$target = arg(5);
$_SESSION[afidup] = getaffiliateforuser(arg(4), true);


db_query("insert into (affiliate_clicks} (affiliate_id, click_datetime, target,  referring_site) values ($_SESSION[afid], now(), '$target', '" . $_SERVER['HTTP_REFERER'] ."') ");
switch (
$target) {
case
'home' : drupal_goto("home");
case
'shopping' : drupal_goto("supplements");
case
'go' : drupal_goto("goipod");
case
'join' : drupal_goto("user/register");
case
'flash' : drupal_goto("flash.php");
case
'zeacaps' : drupal_goto("zeacaps");
default :
drupal_goto("supplements");
    }
}
?>

It grabs the affiliate id and the target page from the URL, then finds the sponsoring affiliate, it then updates the click database (even track the site that sent the click), and then uses drupal_goto to navigate to the target page you selected when you built your link.

All I have left to do is to build a few sexier ads. (I want to do one that rotates, and also one that pulls dynamic content, and one the pulls random products from the ubercart store). I also need to do some additional reporting and summarization so I write the correct commission records over to our payment system.

All in all, I'm pretty happy with what this does for my affiliate needs. If you would like source code, or have questions, please feel rree to ask... Also, I'd love it if you put up one of my affiliate ads on your website. Again, I pay out 20% on sales, and it's FREE.

Enjoy.
Jim Fulford
www.ipodfitnesscenter.com

Posts: 79
Joined: 10/29/2007
Bug Finder

Two things I forgot to mention. I did use the uc_order_history to let my users see their order detail on a customized user profile page. Check it out here...

http://www.ipodfitnesscenter.com/users/demo

<?php
   
global $user;
    if (
$GLOBALS['user']->uid == arg(1)) {
    echo
'<div style="width: 99%;  padding-left: 1%;">
        <div class="cnt_hd_txt">
        <div id="fadeGreen">
            <div class="cnt_hd">Order History</div>'
;
            echo
uc_order_history($user->uid);
        echo
'</div>
        </div>
    </div>'
;
    }
   
$account = user_load(array('uid' => arg(1)));
   
drupal_set_title("Profile - ". $account->profile_fullname);
   
?>

(Yes Ryan, I did have to use drupal_set_title(), thanks for the tip)

If you click on the order, you'll see that I have including UPS and USPS tracking information in the order comments...

http://www.ipodfitnesscenter.com/user/52/order/40

The other thing you may have wondered when building an affiliate link, is why I used such a long path in the affiliate link. ie. Here is a sample link...
http://www.ipodfitnesscenter.com/ipod/fitness/nike/affiliate/52/go

I don't actually need the ipod - fitness - nike part of the URL, I could have used only affiliate, but by having my site keywords included in links pointing to my site, it increases my SEO rating for those pages.... (No big deal, but I had someone ask me about that when they made a link a few days ago.)

Thanks for all the help.
Jim

Posts: 60
Joined: 09/04/2007

This is a nice job you did, VitaLife. Thanks for your ideas on this topic.
I already saw your demo site and I'll keep on testing. Maybe I can implement something similar to my site.

Posts: 4695
Joined: 08/07/2007
AdministratorHead Code Monkey - I eat bugs.

Unbelievable job. This is simply incredible, and honestly it makes me want to start up and start driving traffic to your site today. Sticking out tongue I really appreciate you taking the time to post this all up here, and if you feel up to the task of copying these comments to a tutorial page in the handbook that would be awesome.

Thanks again, Jim! Congrats on an excellent job.

Posts: 203
Joined: 09/06/2007

I thought of altering the click thru module which would store the affilate stuff and then is dropping you at the product itself ...

Posts: 79
Joined: 10/29/2007
Bug Finder

Thanks Ryan,
I'll post the tutorial over later today. I may also post this over on drupal.org, there seems to be a lot of people looking for affiliate stuff, and the only official affiliate module that is posted is listed as alpha code only, so we'll probably get a lot of reads and bring some more folks over the UberCart.
Jim

Posts: 127
Joined: 08/07/2007

as a coding newbie, why is this hard to turn into a module? You've already written most of the code, ya?

Posts: 79
Joined: 10/29/2007
Bug Finder

It would not be particurly too hard to code, but this was wriiten just to work with my business model. I have a custom user profile; when I build links, all the images and image paths are hard coded, these would need to be made configurable, then there may be 1000 different ways that people pay their affiliates, I just programmed it to work the one way I need it too.

I used database fields which are in a whole different database on my system.. Then after you get past that, you'd need to write the install, .info and uninstall code.

As an aside.... I just posted a way to send out emails to new site users with a gift certificate, that code would be pretty easy to turn into a module, becuase everone will use it in pretty much the same way, but this affiilate thing is very tailored to a specific business model.

Jim Fulford
www.ipodfitnesscenter.com

Posts: 6
Joined: 10/11/2007

Nice work, care to explain a little more about how you created the image/text link pages?

Posts: 203
Joined: 09/06/2007

I agree documentation is key!!!

Posts: 69
Joined: 08/08/2007
Internationalizationizer

could you point to/discuss how you track a user from the referrer.

If the user's initial visit
was a distinct session from the actual purchase session,
wouldn't you need cookies to retain the referrer's id ?

Thanks,

Posts: 10
Joined: 08/13/2007
Uber Donor

I created a mashup of the affiliate module on drupal.org and some of the techniques provided by VitaLife. The way I'm tracking affiliate referrals is by the URL. I've setup a wildcard DNS entry (*.mydomain.com) so that users can use their username as their affiliate URL. (e.g. user_name.mydomain.com). The module verifies that the user is actually a valid referral, by role, and redirects to the default URL if they are not. If they are, a click is recorded in the database. BTW, this required that I use a wildcard SSL cert as well so that all secure pages worked using affiliate URLs. The way this is being handled could probably be a configuration setting so that if you can't use wildcard domain name, the url could be something like mydomain.com?a=affiliate_id as in the original affiliate module.

The other thing I added to my module was the ability to change the depth of affiliate tracking. VitaLife's solution was built for 2 levels. The way I've set it up, you could have only one level, or 10, and you can configure the percentage discount for each level.

Although I do have it working, it is still a work in progress and there is some cleanup still to be done. One big thing that still needs to be done is a way configure which products are "commissionable" and which are not. My temporary solution was to create separate product classes and do a check in the code. This needs more thought though. Still, I'm attaching it for anyone that's interested.

AttachmentSize
uc_affiliate.zip16.51 KB
Posts: 5
Joined: 12/03/2007

I love this work, you did great job on this. Of course with every compliment there is a question.

And here is mine, I am able to track the clicks but an having issues getting the commission running. I have one level on the commissions, 1, and 10 in the commission level field. I'm currently using paypal to process payments, using the paypal sandbox to test at this point. I have all authenticated users set-up to be able to act as an affiliate.. In the setting for the uc affilate module "payment status" is set to calculate commission when the payment status is "payment received", which I have confirmed for all the payments to be their current status.

I think the issue has something to do with the fact that I don't see any root level affiliates when I look at the affiliates link under store administration.

Given that everything is working exactly as you would expect I'm thinking I am missing something really basic here.

Thanks ahead for you help

Posts: 46
Joined: 10/02/2007
Bug FinderGetting busy with the Ubercode.

Thanks for the work getting uc_affiliate off the ground!

I've been working on implementing this on a site for one of our clients, and have made some fixes and improvements along the way. I'm attaching my latest working version.

AttachmentSize
uc_affiliate-20071221.tar.gz14.39 KB
Posts: 10
Joined: 12/28/2007

Thanks detour to share your work.

I'm working on testing your module.

Did you already has done smth for the dashbord ?

Once everything will work correctly I can make a french.po

After having completed a payment for an affiliate, I can see in his profile "my business" the order and the money earned. But when I go in the administer section : http://www.myDomain.com/admin/store/affiliate nothing is displayed. Is it normal ?

thanks again

Z.

Posts: 3
Joined: 01/02/2008

Hi,

Is there any way to get the affiliate module working with the "user referral" module instead of using sub-domains?

thank you.

Posts: 60
Joined: 09/04/2007

Hi. I just started to test this module (detour's update).
Before, I had a couple of subdomains, each one with a new Drupal + UC installation, and using different databases for each subdomain. So each subdomain was an affiliate store. I kept track of the orders placed at affiliate stores by controlling the orders sent to specific e-mail addresses (specified under 'Notification Settings'). Surely this is a very complicated way to do it, but then I found your contribution. I'm still trying to figure out a couple of things.

Will each new user (given that it is granted the 'Affiliate' role, which has the 'act as affiliate' permission enabled) be an affiliate? Then, the username will be the subdomain name, right? e.g. user 'John' will be the affiliate 'John' and his site will be john.mydomain.com? In that case, I guess that the subdomain + drupal installation + uc should be previously created, or? Users referred by John will check out at john.mydomain.com/cart/checkout?

Thanks for any help =)

Posts: 10
Joined: 12/28/2007

Very easy, very neat. Work great for me.

Will each new user (given that it is granted the 'Affiliate' role, which has the 'act as affiliate' permission enabled) be an affiliate?
yes

Then, the username will be the subdomain name, right? e.g. user 'John' will be the affiliate 'John' and his site will be john.mydomain.com?
yes

In that case, I guess that the subdomain + drupal installation + uc should be previously created, or?
You do your install for the site www.myDomain.com only once.

Users referred by John will check out at john.mydomain.com/cart/checkout?
Yes
Actually john.mydomain.com/cart/checkout will execute exactely the same code as www.mydomain.com/cart/checkout on the same DB
But in the code there is catch when you use the subdomain.

Thanks for any help =)
you're welcom

Zitun

Posts: 10
Joined: 08/13/2007
Uber Donor

It seems like there's enough interest in this module, so I've went ahead and stuck the latest version from detour in a contrib module over here: http://www.ubercart.org/contrib/2446.

Please direct future comments, feedback, and suggestions to the contrib page so we can start getting them in one place.

Posts: 46
Joined: 10/02/2007
Bug FinderGetting busy with the Ubercode.

@Zitun,

I haven't done anything with the dashboard yet, and I'm not sure what would be helpful to have there that is not already under the other 'my business' sections.

Also, as you mentioned, the administrator pages for uc_affiliate only show summaries of the visits and new users -- they do not show any of the sales information, which is only available through the user profile pages.

It would be excellent to have a French translation. I'll include a translation template in a future release.

@Mariano,

Just to clarify, uc_affiliate does not use separate Drupal installations/databases, but a single installation set up for multi-sites (see: http://drupal.org/getting-started/5/install/multi-site). Since Drupal will use the default site directory for any hostnames not matched by other site directories, it can handle new subdomains without a new installation. (Your DNS must be set up to handle wildcard domains so that john.mysite.com is handled by the same server/account as www.mysite.com.)

@jhuckabee,

Thanks for setting up a module contrib page for this.

Posts: 2
Joined: 05/27/2008

hi,

it's all lokk here great but i have some difficulties:

1. website http://www.vitalifefitness.com/affiliate/ links are not working and i can't see what are on these pages. maybe someone can write code of this sites here?
2. if i install "affiliate" module, should i make also these code changes?
3. if i should make codes changes, in wich files i must do that?

web.take.away

Posts: 5
Joined: 06/11/2008

Hi there, thanks for this fantastic module but I can't seem to figure out how to create the affiliate links for my site. I don't see any documentation about it and I can't seem to find the page where you can create your text links or image links.

I tried going to http://mysite.com/affiliate/textlink but I get to "page not found"

Can you please help me

Thanks

Posts: 127
Joined: 08/07/2007