Updated HubSpot tracking code. Updated test/docs/changelog to reflect this. HUBSPOT_DOMAIN is no longer required.

This commit is contained in:
Craig Bruce 2014-05-26 16:57:49 -06:00
parent 8561069582
commit 1ebe4f676b
4 changed files with 37 additions and 45 deletions

View file

@ -1,3 +1,7 @@
Version 0.17.2
--------------
* Update HubSpot code (Craig Bruce)
Version 0.17.1
--------------
* Fix typo in Intercom.io support (Steven Skoczen)

View file

@ -12,44 +12,42 @@ from analytical.utils import is_internal_ip, disable_html, get_required_setting
PORTAL_ID_RE = re.compile(r'^\d+$')
DOMAIN_RE = re.compile(r'^[\w.-]+$')
TRACKING_CODE = """
<script type="text/javascript" language="javascript">
var hs_portalid = %(portal_id)s;
var hs_salog_version = "2.00";
var hs_ppa = "%(domain)s";
document.write(unescape("%%3Cscript src='" + document.location.protocol + "//" + hs_ppa + "/salog.js.aspx' type='text/javascript'%%3E%%3C/script%%3E"));
</script>
<!-- Start of Async HubSpot Analytics Code -->
<script type="text/javascript">
(function(d,s,i,r) {
if (d.getElementById(i)){return;}
var n=d.createElement(s),e=d.getElementsByTagName(s)[0];
n.id=i;n.src='//js.hs-analytics.net/analytics/'+(Math.ceil(new Date()/r)*r)+'/%(portal_id)s.js';
e.parentNode.insertBefore(n, e);
})(document,"script","hs-analytics",300000);
</script>
<!-- End of Async HubSpot Analytics 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``.
your portal ID (as a string) in the ``HUBSPOT_PORTAL_ID`` setting.
"""
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.portal_id = get_required_setting('HUBSPOT_PORTAL_ID',
PORTAL_ID_RE, "must be a (string containing a) number")
self.domain = get_required_setting('HUBSPOT_DOMAIN',
DOMAIN_RE, "must be an internet domain name")
PORTAL_ID_RE, "must be a (string containing a) number")
def render(self, context):
html = TRACKING_CODE % {'portal_id': self.portal_id,
'domain': self.domain}
html = TRACKING_CODE % {'portal_id': self.portal_id}
if is_internal_ip(context, 'HUBSPOT'):
html = disable_html(html, 'HubSpot')
return html

View file

@ -10,7 +10,7 @@ from analytical.tests.utils import TagTestCase, override_settings, SETTING_DELET
from analytical.utils import AnalyticalException
@override_settings(HUBSPOT_PORTAL_ID='1234', HUBSPOT_DOMAIN='example.com')
@override_settings(HUBSPOT_PORTAL_ID='1234')
class HubSpotTagTestCase(TagTestCase):
"""
Tests for the ``hubspot`` template tag.
@ -18,13 +18,13 @@ class HubSpotTagTestCase(TagTestCase):
def test_tag(self):
r = self.render_tag('hubspot', 'hubspot')
self.assertTrue('var hs_portalid = 1234;' in r, r)
self.assertTrue('var hs_ppa = "example.com";' in r, r)
self.assertTrue("n.id=i;n.src='//js.hs-analytics.net/analytics/'+(Math.ceil(new Date()/r)*r)+'/1234.js';"
in r, r)
def test_node(self):
r = HubSpotNode().render(Context())
self.assertTrue('var hs_portalid = 1234;' in r, r)
self.assertTrue('var hs_ppa = "example.com";' in r, r)
self.assertTrue("n.id=i;n.src='//js.hs-analytics.net/analytics/'+(Math.ceil(new Date()/r)*r)+'/1234.js';"
in r, r)
@override_settings(HUBSPOT_PORTAL_ID=SETTING_DELETED)
def test_no_portal_id(self):
@ -34,20 +34,11 @@ class HubSpotTagTestCase(TagTestCase):
def test_wrong_portal_id(self):
self.assertRaises(AnalyticalException, HubSpotNode)
@override_settings(HUBSPOT_DOMAIN=SETTING_DELETED)
def test_no_domain(self):
self.assertRaises(AnalyticalException, HubSpotNode)
@override_settings(HUBSPOT_DOMAIN='wrong domain')
def test_wrong_domain(self):
self.assertRaises(AnalyticalException, HubSpotNode)
@override_settings(ANALYTICAL_INTERNAL_IPS=['1.1.1.1'])
def test_render_internal_ip(self):
req = HttpRequest()
req.META['REMOTE_ADDR'] = '1.1.1.1'
context = Context({'request': req})
r = HubSpotNode().render(context)
self.assertTrue(r.startswith(
'<!-- HubSpot disabled on internal IP address'), r)
self.assertTrue(r.startswith('<!-- HubSpot disabled on internal IP address'), r)
self.assertTrue(r.endswith('-->'), r)

View file

@ -3,7 +3,7 @@ HubSpot -- inbound marketing
============================
HubSpot_ helps you get found by customers. It provides tools for
content creation, convertion and marketing analysis. HubSpot uses
content creation, conversion and marketing analysis. HubSpot uses
tracking on your website to measure effect of your marketing efforts.
.. _HubSpot: http://www.hubspot.com/
@ -43,29 +43,28 @@ Configuration
=============
Before you can use the HubSpot integration, you must first set your
portal ID and domain.
portal ID, also known as your Hub ID.
.. _hubspot-portal-id:
Setting the portal ID and domain
--------------------------------
Setting the portal ID
---------------------
Your HubSpot account has its own portal ID and primary domain name, 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::
Your HubSpot account has its own portal ID, the :ttag:`hubspot` tag
will include them in the rendered JavaScript code. You can find the
portal ID by accessing your dashboard. Alternatively, read this
`Quick Answer page <http://help.hubspot.com/articles/KCS_Article/Where-can-I-find-my-HUB-ID>`_.
Set :const:`HUBSPOT_PORTAL_ID` 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.
If you do not set the portal ID, the tracking code will not be rendered.
.. deprecated:: 0.17.2
`HUBSPOT_DOMAIN` is no longer required.
.. _hubspot-internal-ips:
Internal IP addresses