Disable Google Analytics when in development

不问归期 提交于 2019-12-05 18:35:48

问题


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.


回答1:


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.




回答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;
    }



回答3:


With the latest version of Google Analytics, you should be using the following code:

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


来源:https://stackoverflow.com/questions/13990391/disable-google-analytics-when-in-development

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!