General A/B testing.
authorRadek Czajka <rczajka@rczajka.pl>
Fri, 15 Nov 2019 14:42:40 +0000 (15:42 +0100)
committerRadek Czajka <rczajka@rczajka.pl>
Fri, 15 Nov 2019 14:42:40 +0000 (15:42 +0100)
requirements/requirements.txt
src/wolnelektury/abtests.py [new file with mode: 0644]
src/wolnelektury/settings/apps.py
src/wolnelektury/settings/basic.py
src/wolnelektury/settings/custom.py
src/wolnelektury/templates/piwik/tracking_code.html [new file with mode: 0644]
src/wolnelektury/templatetags/piwik_tags.py [new file with mode: 0644]

index 08c6882..b0c3c8e 100644 (file)
@@ -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 (file)
index 0000000..7fde579
--- /dev/null
@@ -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}
index 5b55b04..81756be 100644 (file)
@@ -53,7 +53,6 @@ INSTALLED_APPS_CONTRIB = [
     'rest_framework',
     'fnp_django_pagination',
     'pipeline',
-    'piwik',
     'sorl.thumbnail',
     'honeypot',
     'fnpdjango',
index 4e21244..bef8cd9 100644 (file)
@@ -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',
         ),
index 0b8772b..280f539 100644 (file)
@@ -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 (file)
index 0000000..f09a5c6
--- /dev/null
@@ -0,0 +1,15 @@
+<!-- Piwik --> 
+<script type="text/javascript">
+  var pkBaseURL = "{{ url }}";
+document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
+</script><script type="text/javascript">
+try {
+var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", {{ id }});
+{% for ab, abval in AB.items %}
+  piwikTracker.setCustomVariable({{ forloop.counter }}, "{{ ab }}", "{{ abval }}", "page");
+{% endfor %}
+piwikTracker.trackPageView();
+piwikTracker.enableLinkTracking();
+} catch( err ) {}
+</script><noscript><p><img src="{{ url }}piwik.php?idsite={{ id }}" style="border:0" alt="" /></p></noscript>
+<!-- End Piwik Tracking Code -->
diff --git a/src/wolnelektury/templatetags/piwik_tags.py b/src/wolnelektury/templatetags/piwik_tags.py
new file mode 100644 (file)
index 0000000..551b81d
--- /dev/null
@@ -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')}