Skip to content

Instantly share code, notes, and snippets.

@geftimov
Last active August 29, 2015 14:05
Show Gist options
  • Save geftimov/07a73930ce450eabca89 to your computer and use it in GitHub Desktop.
Save geftimov/07a73930ce450eabca89 to your computer and use it in GitHub Desktop.
Configuration for google analytics
***************************************************
* Application class *
***************************************************
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
/**
* Enum used to identify the tracker that needs to be used for tracking.
* <p/>
* A single tracker is usually enough for most purposes. In case you do need multiple trackers,
* storing them all in Application object helps ensure that they are created only once per
* application instance.
*/
public enum TrackerName {
APP_TRACKER, // Tracker used only in this app.
GLOBAL_TRACKER, // Tracker used by all the apps from a company. eg: roll-up tracking.
ECOMMERCE_TRACKER, // Tracker used by all ecommerce transactions from a company.
}
public synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(PROPERTY_ID)
: (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(R.xml.global_tracker)
: analytics.newTracker(R.xml.ecommerce_tracker);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
***************************************************
* res/xml/global_tracker.xml *
***************************************************
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="ga_sessionTimeout">300</integer>
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- The screen names that will appear in reports -->
<screenName name="com.eftimoff.selfie.fragments">
Selfie MainFragment
</screenName>
<!-- The following value should be replaced with correct property id. -->
<string name="ga_trackingId">UA-50964631-4</string>
<bool name="ga_reportUncaughtExceptions">true</bool>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment