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:
@ -33,6 +33,7 @@ Installation
googleanalytics.show_downloads = true
googleanalytics.resource_prefix = /downloads/
googleanalytics.domain = auto
If ``show_downloads`` is set, a download count for resources will
be displayed on individual package pages.
@ -42,7 +43,13 @@ Installation
string, but should ideally resemble a URL path segment, to make
filtering for all resources easier in the Google Analytics web
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
4. Import Google stats by running the following command from

View File

@ -1,15 +1,13 @@
gacode = u"""
gacode = """
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '%s']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setDomainName', '%s']);
(function() {
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';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
"""

View File

@ -37,7 +37,8 @@ class GoogleAnalyticsPlugin(SingletonPlugin):
def filter(self, stream):
log.info("Inserting GA code into template")
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)
resource_url = config.get('googleanalytics.resource_prefix',
DEFAULT_RESOURCE_URL_TAG)

View File

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