My question is pretty simple: is there any way for Google Analytics to be disabled automatically when the application is signed with the debug certificate? Means it should be active only in release version. Thank you in advance.
Asked
Active
Viewed 3,426 times
9
-
possible duplicate of [Disable GoogleAnalytics from Android App when testing or developing](http://stackoverflow.com/questions/12314357/disable-googleanalytics-from-android-app-when-testing-or-developing) – kiranpradeep May 02 '15 at 14:13
3 Answers
12
If you're using ADT 17 and above, you can utilize the BuildConfig class:
if(BuildConfig.DEBUG) {
GoogleAnalytics googleAnalytics = GoogleAnalytics.getInstance(getApplicationContext());
googleAnalytics.setAppOptOut(true);
}
The BuildConfig
class is automatically generated like R.java
is. It only contains the DEBUG
boolean, which is set to true
by default, and to false
when you export an apk.

Raghav Sood
- 81,899
- 22
- 187
- 195
-
Thank you, I'll accept your answer. But I thought Google Analytics could have some kind of configuration APIs that allow you to set this kind of rules. – Egor Dec 21 '12 at 12:57
-
I don't think it does, but I may be wrong. I'm not very familiar with GA itself. – Raghav Sood Dec 21 '12 at 12:58
-
2Beware, BuildConfig.DEBUG seems to be buggy : https://code.google.com/p/android/issues/detail?id=27940 – Cheok Yan Cheng Jul 22 '13 at 03:02
-
1@RaghavSood I think the preferred way of doing this has changed since you posted your answer... it looks like it's encouraged to use `GoogleAnalytics.getInstance(context).setDryRun(true))` instead for debugging purposes. – Alex Lockwood Jul 27 '14 at 20:31
2
Well you can set it to not be active easily enough:
if (...) {
GoogleAnalytics ga= GoogleAnalytics.getInstance(getApplicationContext());
ga.setAppOptOut(true);
}
I usually just check the hardware serial number of some known devices used for testing:
if (Arrays.asList("x", "y").contains(getHardwareSerial()))
Where getHardwareSerial()
is:
public static String getHardwareSerial() {
try {
Field serialField = Build.class.getDeclaredField("SERIAL");
return (String) serialField.get(null);
} catch (NoSuchFieldException nsf) {
} catch (IllegalAccessException ia) {
}
return Build.UNKNOWN;
}

Mattias Isegran Bergander
- 11,811
- 2
- 41
- 49
1
With the latest version of Google Analytics, you should be using the following code:
if(BuildConfig.DEBUG){
GoogleAnalytics.getInstance(this).setDryRun(true);
}

Michael
- 32,527
- 49
- 210
- 370

Nimesh Madhavan
- 6,290
- 6
- 44
- 55