From fc0b706f8c7eb67531df0a2acd972412e47c7010 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Fri, 15 Nov 2019 15:42:40 +0100 Subject: [PATCH] General A/B testing. --- requirements/requirements.txt | 1 - src/wolnelektury/abtests.py | 13 ++++++++++++ src/wolnelektury/settings/apps.py | 1 - src/wolnelektury/settings/basic.py | 1 + src/wolnelektury/settings/custom.py | 4 ++++ .../templates/piwik/tracking_code.html | 15 +++++++++++++ src/wolnelektury/templatetags/piwik_tags.py | 21 +++++++++++++++++++ 7 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/wolnelektury/abtests.py create mode 100644 src/wolnelektury/templates/piwik/tracking_code.html create mode 100644 src/wolnelektury/templatetags/piwik_tags.py diff --git a/requirements/requirements.txt b/requirements/requirements.txt index 08c6882b0..b0c3c8e86 100644 --- a/requirements/requirements.txt +++ b/requirements/requirements.txt @@ -40,7 +40,6 @@ pytz django-honeypot==0.7.0 python-memcached -django-piwik python-fb # Feedparser diff --git a/src/wolnelektury/abtests.py b/src/wolnelektury/abtests.py new file mode 100644 index 000000000..7fde579b0 --- /dev/null +++ b/src/wolnelektury/abtests.py @@ -0,0 +1,13 @@ +import hashlib +from django.conf import settings + + +def context_processor(request): + ab = {} + for abtest, nvalues in settings.AB_TESTS.items(): + print(abtest, nvalues) + ab[abtest] = hashlib.md5( + (abtest + request.META['REMOTE_ADDR']).encode('utf-8') + ).digest()[0] % nvalues + print(ab) + return {'AB': ab} diff --git a/src/wolnelektury/settings/apps.py b/src/wolnelektury/settings/apps.py index 5b55b049e..81756be76 100644 --- a/src/wolnelektury/settings/apps.py +++ b/src/wolnelektury/settings/apps.py @@ -53,7 +53,6 @@ INSTALLED_APPS_CONTRIB = [ 'rest_framework', 'fnp_django_pagination', 'pipeline', - 'piwik', 'sorl.thumbnail', 'honeypot', 'fnpdjango', diff --git a/src/wolnelektury/settings/basic.py b/src/wolnelektury/settings/basic.py index 4e21244d8..bef8cd9d1 100644 --- a/src/wolnelektury/settings/basic.py +++ b/src/wolnelektury/settings/basic.py @@ -60,6 +60,7 @@ TEMPLATES = [{ 'django.template.context_processors.media', 'django.template.context_processors.request', 'wolnelektury.context_processors.extra_settings', + 'wolnelektury.abtests.context_processor', 'search.context_processors.search_form', 'machina.core.context_processors.metadata', ), diff --git a/src/wolnelektury/settings/custom.py b/src/wolnelektury/settings/custom.py index 0b8772b95..280f53955 100644 --- a/src/wolnelektury/settings/custom.py +++ b/src/wolnelektury/settings/custom.py @@ -38,3 +38,7 @@ PAYU_POS = { CLUB_PAYU_POS = '300746' CLUB_PAYU_RECURRING_POS = '300746' CLUB_APP_HOST = None + +AB_TESTS = { + 'PAYLOGO': 2, +} diff --git a/src/wolnelektury/templates/piwik/tracking_code.html b/src/wolnelektury/templates/piwik/tracking_code.html new file mode 100644 index 000000000..f09a5c6e5 --- /dev/null +++ b/src/wolnelektury/templates/piwik/tracking_code.html @@ -0,0 +1,15 @@ + + + diff --git a/src/wolnelektury/templatetags/piwik_tags.py b/src/wolnelektury/templatetags/piwik_tags.py new file mode 100644 index 000000000..551b81d31 --- /dev/null +++ b/src/wolnelektury/templatetags/piwik_tags.py @@ -0,0 +1,21 @@ +"""Piwik template tag.""" + +from django import template +from django.conf import settings +from django.core.exceptions import ImproperlyConfigured + + +register = template.Library() + + +@register.inclusion_tag('piwik/tracking_code.html', takes_context=True) +def tracking_code(context): + try: + id = settings.PIWIK_SITE_ID + except AttributeError: + raise ImproperlyConfigured('PIWIK_SITE_ID does not exist.') + try: + url = settings.PIWIK_URL + except AttributeError: + raise ImproperlyConfigured('PIWIK_URL does not exist.') + return {'id': id, 'url': url, 'AB': context.get('AB')} -- 2.20.1