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.
There are multiple ways that anafero can be used. We have used anafero specifically in these various contexts:
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:
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:
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.
<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](https://github.com/pinax/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.