diff --git a/analytical/optimizely/__init__.py b/analytical/optimizely/__init__.py new file mode 100644 index 0000000..3deb75e --- /dev/null +++ b/analytical/optimizely/__init__.py @@ -0,0 +1,92 @@ +""" +========================= +Optimizely -- A/B testing +========================= + +Optimizely_ is an easy way to implement A/B testing. Try different +decisions, images, layouts, and copy without touching your website code +and see exactly how your experiments are affecting pageviews, +retention and sales. + +.. _Optimizely: http://www.optimizely.com/ + + +.. optimizely-installation: + +Installation +============ + +You only need to do perform these steps if you are not using the +generic :ttag:`analytical.*` tags. If you are, skip to +:ref:`optimizely-configuration`. + +In order to use the template tag, you need to add +:mod:`analytical.optimizely` to the installed applications list in the +project :file:`settings.py` file:: + + INSTALLED_APPS = [ + ... + 'analytical.optimizely', + ... + ] + +The Optimizely Javascript code is inserted into templates using a +template tag. Load the :mod:`mixpanel` template tag library and insert +the :ttag:`optimizely` tag. Because every page that you want to track +must have the tag, it is useful to add it to your base template. +Insert the tag at the top of the HTML head:: + + {% load optimizely %} + + + {% optimizely %} + ... + + +.. _optimizely-configuration: + +Configuration +============= + +Before you can use the Optimizely integration, you must first set your +account number. + + +.. _optimizely-account-number: + +Setting the account number +-------------------------- + +Optimizely gives you a unique account number, and the :ttag:`optimizely` +tag will include it in the rendered Javascript code. You can find your +account number by clicking the `Implementation` link in the top +right-hand corner of the Optimizely website. A pop-up window will +appear containing HTML code looking like this:: + + + +The number ``XXXXXXX`` is your account number. Set +:const:`OPTIMIZELY_ACCOUNT_NUMBER` in the project :file:`settings.py` +file:: + + OPTIMIZELY_ACCOUNT_NUMBER = 'XXXXXXX' + +If you do not set an account number, the Javascript code will not be +rendered. + + +.. _optimizely-internal-ips: + +Internal IP addresses +--------------------- + +Usually you do not want to use A/B testing on your development or +internal IP addresses. By default, if the tags detect that the client +comes from any address in the :const:`INTERNAL_IPS` setting, the +tracking code is commented out. See :const:`ANALYTICAL_INTERNAL_IPS` +for important information about detecting the visitor IP address. +""" + +optimizely_service = { + 'head_top': 'analytical.optimizely.templatetags.optimizely.OptimizelyNode', +} diff --git a/analytical/optimizely/templatetags/__init__.py b/analytical/optimizely/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/analytical/optimizely/templatetags/optimizely.py b/analytical/optimizely/templatetags/optimizely.py new file mode 100644 index 0000000..ff09034 --- /dev/null +++ b/analytical/optimizely/templatetags/optimizely.py @@ -0,0 +1,43 @@ +""" +Optimizely template tags. +""" + +import re + +from django.template import Library, Node, TemplateSyntaxError + +from analytical.utils import is_internal_ip, disable_html + + +ACCOUNT_NUMBER_RE = re.compile(r'^\d{7}$') +SETUP_CODE = """""" + + +register = Library() + + +@register.tag +def optimizely(parser, token): + """ + Optimizely template tag. + + Renders Javascript code to set-up A/B testing. You must supply + your Optimizely account number in the ``OPTIMIZELY_ACCOUNT_NUMBER`` + setting. + """ + bits = token.split_contents() + if len(bits) > 1: + raise TemplateSyntaxError("'%s' takes no arguments" % bits[0]) + return OptimizelyNode() + +class OptimizelyNode(Node): + def __init__(self): + self.account_number = self.get_required_setting( + 'OPTIMIZELY_ACCOUNT_NUMBER', ACCOUNT_NUMBER_RE, + "must be a string containing an seven-digit number") + + def render(self, context): + html = SETUP_CODE % {'account_number': self.account_number} + if is_internal_ip(context): + html = disable_html(html, 'Optimizely') + return html diff --git a/analytical/services/optimizely.py b/analytical/services/optimizely.py deleted file mode 100644 index e4b9ae0..0000000 --- a/analytical/services/optimizely.py +++ /dev/null @@ -1,21 +0,0 @@ -""" -Optimizely service. -""" - -import re - -from analytical.services.base import AnalyticalService - - -ACCOUNT_NUMBER_RE = re.compile(r'^\d{7}$') -SETUP_CODE = """""" - - -class OptimizelyService(AnalyticalService): - def __init__(self): - self.account_number = self.get_required_setting( - 'OPTIMIZELY_ACCOUNT_NUMBER', ACCOUNT_NUMBER_RE, - "must be a string containing an seven-digit number") - - def render_head_top(self, context): - return SETUP_CODE % {'account_number': self.account_number} diff --git a/docs/services/optimizely.rst b/docs/services/optimizely.rst index c31d327..b0af136 100644 --- a/docs/services/optimizely.rst +++ b/docs/services/optimizely.rst @@ -1,25 +1,3 @@ -Optimizely -- A/B testing -========================= +.. currentmodule:: analytical.optimizely -Optimizely_ is an easy way to implement A/B testing. Try different -decisions, images, layouts, and copy without touching your website code -and see exactly how your experiments are affecting pagevieuws, -retention and sales. - -.. _Optimizely: http://www.optimizely.com/ - - -Required settings ------------------ - -.. data:: OPTIMIZELY_ACCOUNT_NUMBER - - The website project token :: - - OPTIMIZELY_ACCOUNT_NUMBER = '1234567' - - You can find your account number by clicking the `Implementation` link - in the top right-hand corner of the Optimizely website. A pop-up - window will appear containing HTML code looking like this: - ````. - The number ``XXXXXXX`` is your account number. +.. automodule:: analytical.optimizely