Browse Source

Add internal campaign tracking

master
Alice 5 years ago
parent
commit
f5483c1ad7
6 changed files with 66 additions and 2 deletions
  1. +1
    -0
      ccvpn/settings.py
  2. +3
    -2
      lambdainst/admin.py
  3. +39
    -0
      lambdainst/middleware.py
  4. +20
    -0
      lambdainst/migrations/0002_vpnuser_campaign.py
  5. +1
    -0
      lambdainst/models.py
  6. +2
    -0
      lambdainst/views.py

+ 1
- 0
ccvpn/settings.py View File

@@ -57,6 +57,7 @@ MIDDLEWARE_CLASSES = (
'django.middleware.security.SecurityMiddleware',
'django.middleware.locale.LocaleMiddleware',
'lambdainst.middleware.ReferrerMiddleware',
'lambdainst.middleware.CampaignMiddleware',
)

ROOT_URLCONF = 'ccvpn.urls'


+ 3
- 2
lambdainst/admin.py View File

@@ -9,6 +9,7 @@ from django.utils.translation import ugettext as _
from lambdainst.models import VPNUser, GiftCode, GiftCodeUser
from . import core


def make_user_link(user):
change_url = resolve_url('admin:auth_user_change', user.id)
return '<a href="%s">%s</a>' % (change_url, user.username)
@@ -31,8 +32,8 @@ class VPNUserInline(admin.StackedInline):
fk_name = 'user'

fields = ('notes', 'expiration', 'last_expiry_notice', 'notify_expiration',
'trial_periods_given', 'referrer_a', 'last_vpn_auth')
readonly_fields = ('referrer_a', 'last_vpn_auth')
'trial_periods_given', 'referrer_a', 'campaign', 'last_vpn_auth')
readonly_fields = ('referrer_a', 'last_vpn_auth', 'campaign')

def referrer_a(self, object):
if not object.referrer:


+ 39
- 0
lambdainst/middleware.py View File

@@ -1,4 +1,5 @@
from datetime import datetime, timedelta
import re

from django.conf import settings
from .models import User
@@ -40,3 +41,41 @@ class ReferrerMiddleware():
secure=settings.SESSION_COOKIE_SECURE or None)
return response


class CampaignMiddleware():
GET_FIELDS = ['pk_campaign', 'utm_campaign', 'utm_medium', 'utm_source']

def _get_name(self, request):
for f in self.GET_FIELDS:
if f in request.GET and request.GET[f]:
return request.GET[f]
if 'campaign' in request.COOKIES:
return request.COOKIES['campaign']
return None

def process_request(self, request):
name = self._get_name(request)
if not name:
return

name = name.strip()

if len(name) >= 64 or not re.match('^[a-zA-Z0-9_.:-]+$', name):
return

request.session['campaign'] = name

def process_response(self, request, response):
name = request.session.get('campaign')
if not name:
return response

max_age = 365 * 24 * 60 * 60
expires = (datetime.utcnow() + timedelta(seconds=max_age))
expires = expires.strftime("%a, %d-%b-%Y %H:%M:%S GMT")
response.set_cookie('campaign', name,
max_age=max_age,
expires=expires,
domain=settings.SESSION_COOKIE_DOMAIN,
secure=settings.SESSION_COOKIE_SECURE or None)
return response

+ 20
- 0
lambdainst/migrations/0002_vpnuser_campaign.py View File

@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-04-01 18:50
from __future__ import unicode_literals

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('lambdainst', '0001_initial'),
]

operations = [
migrations.AddField(
model_name='vpnuser',
name='campaign',
field=models.CharField(blank=True, max_length=64, null=True),
),
]

+ 1
- 0
lambdainst/models.py View File

@@ -41,6 +41,7 @@ class VPNUser(models.Model):
referrer = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL,
related_name='referrals')
referrer_used = models.BooleanField(default=False)
campaign = models.CharField(blank=True, null=True, max_length=64)

@property
def is_paid(self):


+ 2
- 0
lambdainst/views.py View File

@@ -85,6 +85,8 @@ def signup(request):
except User.DoesNotExist:
pass

user.vpnuser.campaign = request.session.get('campaign')

user.vpnuser.save()

user.backend = 'django.contrib.auth.backends.ModelBackend'


Loading…
Cancel
Save