在开发时禁用谷歌分析

时间:2022-01-16 15:15:51

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.

我的问题非常简单:有什么方法可以在应用程序与调试证书签署时自动禁用谷歌分析?意味着它应该只在发布版本中激活。提前谢谢你。

3 个解决方案

#1


12  

If you're using ADT 17 and above, you can utilize the BuildConfig class:

如果您使用的是ADT 17或以上,您可以使用BuildConfig类:

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.

BuildConfig类像R一样自动生成。java。它只包含调试布尔值,默认设置为true,在导出apk时设置为false。

#2


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:

在getHardwareSerial():

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


1  

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

使用最新版本的谷歌分析,您应该使用以下代码:

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

#1


12  

If you're using ADT 17 and above, you can utilize the BuildConfig class:

如果您使用的是ADT 17或以上,您可以使用BuildConfig类:

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.

BuildConfig类像R一样自动生成。java。它只包含调试布尔值,默认设置为true,在导出apk时设置为false。

#2


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:

在getHardwareSerial():

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


1  

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

使用最新版本的谷歌分析,您应该使用以下代码:

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