Adds new columns to the table to store the relevant organisation, but also
allows for the metric/value to be stored so that it can eventually contain
different metrics than the default
--- a/README.rst
+++ b/README.rst
@@ -32,11 +32,9 @@
googleanalytics.id = UA-1010101-1
googleanalytics.account = Account name (i.e. data.gov.uk, see top level item at https://www.google.com/analytics)
- googleanalytics.username = googleaccount@gmail.com
- googleanalytics.password = googlepassword
ga-report.period = monthly
- Note that your password will be readable by system administrators on your server. Rather than use sensitive account details, it is suggested you give access to the GA account to a new Google account that you create just for this purpose.
+ Note that your credentials will be readable by system administrators on your server. Rather than use sensitive account details, it is suggested you give access to the GA account to a new Google account that you create just for this purpose.
3. Set up this extension's database tables using a paster command. (Ensure your CKAN pyenv is still activated, run the command from ``src/ckanext-ga-report``, alter the ``--config`` option to point to your site config file)::
--- a/ckanext/ga_report/download_analytics.py
+++ b/ckanext/ga_report/download_analytics.py
@@ -94,9 +94,6 @@
'''Get data from GA for a given time period'''
start_date = start_date.strftime('%Y-%m-%d')
end_date = end_date.strftime('%Y-%m-%d')
- # url
- #query = 'ga:pagePath=~^%s,ga:pagePath=~^%s' % \
- # (PACKAGE_URL, self.resource_url_tag)
query = 'ga:pagePath=~/dataset/[a-z0-9-]+$'
metrics = 'ga:uniquePageviews'
sort = '-ga:uniquePageviews'
@@ -110,7 +107,9 @@
metrics=metrics,
sort=sort,
dimensions="ga:pagePath",
+ max_results=10000,
end_date=end_date).execute()
+
import pprint
pprint.pprint(results)
@@ -119,18 +118,8 @@
packages = []
for entry in results.get('rows'):
(loc,size,) = entry
- packages.append( ('http:/' + loc,size, '',) )
+ packages.append( ('http:/' + loc,size, '',) ) # Temporary hack
return dict(url=packages)
-
- def print_results(self, results):
- import pprint
- pprint.pprint(results)
- if results:
- print 'Profile: %s' % results.get('profileInfo').get('profileName')
-
- print 'Total Visits: %s' % results.get('rows', [[-1]])[0][0]
- else:
- print 'No results found'
def store(self, period_name, period_complete_day, data):
if 'url' in data:
--- a/ckanext/ga_report/ga_model.py
+++ b/ckanext/ga_report/ga_model.py
@@ -28,9 +28,10 @@
default=make_uuid),
Column('period_name', types.UnicodeText),
Column('period_complete_day', types.Integer),
- Column('visits', types.Integer),
+ Column('metric', types.UnicodeText),
+ Column('value', types.UnicodeText),
Column('url', types.UnicodeText),
- Column('next_page', types.UnicodeText),
+ Column('department_id', types.UnicodeText),
)
mapper(GA_Url, url_table)
@@ -71,7 +72,7 @@
if dataset:
publisher_groups = dataset.get_groups('publisher')
if publisher_groups:
- return publisher_groups[0].id
+ return publisher_groups[0].name
def update_url_stats(period_name, period_complete_day, url_data):
@@ -79,13 +80,16 @@
for url, views, next_page in url_data:
url = _normalize_url(url)
department_id = _get_department_id_of_url(url)
+
# see if the row for this url & month is in the table already
- item = model.Session.query(GA_Url).filter(GA_Url.period_name==period_name).filter(GA_Url.url==url).first()
+ item = model.Session.query(GA_Url).\
+ filter(GA_Url.period_name==period_name).\
+ filter(GA_Url.url==url).\
+ filter(GA_Url.metric == 'Total views').first()
if item:
- item.period_name = period_complete_day=period_complete_day
- item.views = views
+ item.period_name = period_complete_day = period_complete_day
+ item.value = views
item.department_id = department_id
- item.next_page = next_page
model.Session.add(item)
else:
# create the row
@@ -93,10 +97,10 @@
'period_name': period_name,
'period_complete_day': period_complete_day,
'url': url,
- 'views': views,
- 'department_id': department_id,
- 'next_page': next_page}
- obj = GA_Url(**values)
- model.Session.add(obj)
+ 'value': views,
+ 'metric': 'Total views',
+ 'department_id': department_id
+ }
+ model.Session.add(GA_Url(**values))
model.Session.commit()