Django: Implementing a referral program
Asked Answered
K

4

12

I have an ecommerce website that works in a classical way: people sign up, buy a product with their CC.

It uses the default Django auth system for users and registration and session framework.

Now I want to implement a referral system to my system where people can invite other people by giving their referral link (ie mysite.com/referral/123123/ ) and if a person signs up AND buys an item, the inviter gets 5$.

How can this the implemented ? For instance:

  • After the new user enters the site with the referral link, how can I keep track of this user with the referrer ? Saving his referrer to the session ?

  • What modification should I do to the built-in django user model to track these referrers and referred ones?

Kolva answered 25/3, 2010 at 16:50 Comment(0)
E
4

Define a special set of URLs (in your urls.py) for referral links. Pass them through a Python function that registers the referral and then send them to the normal view with the referral number as a parameter.

Is this what you had in mind, or what else would you like to know?

Eardrum answered 25/3, 2010 at 16:56 Comment(0)
C
8

I have done this a while back and figured my notes could help someone looking for this:

Create referral urls

Whenever you issue a referral url to your user, store it in a table. In my case I had urls for different purposes ('invite your friends', 'share this item' etc.) So I stored the type of referral as well. Add an entry point to your site like http://example.com/ref/jklejis to process the incoming url. If you don't like to have an obvious 'referral' url you could use middleware to simply capture a special url parameter on any url to your site and process it that way i.e. http://example.com/items/123?r=jklejis

Track referred visitor

As soon as a visitor visits from a referral I plant a cookie and any further requests by that user are tracked by a piece of middleware that tracks the actions in a table. I then run a job to parse the 'action' table to issue credits to users. I had no real-time requirements here, but if you do then use a signal to specific actions to kick off your code.

I recently came across this project which seems to be still 'alpha' but it does something similar and could get you started: https://github.com/pinax/pinax-referrals

Chasten answered 13/12, 2011 at 16:35 Comment(0)
Y
7

You could use a very simple model to keep track of them

class UserReferral(models.Model):
    referrer = models.ForeignKey(User)
    referred = models.ForeignKey(User)

    class Meta:
        unique_together = (('referrer', 'referred'),)

Then you can count how many a user has referred:

UserReferral.objects.filter(referrer=user).count()

Etc..

Ylangylang answered 3/11, 2010 at 9:24 Comment(2)
what about using a one-to-one key from user to user. Which one is better?Metaphase
@AbhijithK That would not be ideal as one user can refer multiple and likewise a user can get referrals from numerous people. Say for example you run a promotion to get people to join your app. After a period of user activity decline you run another promotion to reignite their interest in the app.Serna
E
4

Define a special set of URLs (in your urls.py) for referral links. Pass them through a Python function that registers the referral and then send them to the normal view with the referral number as a parameter.

Is this what you had in mind, or what else would you like to know?

Eardrum answered 25/3, 2010 at 16:56 Comment(0)
M
3

Keeping track of a user's session especially after they cross the boundry of signup/login is a tricky issue. I have written a referrals app http://paltman.com/how-to-easily-add-referrals-to-a-website/.

Message answered 17/8, 2012 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.