From 3d411e31d0786af24356b2c3bf55b97721d1b115 Mon Sep 17 00:00:00 2001 From: Joost Cassee Date: Thu, 27 Jan 2011 11:21:50 +0100 Subject: [PATCH] Add HubSpot integration This commit has not been tested. --- README.rst | 3 +- analytical/hubspot/__init__.py | 88 +++++++++++++++++++++ analytical/hubspot/templatetags/__init__.py | 0 analytical/hubspot/templatetags/hubspot.py | 53 +++++++++++++ analytical/templatetags/analytical.py | 1 + 5 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 analytical/hubspot/__init__.py create mode 100644 analytical/hubspot/templatetags/__init__.py create mode 100644 analytical/hubspot/templatetags/hubspot.py diff --git a/README.rst b/README.rst index f54b586..d6f1481 100644 --- a/README.rst +++ b/README.rst @@ -7,7 +7,8 @@ into a Django_ project. Currently supported services: * `Chartbeat`_ -- traffic analysis * `Clicky`_ -- traffic analysis * `Crazy Egg`_ -- visual click tracking -* `Google Analytics`_ traffic analysis +* `Google Analytics`_ -- traffic analysis +* `HubSpot`_ -- inbound marketing * `KISSinsights`_ -- feedback surveys * `KISSmetrics`_ -- funnel analysis * `Mixpanel`_ -- event tracking diff --git a/analytical/hubspot/__init__.py b/analytical/hubspot/__init__.py new file mode 100644 index 0000000..f14eb45 --- /dev/null +++ b/analytical/hubspot/__init__.py @@ -0,0 +1,88 @@ +""" +============================ +HubSpot -- inbound marketing +============================ + +HubSpot_ helps you get found by customers. It provides tools for +content creation, convertion and marketing analysis. HubSpot uses +tracking on your website to measure effect of your marketing efforts. + +.. _HubSpot: http://www.hubspot.com/ + + +.. hubspot-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:`hubspot-configuration`. + +In order to use the template tag, you need to add +:mod:`analytical.hubspot` to the installed applications list in the +project :file:`settings.py` file:: + + INSTALLED_APPS = [ + ... + 'analytical.hubspot', + ... + ] + +The HubSpot tracking code is inserted into templates using a template +tag. Load the :mod:`hubspot` template tag library and insert the +:ttag:`hubspot` 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 bottom of the HTML body:: + + {% load hubspot %} + ... + {% hubspot %} + + + + +.. _hubspot-configuration: + +Configuration +============= + +Before you can use the HubSpot integration, you must first set your +portal ID and domain. + + +.. _hubspot-portal-id: + +Setting the portal ID and domain +-------------------------------- + +Your HubSpot account has its own portal ID and primary websit, and the +:ttag:`hubspot` tag will include them in the rendered Javascript code. +You can find the portal ID and domain by going to the *Domains* tab in +your HubSpot account. The domain you need to use is listed as *Primary +Domain* on that page, and the portal ID can be found in the footer. Set +:const:`HUBSPOT_PORTAL_ID` and :const:`HUBSPOT_DOMAIN` in the +project :file:`settings.py` file:: + + HUBSPOT_PORTAL_ID = 'XXXX' + HUBSPOT_DOMAIN = 'XXXXXXXX.web101.hubspot.com' + +If you do not set the portal ID and domain, the tracking code will not +be rendered. + + +.. _hubspot-internal-ips: + +Internal IP addresses +--------------------- + +Usually you do not want to track clicks from 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. +""" + +hubspot_service = { + 'body_bottom': 'analytical.hubspot.templatetags.hupspot.HubSpotNode', +} diff --git a/analytical/hubspot/templatetags/__init__.py b/analytical/hubspot/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/analytical/hubspot/templatetags/hubspot.py b/analytical/hubspot/templatetags/hubspot.py new file mode 100644 index 0000000..0b89847 --- /dev/null +++ b/analytical/hubspot/templatetags/hubspot.py @@ -0,0 +1,53 @@ +""" +HubSpot template tags. +""" + +import re + +from django.template import Library, Node, TemplateSyntaxError + +from analytical.utils import get_required_setting, is_internal_ip, disable_html + + +PORTAL_ID_RE = re.compile(r'^\d+$') +DOMAIN_RE = re.compile(r'^[\w.-]+$') +TRACKING_CODE = """ + +""" + + +register = Library() + + +@register.tag +def hubspot(parser, token): + """ + HubSpot tracking template tag. + + Renders Javascript code to track page visits. You must supply + your portal ID (as a string) in the ``HUBSPOT_PORTAL_ID`` setting, + and the website domain in ``HUBSPOT_DOMAIN``. + """ + bits = token.split_contents() + if len(bits) > 1: + raise TemplateSyntaxError("'%s' takes no arguments" % bits[0]) + return HubSpotNode() + +class HubSpotNode(Node): + def __init__(self): + self.site_id = get_required_setting('HUPSPOT_PORTAL_ID', + PORTAL_ID_RE, "must be a (string containing a) number") + self.domain = get_required_setting('HUPSPOT_DOMAIN', + DOMAIN_RE, "must be an internet domain name") + + def render(self, context): + html = TRACKING_CODE % {'portal_id': self.portal_id, + 'domain': self.domain} + if is_internal_ip(context): + html = disable_html(html, 'HubSpot') + return html diff --git a/analytical/templatetags/analytical.py b/analytical/templatetags/analytical.py index c6a5f5f..195f5ec 100644 --- a/analytical/templatetags/analytical.py +++ b/analytical/templatetags/analytical.py @@ -16,6 +16,7 @@ DEFAULT_SERVICES = [ 'analytical.clicky.clicky_service', 'analytical.crazy_egg.crazy_egg_service', 'analytical.google_analytics.google_analytics_service', + 'analytical.hubspot.hubspot_service', 'analytical.kiss_insights.KissInsightsService', 'analytical.kiss_metrics.KissMetricsService', 'analytical.mixpanel.MixpanelService',