initial commit

This commit is contained in:
Seb Bacon 2011-02-10 13:32:19 +00:00
commit 3f060228d3
5 changed files with 106 additions and 0 deletions

8
.hgignore Normal file
View File

@ -0,0 +1,8 @@
syntax: glob
*.egg-info
*.pyc
*.swp
*.swo
*~
build/
dist/

15
ckanext/__init__.py Normal file
View File

@ -0,0 +1,15 @@
# this is a namespace package
try:
import pkg_resources
pkg_resources.declare_namespace(__name__)
except ImportError:
import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)
try:
import modulefinder
except ImportError:
pass
else:
for p in __path__:
modulefinder.AddPackagePath(__name__, p)

View File

@ -0,0 +1,37 @@
import logging
log = logging.getLogger(__name__)
from genshi.filters import Transformer
from genshi import HTML
from ckan.plugins import implements, SingletonPlugin
from ckan.plugins import IGenshiStreamFilter, IConfigurable
from gasnippet import gacode
class GoogleAnalyticsException(Exception):
pass
class GoogleAnalyticsPlugin(SingletonPlugin):
implements(IConfigurable, inherit=True)
implements(IGenshiStreamFilter, inherit=True)
def configure(self, config):
self.config = config
log.info("Loading Google Analytics plugin...")
if (not 'googleanalytics.id' in config):
msg = "Missing googleanalytics.id in config"
raise GoogleAnalyticsException(msg)
def filter(self, stream):
"""
Required to implement IGenshiStreamFilter; will apply some HTML
transformations to the page currently rendered. Depends on Pylons
global objects, how can this be fixed without obscuring the
inteface?
"""
log.info("Inserting GA code into template")
ga_id = self.config['googleanalytics.id']
code = HTML(gacode % ga_id)
stream = stream | Transformer('head').append(code)
return stream

View File

@ -0,0 +1,15 @@
gacode = """
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '%s']);
_gaq.push(['_trackPageview']);
(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>
"""

31
setup.py Normal file
View File

@ -0,0 +1,31 @@
from setuptools import setup, find_packages
import sys, os
version = '0.1'
setup(
name='ckanext-googleanalytics',
version=version,
description="Add GA tracking and reporting to CKAN instance",
long_description="""\
""",
classifiers=[], # Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
keywords='',
author='Seb Bacon',
author_email='seb.bacon@gmail.com',
url='',
license='',
packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
namespace_packages=['ckanext', 'ckanext.googleanalytics'],
include_package_data=True,
zip_safe=False,
install_requires=[
'gdata'
],
entry_points=\
"""
[ckan.plugins]
# Add plugins here, eg
googleanalytics=ckanext.googleanalytics:GoogleAnalyticsPlugin
""",
)