Disable GoogleAnalytics from Android App when testing or developing

I believe the correct way to do this with Version 4 of Analytics is with the Opt Out method

GoogleAnalytics.getInstance(this).setAppOptOut(true);

You could set that method to be set if you build in debug mode. ie.

GoogleAnalytics.getInstance(this).setAppOptOut(BuildConfig.DEBUG);

I am using something similiar to allow users to opt-out of analytics.

I found this information at he following link: https://developers.google.com/analytics/devguides/collection/android/v4/advanced

Edit: Just saw the date of the original question, but thought I would add this answer anyway as it was something I was looking for.


You can use a class with a static boolean value let's say DEBUG like this :

public final class BuildMode {
        public final static boolean DEBUG = true;
}

In code, just use :

if (BuildMode.DEBUG) ...

This is a solution working on all android SDK versions!


UPDATE: With release of Google Analytics v3 for Android,

The SDK provides a dryRun flag that when set, prevents any data from being sent to Google Analytics. The dryRun flag should be set whenever you are testing or debugging an implementation and do not want test data to appear in your Google Analytics reports.

To set the dry run flag:

// When dry run is set, hits will not be dispatched, but will still be logged as though they were dispatched.

GoogeAnalytics.getInstance(this).setDryRun(true);

+++ My old answer +++

Just comment the following line in your analytics.xml file while you are in development mode.

<string name="ga_trackingId">UA-****</string>

Google Analytics wouldn't be able to find any tracking id, so EasyTracker won't be able to do its job. When you are building the app for release, uncomment the line and you're good to go.


If you are building a standalone app(not a library), this will be the easiest way to do it, let the build system figure out if it is a debug build or not.

if(BuildConfig.DEBUG){
    GoogleAnalytics.getInstance(this).setDryRun(true);
}

I see on the web that this method does not work well for library projects as there is bug in the build tools which does not set the BuildConfig.DEBUG flag correctly for libraries. Not sure if this issue is fixed now.