GSA/ckanext-ga-report

Name: ckanext-ga-report

Owner: U.S. General Services Administration

Description: For creating detailed reports of CKAN analytics, sliced by group

Forked from: datagovuk/ckanext-ga-report

Created: 2016-09-16 18:23:56.0

Updated: 2016-09-16 18:23:58.0

Pushed: 2017-12-06 18:33:20.0

Homepage: null

Size: 1400

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

ckanext-ga-report

Status: In current use (data.gov.uk)

CKAN Version: 1.7.1+

Overview

For creating detailed reports of CKAN analytics, including totals per group.

Whereas ckanext-googleanalytics focusses on providing page view stats a recent period and for all time (aimed at end users), ckanext-ga-report is more interested in building regular periodic reports (more for site managers to monitor).

Contents of this extension:

Setup Google Analytics

Before you can use this CKAN extension you need to be already collecting Google Analytics data and see it appearing on the Google Analytics website:

  1. If your organization already has a Google Analytics “account” then your Google user account needs to be given the “Edit” permission, or you need to work with someone who does have it. OR if your organization doesn't already have a Google Analytics “account”, create one.

  2. Set up a Google Analytics “property” for your DGU To Go website: https://support.google.com/analytics/answer/1042508?hl=en and note the Tracking ID it gives you. e.g. UA-1010101-1

  3. Add the Google Analytics tracking code snippet to your templates by putting it into the ckan.template_footer_end setting in your ckan.ini:

.template_footer_end =   <script type="text/javascript">
-//--><![CDATA[//><!--
nction(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
window,document,'script','//www.google-analytics.com/analytics.js','ga');
'create', 'UA-1010101-1', 'auto');
'send', 'pageview');
-><!]]>
cript>

Notes:

Installation
  1. Activate you CKAN python environment and install this extension's software:

    env/bin/activate
    p install -e  git+https://github.com/datagovuk/ckanext-ga-report.git#egg=ckanext-ga-report
    
  2. Install dependencies (e.g. Google's python client library):

    p install -r ckanext-ga-report/requirements.txt
    
  3. Ensure you development.ini (or similar) contains the info about your Google Analytics account and configuration:

leanalytics.id = UA-1010101-1
leanalytics.account = Account name (e.g. data.gov.uk, see top level item at https://www.google.com/analytics)
leanalytics.token.filepath = ~/pyenv/token.dat
eport.period = monthly
eport.bounce_url = /

The ga-report.bounce_url specifies a particular path to record the bounce rate for. Typically it is / (the home page).

  1. 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):

    ster initdb --config=../ckan/development.ini
    
  2. Enable the extension in your CKAN config file by adding it to ckan.plugins:

    .plugins = ga-report
    
Problem shooting
Authorization

Before you can access the data, you need to create an OAUTH token of type “Installed application”, which you can do by following the instructions the outcome of which will be a file called credentials.json (also known as client_secrets.json) which should look like credentials.json.template only with the relevant fields completed. The steps are listed below for convenience.

  1. Visit the Google APIs Console using a Google account that has access to the Google Analytics for your project. (That Google user has a quota of API requests and has the power to revoke API access via the token associated.) You may have to accept the Terms of Service at this point.

  2. Create a project or use an existing project.

  3. In the Services pane, activate “Analytics API” for your project.

  4. Go to the API Access pane

  5. Click “Create new Client ID”

  6. Select type “Installed application” (in this context, 'ckanext-ga-report' is the application)

  7. Now shown the “Consent screen”, you only need to provide the Product Name (ckanext-ga-report although it is not important).

  8. In the “Create Client ID” dialog again you need to select “Installed application” and type “Other”

  9. It will now show the information that go into credentials.json: Client ID, Client secret, and Redirect URIs. You can click “Download JSON” to save it. Now rename the file to credentials.json.

  10. This paster command should be run on any machine with ckanext-ga-report installed and the credentials.json file at hand:

    $ paster --plugin=ckanext-ga-report getauthtoken credentials.json --config=../ckan/development.ini
    

    This will try to start a web browser at a URL like this:

    https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.readonly&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&response_type=code&client_id=769797987376-tj82tp3ttp5guhiioyi6jneecd7mj5g.apps.googleusercontent.com&access_type=offline
    

    If you are running it on a server or VM without a browser then you can simply copy & paste that URL into a local web browser.

  11. The web page asks you to log-in to Google - use the same account you created the credentials with earlier.

  12. In the browser it now asks “ckanext-ga-report would like to view your Google Analytics data”. Click “Accept”.

  13. Your browser will now be redirected to something like:

    http://localhost:8080/?code=4/Com0kAHaio858diw2dv70u3rhjwkjsduofyzj0.g8f93jd889sjsj29d-If93
    

    If you are in a browser that is not on the same machine as the paster command than you will see a browser error. In this case you need to resolve that URL on the machine where the paster command is running (but in a new terminal):

    curl http://localhost:8080/?code=4/Com0kAHaio858diw2dv70u3rhjwkjsduofyzj0.g8f93jd889sjsj29d-If93
    

    The paster window should now say “Authentication successful” and the token.dat file will be created.

  14. Now ensure you reference the correct path to your token.dat in your CKAN config file (e.g. development.ini)::

    googleanalytics.token.filepath = ~/pyenv/token.dat

Tutorial

Download some GA data and store it in CKAN's database. (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) and specifying the name of your auth file (token.dat by default) from the previous step:

$ paster loadanalytics latest --config=../ckan/development.ini

The value after the token file is how much data you want to retrieve, this can be

Software Licence

This software is developed by Cabinet Office. It is Crown Copyright and opened up under dual licences:

  1. Open Government Licence (OGL) (which is compatible with Creative Commons Attibution License). OGL terms: http://www.nationalarchives.gov.uk/doc/open-government-licence/

  2. GNU Affero General Public License (AGPL-3.0). Terms: http://opensource.org/licenses/AGPL-3.0


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.