Today we’re talking about a CKAN extension that both sends tracking data to Google Analytics and retrieves statistics from Google Analytics and inserts them into CKAN pages.

For real-time portal and dataset access monitoring and reporting, the Google Analytics web app can be utilized since each Data Portal uses the Google Analytics extension specifically for this. Google Analytics is used to track website activity such as session duration, pages per session, bounce rate, etc. of individuals using the site, along with the information on the source of the traffic. The ckanext-googleanalytics extension can be easily enabled on CKAN portals. The first step is creating a Google Analytics domain name.

What can you track with Google Analytics on CKAN?

Some of the Google Analytics reports include:

  1. Resource Download
  1. DataStore search
  1. Web-page visit by the user
  1. Data Active users
  1. User activities dashboard

Additionally, these reports can be customized with different fields so they can vary depending on the reporting requirement. For example, the Google Analytics Event Tracking function that can be used in any extension to create your custom events tracking and also track the API calls, so the usage of the API can be also reported via Google Analytics.

How does the Google Analytics extension work?

First, a Google Analytics account should be created. Once you’ve created your account, the Google Analytics profile should be configured to present statistics and reports for a particular CKAN portal. The Google Analytics credentials are added to the CKAN portal configuration so that the data portal can be associated with those analytics. 

How to install ckanext-googleanalytics 

To use ckanext-googleanalytics with CKAN 1.x, make sure you have ckan.legacy_templates = true in your CKAN ini file.

Installation

  1. Install the extension as usual, e.g. (from an activated virtualenv):
$ pip install -e  git+https://github.com/ckan/ckanext-googleanalytics.git#egg=ckanext-googleanalytics
$ pip install -r ckanext-googleanalytics/requirements.txt
  1. Edit your development.ini (or similar) to provide these necessary parameters:
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 = [email protected]
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.

  1. Edit again your configuration ini file to activate the plugin with:
ckan.plugins = googleanalytics

(If there are other plugins activated, add this to the list. Each plugin should be separated with a space). 4. If you are using this plugin with a version of CKAN < 2.0 then you should also put the following in your ini file:

ckan.legacy_templates = true
  1. Finally, here are some optional configuration settings shown with their default settings:
googleanalytics_resource_prefix = /downloads/
googleanalytics.domain = auto
googleanalytics.track_events = false
googleanalytics.fields = {}

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.

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 for more info.

If track_events is set, Google Analytics event tracking will be enabled. CKAN 1.x only. Note that event tracking for resource downloads is always enabled, track_events enables event tracking for other pages as well.

fields allows you to specify various options when creating the tracker. See Google’s documentation.

Authorization

Before ckanext-googleanalytics can retrieve statistics from Google Analytics, you need to set up the OAUTH details which you can do by following the instructions the outcome of which will be a file with authentication key. These steps are below for convenience:

  1. Visit the Google APIs Console
  2. Sign-in and create a project or use an existing project.
  3. In the Service accounts pane choose your project and create new account. During creation check "Furnish a new private key" -> JSON type. Write down "Service account ID"(looks like email) – it will be used later.
  4. Save downloaded file – it will be used by loadanalytics command(referenced as <credentials.json>)
  5. Go to GoogleAnalytics console and chose ADMIN tab.
  6. Find "User management" button in corresponding column. Add service account using Service account ID(email) generated in 3rd step and grant "Read" role to it.

Testing

There are some very high-level functional tests that you can run using:

(pyenv)~/pyenv/src/ckan$ nosetests –ckan ../ckanext-googleanalytics/tests/

(note — that’s run from the CKAN software root, not the extension root)

Leave a Reply