1.Add google-play-services_lib as a library
2.Add code in AndroidManifest.xml
<meta-data android:name="com.google.android.gms.analytics.globalConfigResource" android:resource="@xml/global_tracker" />
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
3.MyApplication.java
public class MyApplication extends Application {
// The following line should be changed to include the correct property id.
private static final String PROPERTY_ID = "UA-XXXXXXXX-XX";
//Logging TAG
private static final String TAG = "MyApp";
public static int GENERAL_TRACKER = 0;
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.
}
HashMap<TrackerName, Tracker> mTrackers = new HashMap<TrackerName, Tracker>();
public MyApplication() {
super();
}
synchronized Tracker getTracker(TrackerName trackerId) {
if (!mTrackers.containsKey(trackerId)) {
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
Tracker t = (trackerId == TrackerName.APP_TRACKER) ? analytics.newTracker(R.xml.app_tracker)
: (trackerId == TrackerName.GLOBAL_TRACKER) ? analytics.newTracker(PROPERTY_ID)
: analytics.newTracker(R.xml.app_tracker);
mTrackers.put(trackerId, t);
}
return mTrackers.get(trackerId);
}
}
3.1 app_tracker.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes">
<!-- The apps Analytics Tracking Id -->
<string name="ga_trackingId">UA-XXXXXX-XX</string>
<!-- Percentage of events to include in reports -->
<string name="ga_sampleFrequency">100.0</string>
<!-- Enable automatic Activity measurement -->
<bool name="ga_autoActivityTracking">true</bool>
<!-- catch and report uncaught exceptions from the app -->
<bool name="ga_reportUncaughtExceptions">true</bool>
<!-- How long a session exists before giving up -->
<integer name="ga_sessionTimeout">-1</integer>
<!-- If ga_autoActivityTracking is enabled, an alternate screen name can be specified to
substitute for the full length canonical Activity name in screen view hit. In order to
specify an alternate screen name use an <screenName> element, with the name attribute
specifying the canonical name, and the value the alias to use instead. -->
<screenName name="com.dailyapps.ttxgame.wirejump.MainActivity">wirejump</screenName>
</resources>
3.2 global_tracker.xml
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes">
<!-- the Local LogLevel for Analytics -->
<string name="ga_logLevel">verbose</string>
<!-- how often the dispatcher should fire -->
<integer name="ga_dispatchPeriod">30</integer>
<!-- Treat events as test events and don't send to google -->
<bool name="ga_dryRun">false</bool>
<!-- The screen names that will appear in reports -->
<string name="LyricBook 3.0">LyricBook 3.0</string>
</resources>
4.Send Screen tracker
// Get tracker.
<pre name="code" class="java"> Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Send a screen view. t.setScreenName(this.getString(R.string.app_name) + "_" + title); t.send(new HitBuilders.AppViewBuilder().build());
5. Send Event tracker
// Get tracker.
Tracker t = ((AnalyticsSampleApp) getActivity().getApplication()).getTracker(
TrackerName.APP_TRACKER);
// Build and send an Event.
t.send(new HitBuilders.EventBuilder()
.setCategory(getString(categoryId))
.setAction(getString(actionId))
.setLabel(getString(labelId))
.build());
Remember to add android:name=".MyApplication" in <application> in AndroidManifest.xml.
Reference:
https://developers.google.com/analytics/devguides/collection/android/v4/