add 'domain' config option for tracking code

This commit is contained in:
Seb Bacon 2011-04-06 12:01:21 +01:00
parent 0ba01faed2
commit 0397483a8c
4 changed files with 27 additions and 17 deletions

View File

@ -14,7 +14,7 @@ Installation
:: ::
$ pip install -e hg+https://bitbucket.org/okfn/ckanext-googleanalytics#package=/ckanext-googleanalytics $ pip install -e hg+https://bitbucket.org/okfn/ckanext-googleanalytics#egg=ckanext-googleanalytics
2. Edit your development.ini (or similar) with: 2. Edit your development.ini (or similar) with:
@ -33,6 +33,7 @@ Installation
googleanalytics.show_downloads = true googleanalytics.show_downloads = true
googleanalytics.resource_prefix = /downloads/ googleanalytics.resource_prefix = /downloads/
googleanalytics.domain = auto
If ``show_downloads`` is set, a download count for resources will If ``show_downloads`` is set, a download count for resources will
be displayed on individual package pages. be displayed on individual package pages.
@ -43,6 +44,12 @@ Installation
filtering for all resources easier in the Google Analytics web filtering for all resources easier in the Google Analytics web
interface. interface.
``domain`` allows you to specify a domain against which Analytics
will track users. You will usually want to leave this as ``auto``;
if you are tracking users from multiple subdomains, you might want
to specify something like ``.mydomain.com``. See `Google's
documentation <http://code.google.com/apis/analytics/docs/gaJS/gaJSApiDomainDirectory.html#_gat.GA_Tracker_._setDomainName>` for more info.
3. Wait a day or so for some stats to be recorded in Google 3. Wait a day or so for some stats to be recorded in Google
4. Import Google stats by running the following command from 4. Import Google stats by running the following command from

View File

@ -1,15 +1,13 @@
gacode = u""" gacode = """
<script type="text/javascript"> <script type="text/javascript">
var _gaq = _gaq || []; var _gaq = _gaq || [];
_gaq.push(['_setAccount', '%s']); _gaq.push(['_setAccount', '%s']);
_gaq.push(['_trackPageview']); _gaq.push(['_trackPageview']);
_gaq.push(['_setDomainName', '%s']);
(function() { (function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})(); })();
</script> </script>
""" """

View File

@ -37,7 +37,8 @@ class GoogleAnalyticsPlugin(SingletonPlugin):
def filter(self, stream): def filter(self, stream):
log.info("Inserting GA code into template") log.info("Inserting GA code into template")
ga_id = self.config['googleanalytics.id'] ga_id = self.config['googleanalytics.id']
code = HTML(gacode % ga_id) ga_domain = self.config.get('googleanalytics.domain', 'auto')
code = HTML(gacode % (ga_id, ga_domain))
stream = stream | Transformer('head').append(code) stream = stream | Transformer('head').append(code)
resource_url = config.get('googleanalytics.resource_prefix', resource_url = config.get('googleanalytics.resource_prefix',
DEFAULT_RESOURCE_URL_TAG) DEFAULT_RESOURCE_URL_TAG)

View File

@ -1,5 +1,5 @@
import httplib import httplib
from unittest import TestCase
from ckan.config.middleware import make_app from ckan.config.middleware import make_app
from paste.deploy import appconfig from paste.deploy import appconfig
@ -9,6 +9,7 @@ from ckan.tests import conf_dir, url_for, CreateTestData
from mockgoogleanalytics import runmockserver from mockgoogleanalytics import runmockserver
from ckanext.googleanalytics.commands import LoadAnalytics from ckanext.googleanalytics.commands import LoadAnalytics
from ckanext.googleanalytics import dbutil from ckanext.googleanalytics import dbutil
from ckanext.googleanalytics.gasnippet import gacode
class MockClient(httplib.HTTPConnection): class MockClient(httplib.HTTPConnection):
@ -27,20 +28,17 @@ class MockClient(httplib.HTTPConnection):
return resp return resp
class TestConfig: class TestConfig(TestCase):
def test_config(self): def test_config(self):
config = appconfig('config:test.ini', relative_to=conf_dir) config = appconfig('config:test.ini', relative_to=conf_dir)
config.local_conf['ckan.plugins'] = 'googleanalytics' config.local_conf['ckan.plugins'] = 'googleanalytics'
config.local_conf['googleanalytics.id'] = ''
command = LoadAnalytics("loadanalytics") command = LoadAnalytics("loadanalytics")
command.CONFIG = config.local_conf command.CONFIG = config.local_conf
command.run([]) self.assertRaises(Exception, command.run, [])
@classmethod
def teardown_class(cls):
CreateTestData.delete()
class TestLoadCommand: class xTestLoadCommand(TestCase):
@classmethod @classmethod
def setup_class(cls): def setup_class(cls):
config = appconfig('config:test.ini', relative_to=conf_dir) config = appconfig('config:test.ini', relative_to=conf_dir)
@ -68,6 +66,12 @@ class TestLoadCommand:
conn.request("QUIT", "/") conn.request("QUIT", "/")
conn.getresponse() conn.getresponse()
def test_analytics_snippet(self):
response = self.app.get(url_for(controller='tag'))
code = gacode % (self.config['googleanalytics.id'],
'auto')
assert code in response.body
def test_top_packages(self): def test_top_packages(self):
command = LoadAnalytics("loadanalytics") command = LoadAnalytics("loadanalytics")
command.TEST_HOST = MockClient('localhost', 6969) command.TEST_HOST = MockClient('localhost', 6969)
@ -75,8 +79,8 @@ class TestLoadCommand:
command.run([]) command.run([])
packages = dbutil.get_top_packages() packages = dbutil.get_top_packages()
resources = dbutil.get_top_resources() resources = dbutil.get_top_resources()
assert packages[0][1] == 2 self.assertEquals(packages[0][1], 2)
assert resources[0][1] == 4 self.assertEquals(resources[0][1], 4)
def test_download_count_inserted(self): def test_download_count_inserted(self):
command = LoadAnalytics("loadanalytics") command = LoadAnalytics("loadanalytics")