Git Product home page Git Product logo

django-hype's Introduction

Hype

A Django module that implements referral link logic.

Concepts

This library implements two models:

  • The ReferralLink. This object represents a user's referral link, or invitation link. It has a string identifier which allows the user to share their link as /ref/<refid>/.
  • The ReferralHit. This is an instance of a user (logged in or anonymous) actually following a referral link.

The Anonymous Cookie

When a ReferralLink is followed, a ReferralHit object is created. If the link was followed by a logged in user, that user will be available on the ReferralHit object as a foreign key. If the link was followed by an anonymous user, a cookie will be set on the user for future reference.

The cookie contains a random UUID which is set on the ReferralHit. At any time, you may get that cookie and, should the user log in, update all ReferralHit objects with that matching UUID. The library includes a middleware which will automatically do this for every logged in users, see hype.middleware.AnonymousReferralMiddleware.

Confirming Referrals

You may wish to implement a SuccessfulReferral model which is created when a user who previously followed a ReferralLink (and thus created a ReferralHit) actually completes whichever steps the referral system requires referred users to complete (for example: Register to the website, make their first purchase, post their first comment, ...).

The ReferralHit model also has a confirmed DateTimeField which you may use for this purpose.

Supporting Referral Links on Any URL

Implementers may find it useful to allow a referral on any URL. This is implemented in the hype.middleware.ReferralLinkMiddleware middleware, which looks at all GET requests and, should a valid referral link be present in the GET parameters, redirects to that referral link's URL with the next parameter set to the original URL, without the referral link present.

Example:

  • /accounts/signup/?ref=abc123def redirects to...
  • /ref/abs123def?next=/accounts/signup/ which redirects to...
  • /accounts/signup/, after creating a ReferralHit.

Setup and configuration

  1. Install via python -m pip install django-hype
  2. Add hype to your INSTALLED_APPS
  3. Include hype.urls in your URLs. Example: url(r"^ref/", include("hype.urls"))
  4. Add hype.middleware.AnonymousReferralMiddleware to your MIDDLEWARE. This is required to update referrals for anonymous users when they log in.
  5. (optional) Add hype.middleware.ReferralLinkMiddleware to your MIDDLEWARE. This is required if you want ?ref=... to redirect properly.

These steps are enough to start gathering referral information. You create a referral link, and watch the ReferralHit table fill up as users follow it.

In addition to having that data, you may want to "confirm" referrals. The ConfirmedReferral model is there as a convenience model to allow you to filter down the referral hits in question. Upon creating a ConfirmedReferral you may also want to do something else, such as crediting a user some points. The atomicity and idempotency of such events is left as an exercise for the reader.

License

This project is licensed under the MIT license. The full license text is available in the LICENSE file.

django-hype's People

Contributors

jleclanche avatar joolean avatar justinmayer avatar beheh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.