diff --git a/README.rst b/README.rst
index 2fe7aaf..b339c7e 100644
--- a/README.rst
+++ b/README.rst
@@ -23,25 +23,25 @@ Installation
googleanalytics.id = UA-1010101-1
googleanalytics.username = googleaccount@gmail.com
googleanalytics.password = googlepassword
+
+ Note that your password will probably be readable by other people;
+ so you may want to set up a new gmail account specifically for
+ accessing your gmail profile.
+
+ There are two optional configuration settings (shown with their
+ default settings)::
+
googleanalytics.show_downloads = true
- # the following *must* match profile name in GA dashboard
- googleanalytics.profile_name = mydomain.com
-
- That last comment is worth emphasising. Due to the strange
- relationship between tracking IDs and profiles, you need to get
- that right. It's the relevant value in the "Name" column for the
- list of "Website Profiles" that you see when you click on an
- Analytics Account link in the Google Analytics homepage.
- E.g. you'll need two clicks from the analytics home page to see the
- profile name. Sometimes your profile name might have a trailing
- slash; you need to include that, too, if so.
-
- Note also that your password will probably be readable by other
- people; so you may want to set up a new gmail account specifically
- for accessing your gmail profile.
+ googleanalytics.resource_prefix = /downloads/
If ``show_downloads`` is set, a download count for resources will
be displayed on individual package pages.
+
+ ``resource_prefix`` is an arbitrary identifier so that we can query
+ for downloads in Google Analytics. It can theoretically be any
+ string, but should ideally resemble a URL path segment, to make
+ filtering for all resources easier in the Google Analytics web
+ interface.
3. Wait a day or so for some stats to be recorded in Google
@@ -66,7 +66,7 @@ Installation
remember to run it by hand!
Testing
-=======
+=======
There are some very high-level functional tests that you can run using::
@@ -85,3 +85,7 @@ into any of its features. For example, as a measure of popularity, we
could record bounce rate, or new visits only; we could also display
which datasets are popular where, or highlight packages that have been
linked to from other locations.
+
+We could also embed extra metadata information in tracking links, to
+enable reports on particular types of data (e.g. most popular data
+format by country of origin, or most downloaded resource by license)
diff --git a/ckanext/googleanalytics/commands.py b/ckanext/googleanalytics/commands.py
index f43985a..0954f60 100644
--- a/ckanext/googleanalytics/commands.py
+++ b/ckanext/googleanalytics/commands.py
@@ -23,7 +23,7 @@ class LoadAnalytics(CkanCommand):
min_args = 0
TEST_HOST = None
CONFIG = pylonsconfig
-
+
def command(self):
self._load_config()
self.resource_url_tag = self.CONFIG.get(
@@ -37,11 +37,16 @@ class LoadAnalytics(CkanCommand):
self.parse_and_save()
def parse_and_save(self):
+ """Grab raw data from Google Analytics and save to the
+ database
+ """
packages_data = self.get_ga_data()
self.save_ga_data(packages_data)
log.info("Saved %s records from google" % len(packages_data))
def save_ga_data(self, packages_data):
+ """Save tuples of packages_data to the database
+ """
dbutil.init_tables()
for identifier, visits in packages_data.items():
recently = visits.get('recent', 0)
@@ -69,11 +74,14 @@ class LoadAnalytics(CkanCommand):
model.Session.commit()
def setup_ga_connection(self):
+ """Log into the Google Data API, and find out the ``table_id``
+ that is associated with the profile, for later querying
+ """
SOURCE_APP_NAME = "CKAN Google Analytics Plugin"
username = self.CONFIG.get('googleanalytics.username')
password = self.CONFIG.get('googleanalytics.password')
- profile_name = self.CONFIG.get('googleanalytics.profile_name')
- if not username or not password or not profile_name:
+ ga_id = self.CONFIG.get('googleanalytics.id')
+ if not username or not password or not ga_id:
raise Exception("No googleanalytics profile info in config")
if self.TEST_HOST:
my_client = client.AnalyticsClient(source=SOURCE_APP_NAME,
@@ -87,16 +95,18 @@ class LoadAnalytics(CkanCommand):
feed = my_client.GetAccountFeed(account_query)
table_id = None
for entry in feed.entry:
- if entry.title.text == profile_name:
+ if entry.get_property("ga:webPropertyId").value == ga_id:
table_id = entry.table_id.text
break
if not table_id:
- msg = "Couldn't find a profile called '%s'" % profile_name
+ msg = "Couldn't find a profile with id '%s'" % ga_id
raise Exception(msg)
self.table_id = table_id
self.client = my_client
def ga_query(self, query_filter=None, from_date=None):
+ """Executie a query against Google Analytics
+ """
now = datetime.datetime.now()
to_date = now.strftime("%Y-%m-%d")
metrics = 'ga:visits,ga:visitors,ga:newVisits,ga:uniquePageviews'
@@ -113,7 +123,11 @@ class LoadAnalytics(CkanCommand):
return feed
def get_ga_data(self, query_filter=None):
- """Return a dictionary like
+ """Get raw data from Google Analtyics for packages and
+ resources, and for both the last two weeks and ever.
+
+ Returns a dictionary like::
+
{'identifier': {'recent':3, 'ever':6}}
"""
now = datetime.datetime.now()
diff --git a/tests/accountsfixture.xml b/tests/accountsfixture.xml
index a6631c3..565ad08 100644
--- a/tests/accountsfixture.xml
+++ b/tests/accountsfixture.xml
@@ -1 +1 @@
-2011-03-13T01:59:55.435-08:00http://www.google.com/analytics/feeds/accounts/seb.bacon@okfn.orgGoogle AnalyticsGoogle Analytics ga:visitorType==New Visitorga:visitorType==Returning Visitorga:medium==cpa,ga:medium==cpc,ga:medium==cpm,ga:medium==cpp,ga:medium==cpv,ga:medium==ppcga:medium==organicga:medium==cpa,ga:medium==cpc,ga:medium==cpm,ga:medium==cpp,ga:medium==cpv,ga:medium==organic,ga:medium==ppcga:medium==(none)ga:medium==referralga:goalCompletionsAll>0ga:transactions>0ga:isMobile==Yesga:bounces==011300http://www.google.com/analytics/feeds/accounts/ga:42156377ga:421563772011-03-13T01:59:55.435-08:00borfProfile list for seb.bacon@okfn.org
+2011-03-13T01:59:55.435-08:00http://www.google.com/analytics/feeds/accounts/seb.bacon@okfn.orgGoogle AnalyticsGoogle Analytics ga:visitorType==New Visitorga:visitorType==Returning Visitorga:medium==cpa,ga:medium==cpc,ga:medium==cpm,ga:medium==cpp,ga:medium==cpv,ga:medium==ppcga:medium==organicga:medium==cpa,ga:medium==cpc,ga:medium==cpm,ga:medium==cpp,ga:medium==cpv,ga:medium==organic,ga:medium==ppcga:medium==(none)ga:medium==referralga:goalCompletionsAll>0ga:transactions>0ga:isMobile==Yesga:bounces==011300http://www.google.com/analytics/feeds/accounts/ga:42156377ga:421563772011-03-13T01:59:55.435-08:00borfProfile list for seb.bacon@okfn.org
diff --git a/tests/test_general.py b/tests/test_general.py
index 55011f4..fdfa258 100644
--- a/tests/test_general.py
+++ b/tests/test_general.py
@@ -49,8 +49,8 @@ class TestLoadCommand:
= 'borf'
config.local_conf['googleanalytics.password'] \
= 'borf'
- config.local_conf['googleanalytics.profile_name'] \
- = 'borf'
+ config.local_conf['googleanalytics.id'] \
+ = 'UA-borf-1'
config.local_conf['googleanalytics.show_downloads'] \
= 'true'
cls.config = config.local_conf