Over a million developers have joined DZone.

Solution behind the referral marketing system

DZone's Guide to

Solution behind the referral marketing system

· Web Dev Zone ·
Free Resource

Learn how to add document editing and viewing to your web app on .Net (C#), Node.JS, Java, PHP, Ruby, etc.

In order to attract users, services sometimes use affiliate programs. This idea behind this is simple: ask subscribed users to invite their friends to join the system and start to use your service. There are some bonuses for each user that brings you new users (recharge, increased functionality, renewals, etc.).

Here comes our tech experience of how we implemented these features in our service:


1) A user is registered in our service and he is assigned a random referral code. This code is inserted in the invitation link like "http://example.com".

2) User A sends the link to a user B (sends a private message, shares in social networks, etc.)

3) When user B enters the website by the link, this code is memorized in the Local Storage Browser. Now, even if the user B closes the tab and goes down to our service later by the link "http://example.com", we'll still know about who invited him.

4) During registration this code is sent to the server. According to it, user A is found in the database, and a new object is created in a separate table 'Affiliate'.

5) User A is given some bonus.

Technical part

As a backend-side in our BitCalm project we used Django. With this framework it is very easy to implement what we need.

Creating a field with a referral code in the User

class User(AbstractUser):
     affiliate_code = models.CharField(_('Affiliate code'), max_length=16, 

When you create a new user, this field is filled with a code generated the function generate_key.

Record referral code in Local Storage

In the script on the front page, we check whether there is a key we need in the URL address. If it exists, then write it in the Local Storage.

(function () {
    var getParameterByName = function (name) {
            name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
            var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
                results = regex.exec(location.search);
            return results === null ? null : decodeURIComponent(results[1].replace(/\+/g, " "));
        affiliateCode = getParameterByName('ref');
    if (!('affiliateCode' in localStorage)) {
        if (affiliateCode && affiliateCode.length) {
            localStorage['affiliateCode'] = affiliateCode;
        } else {
            localStorage['affiliateCode'] = null;

Sending data code when registering

When a user is registering, apart from the login and password we also send data about referral code to the server.

    name: $scope.user.name || '',
    login: $scope.user.login,
    password: $scope.user.password,
    affiliateCode: localStorage.affiliateCode ? localStorage.affiliateCode : ''

Read more about how our authorization system works in our blog.

Processing data on the server

For convenient work with the mechanism of affiliate programs we created a model of Affiliate:

class Affiliate(models.Model):
    date = models.DateTimeField(default=timezone.now)
    owner = models.ForeignKey(User, related_name='owned_affiliates')
    member = models.ForeignKey(User, related_name='participating_affiliates')

This model relates two users and adds the date of creation. The owner field is a reference to the user to invite a friend, and is placed in the current user, which was registered by this link.

When you register a new user with a non-empty field affiliateCode, we are checking the database for a user that owns the code:

def signup(request):
    data = json.loads(request.body)


        affiliate_code = data['affiliateCode']
        owner = User.objects.get(affiliate_code__iexact=affiliate_code)
        Affiliate.objects.create(owner=owner, member=user)
    except (KeyError, User.DoesNotExist) as e:

Now we can get information about all the invited users of a specific user by referring to the field owned_affiliates.

Thanks to participating_affiliates we can understand, who invited this particular user.

For the calculation of some bonus or other actions we can use receiver for the model Affiliate:

@receiver(post_save, sender=Affiliate)
def client_receiver(sender, instance, created, **kwargs):
    if created:


The introduction of such a simple system took a long time, but it works great and solves all the necessary tasks. In addition, it is easy enough to expand it and introduce new features, that how we do in our project.

Extend your web service functionality with docx, xlsx and pptx editing. Check out ONLYOFFICE document editors for integration.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}