Patrick Altman

I am the VP of Engineering at Eldarion, Co-Founder of Amino Software, and a Mentor at jumpstart foundry.

I am equally passionate about writing code as I am about building businesses.

You really should

Posted On

Aug. 17, 2012, 3:28 p.m.

Tags

Do you have an iOS or web application that you'd like help with?

Eldarion offers lean coaching, software development, code reviews, and in some cases staff augmentation.

Give me a call at 615-300-2930 or send us an email and let's see how we can help you.

This site is hosted on

You should consider it for your Django/Python web hosting as well!

How to Easily Add Referrals to a Website

Referrals are a popular and very effective way to generate quality traffic while allowing users that love your site to become promoters. We built anafero because we were increasingly wanting to deploy referral systems on more of our sites as well as sites for some of our clients.

Multiple Uses

There are multiple ways that anafero can be used. We have used anafero specifically in these various contexts:

  1. Generate one referral per user and just display a simple number to them to show how many responses they have generated.
  2. Setup of specific activities that you want to track and report to the referring user (e.g. not just that respondents clicked on the link but that they signed up as well).
  3. Allow users to generate their own referrals, naming them, so that they can track different channels and what methods of promoting the site are working for them.
  4. Let users use generated referral links to act as semi-private share links that unlocks private content for the visitor but only when they get to the site using the private referral link.
  5. Setup thresholds that when a referring user surpasses certain goals they receive an award. This is what we are doing on Gondor (you can see my referral link in the left hand side of this blog where I am telling you that you should host your Python web projects there). If you signup as a result of using that link, I am rewarded with a credit on my account worth 5% of what you spend.

Using anafero

Let's see how we'd take anafero and add the ability for each one of our site users to have a referral code created unique to them and providing feedback how many people they referred did the following:

  • clicked on the link
  • signed up
  • uploaded a photo

First things first, we need to add anafero to our project:

$ pip install anafero  # even better, put anafero==0.7 in your requirements.txt file

Then configure your settings.py:

INSTALLED_APS = [
    ...
    "anafero",
]

MIDDLEWARE_CLASSES = [
    ...
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    ...
    "anafero.middleware.SessionJumpingMiddleware",
    ...
]

Lastly, you will want to add this to your urls.py:

url(r"^r/", include("anafero.urls")),

Now that we have it installed and configured in our site, we need to actually integrate it. This involves the following steps:

  1. Hook up a receiver for the django-user-accounts user_signed_up signal
  2. Expose in a template somewhere the referral URL for your users to copy and paste
  3. Log the signup and upload a photo actions

Hook Up Receiver

from django.dispatch import receiver

from account.signals import user_signed_up
@receiver(user_signed_up) def handle_user_signed_up(sender, user, form, **kwargs): Referral.create( redirect_to=user.profile.get_absolute_url() )

Now that every user will have a referral code associated with them we should setup a template to expose that url so it's easy for them to copy and paste in order to distribute however they see fit.

The Template

<section class="referral">
    <h2>Your Referral Link</h2>
    <p>{{ user.referral_codes.all.0.url }}</p>
</section>

Lastly, we need to hook up some actions. Actions are just strings that you specify to record activity of users that enter the system with a referral code. Currently, this is limited to contexts where you have the request object available. Out of the box, anafero only tracks the initial responded action.

In order to record the signup action we cannot just add some code to the signal above because django-user-accounts  does not send the request object with the signal. So in order to do this we need to subclass the SignupView and pass it in a url override. Don't worry, it's all pretty simple:

# views.py
from account.views import SignupView as PinaxSignupView
from anafero.models import Referral


class SignupView(PinaxSignupView): def after_signup(self, form): super(SignupView, self).after_signup(form) Referral.record_response(self.request, "USER_SIGNUP")
# urls.py from .views import SignupView ... url(r"account/signup/$", SignupView.as_view(), name="account_signup"), url(r"account/", include("account.urls")), ...

Now elsewhere in your site where you have a view that is handling photo uploads you can add the other action:

from anafero.models import Referral

def upload_photo(request): # ... do upload handling and other stuff Referral.record_response(request, "UPLOAD_PHOTO") # ... return the right HttpResponse

There are tons of other cool things like overriding the way codes are generated, which you can read more about in the anafero documentation.

I hope you have found this introduction of anafero useful and inspiring. Referrals can be a lot of fun and anafero should make it easy to add into your site.

If you have any trouble let me know. If you have a project that needs building, then by all means let's chat and see how Eldarion can help you.

Feedback and Commentary

blog comments powered by Disqus