sunlightlabs/congress-android — pull request #628 by konklone (2014-04-24 21:52:17+00:00)
Title: Upgrade to Google Analytics SDK for Android v4

Resolves #623.

[Google Analytics Android SDK v4](https://developers.google.com/analytics/devguides/collection/android/v4/) was released earlier this year, and has changed its architecture to become part of the Google Play Services SDK. However, according to the [changelog](https://developers.google.com/analytics/devguides/collection/android/changelog), the SDK "does not require devices to have Google Play Services installed. When Google Play Services is not available the SDK will automatically fall back to local dispatching." So, this will not disturb Kindle compatibility.

Some effects:

* We're now using an `Application` subclass, called `CongressApp`, that houses the singleton `Tracker` for the application. It has one method, which idempotently creates a tracker if it does not exist, and returns it if it has already been created.
* Network requests have now doubled, as we're sending a `ping` event with every activity start. This is to associate custom dimensions with normal hits, and the v4 SDK now only allows custom dimensions to be set on hits/events, not at the tracker level. Since we use the activity start/stop convenience methods for the tracker, we don't have an opportunity to set custom dimensions on normal hits, only events. Thus, this hack.
* The new v4 SDK supports multiple trackers, especially to allow for "roll-up" and e-commerce trackers separate from one's main app tracker. This was evaluated, and implemented and removed along the way. It was removed primarily because of the additional network requests (2 extra, because of the `ping` issue described above) it would incur. It's also just not worth the complexity in the code.
* All activities using the tracker are still making method calls in `onStart` and `onStop`. However, now they are making an additional call in `onCreate`, which is where trackers are initialized if need be. Calls in `onStart` and `onStop` are calls to the `GoogleAnalytics` singleton, which manages any number of initialized trackers.
* The app brings in the Google Analytics SDK over the network via gradle. The old `.jar` in `libs/` has been removed.
* When the user checks/unchecks the analytics setting, it also sends a signal to the tracker to respect the optout. In theory, this should be superfluous, but this should add some small layer of protection against bugs in my own analytics-disabling code.

Hopefully this is the last time the Analytics SDK goes through a major revision by Google for a while! The v3 tracker didn't have a very long shelf-life before being replaced by v4.