mozilla/affiliates — pull request #176 by Osmose (2014-03-31 16:50:50+00:00)
Title: Bug 975258: Click tracking

Adds click tracking via Google Analytics. In the old site, when users clicked on a link in a banner, they would make a request that incremented a counter in the Affiliates database, and be sent a 302 redirect to their final destination.

In this new system, we instead have users hit an intermediate page that has Google Analytics on it, and we synchronously log their pageview with Google Analytics before redirecting them on their way (a future PR will include a redirect for users without JavaScript enabled). Then, the site uses the [Google Analytics Core Reporting API](https://developers.google.com/analytics/devguides/reporting/core/v3/coreDevguide) to retrieve the click data from Google Analytics for the previous day.

The upside is that Google Analytics provides many more options for viewing and filtering click data. Future PRs will have us either embedding or generating links to Google Analytics views for click data, instead of having to come up with graphing and stats views ourselves.

If you want to test this on a local instance, you'll have to create a Google Analytics profile for testing. The `GA_ACCOUNT_CODE` is the tracking ID Google Analytics gives you ([docs](https://support.google.com/analytics/answer/1032385?hl=en)).

The `GA_PROFILE_ID` setting value is the profile ID, [which is a little annoying to retrieve](https://productforums.google.com/forum/#!topic/analytics/dRuAr1K4waI). You'll also need to create a service account. Service accounts are tied to a [Developer's Console](https://console.developers.google.com/project) app. You create an app with the dev console, then create a service account and download the p12 key for the account. `GA_API_ACCOUNT_EMAIL` is the email address for the service account, while `GA_API_KEYFILE` is the path to the keyfile you download. [There's some slightly helpful docs for creating a service account and getting the credentials you need for them.](https://developers.google.com/drive/web/service-accounts#use_service_accounts_as_application-owned_accounts).

You will also need to grant read access to the service account for your test GA profile ([docs](https://support.google.com/analytics/answer/2884495?hl=en)). Use the email from the `GA_API_ACCOUNT_EMAIL` setting for specifying the service account as a user. Then, once the service account is created, has read permissions, and you've set the app up to authenticate as the service account, you should be good to go! You'll then be able to create a banner, and if you click on it, see the data show up in your test GA profile (the real-time dashboard is super-useful for testing), and then be able to pull the data from the API using the management command (which, by default, pulls the data from the previous day UTC; I should probably add the date as an argument to the command).