从Firebase分析日志中排除测试设备

时间:2022-07-05 15:24:10

I am sure that Firebase is counting all my development work too in its analytics. I open my app like hundred times a day to debug and test on a few devices, it's really skewing up my readings.

我确信Firebase也在计算我所有的开发工作。我每天都要打开我的应用程序上百次,在一些设备上调试和测试,这真的会影响我的阅读量。

I have used a function to get me a somewhat unique ID to represent my devices and ignored all its analytics through code.

我使用了一个函数来获得一个有点独特的ID来表示我的设备,并忽略了它通过代码进行的所有分析。

public static String getPsuedoID() {
    String m_szDevIDShort = "35" + (Build.BOARD.length() % 10)
    + (Build.BRAND.length() % 10) + (Build.VERSION.SDK_INT % 10)
    + (Build.DEVICE.length() % 10) + (Build.DISPLAY.length() % 10)
    + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10);

    String serial;
    try {
        serial = android.os.Build.class.getField("SERIAL").get(null).toString();
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    } catch (Exception exception) {
        serial = "getUniquePsuedoIDfailed";
    }
    return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
}

But I just figured out it is not as unique as I assumed. Apparently, my ID was same as around a few (very few) users.

但我发现它并不像我想象的那样独特。显然,我的ID与少数用户(非常少)相同。

Is there a foolproof method to do just this?

有什么简单的方法可以做到这一点吗?

3 个解决方案

#1


13  

You can control analytics collection using manifest metadata with the setting defined by a manifestPlaceholder:

您可以使用显式占位符定义的设置来控制分析收集,使用manifest元数据:

<application
    android:name="MyApplication"
    //... >
    <meta-data
        android:name="firebase_analytics_collection_deactivated"
        android:value="${analytics_deactivated}" />
    //...
 </application>

Then define the placeholder value in the build variant blocks of your build.gradle file:

然后在构建的变体块中定义占位符值。gradle文件:

buildTypes {
    debug {
        manifestPlaceholders = [analytics_deactivated: "true"]
        //...
    }

    release {
        manifestPlaceholders = [analytics_deactivated: "false"]
        //...
    }

#2


1  

Should be able to do something like following:

应该能够做如下事情:

    if (BuildConfig.DEBUG) {
        FirebaseAnalytics.getInstance(getApplicationContext()).setAnalyticsCollectionEnabled(false);
     }

#3


0  

This is what I could figure out that can be relied upon. Android has a provision of a unique device ID called the ANDROID_ID that is a 64-bit number and is randomly generated when the user first sets up the device and should remain constant for the lifetime of the user's device.

这就是我能找到的可以信赖的东西。Android提供了一个名为ANDROID_ID的唯一设备ID,它是一个64位的数字,在用户首次设置设备时随机生成,并且在用户设备的整个生命周期中应该保持不变。

Below are helper functions leveraging the ANDROID_ID to uniquely identify a testing device.

下面是利用ANDROID_ID惟一标识测试设备的助手函数。

private static String testingDeviceIDs[] = {"8ab5946d3d65e893", "ada1247bfb6cfa5d"};


private static String getDeviceID(Context c) {
    return Settings.Secure.getString(c.getContentResolver(), Settings.Secure.ANDROID_ID);
}

private static boolean isDeviceForTesting(Context c) {
    for (String testingID : testingDeviceIDs)
        if (getDeviceID(c).equals(testingID))
            return true;
    return false;
}

Lastly, here's the main function that would check for testing devices before firing events to Firebase.

最后,这里是在向Firebase触发事件之前检查测试设备的主要功能。

static void logFirebaseEvent(Context c, String name) {
    if (!isDeviceForTesting(c))
        FirebaseAnalytics.getInstance(c).logEvent(name, null);
}

#1


13  

You can control analytics collection using manifest metadata with the setting defined by a manifestPlaceholder:

您可以使用显式占位符定义的设置来控制分析收集,使用manifest元数据:

<application
    android:name="MyApplication"
    //... >
    <meta-data
        android:name="firebase_analytics_collection_deactivated"
        android:value="${analytics_deactivated}" />
    //...
 </application>

Then define the placeholder value in the build variant blocks of your build.gradle file:

然后在构建的变体块中定义占位符值。gradle文件:

buildTypes {
    debug {
        manifestPlaceholders = [analytics_deactivated: "true"]
        //...
    }

    release {
        manifestPlaceholders = [analytics_deactivated: "false"]
        //...
    }

#2


1  

Should be able to do something like following:

应该能够做如下事情:

    if (BuildConfig.DEBUG) {
        FirebaseAnalytics.getInstance(getApplicationContext()).setAnalyticsCollectionEnabled(false);
     }

#3


0  

This is what I could figure out that can be relied upon. Android has a provision of a unique device ID called the ANDROID_ID that is a 64-bit number and is randomly generated when the user first sets up the device and should remain constant for the lifetime of the user's device.

这就是我能找到的可以信赖的东西。Android提供了一个名为ANDROID_ID的唯一设备ID,它是一个64位的数字,在用户首次设置设备时随机生成,并且在用户设备的整个生命周期中应该保持不变。

Below are helper functions leveraging the ANDROID_ID to uniquely identify a testing device.

下面是利用ANDROID_ID惟一标识测试设备的助手函数。

private static String testingDeviceIDs[] = {"8ab5946d3d65e893", "ada1247bfb6cfa5d"};


private static String getDeviceID(Context c) {
    return Settings.Secure.getString(c.getContentResolver(), Settings.Secure.ANDROID_ID);
}

private static boolean isDeviceForTesting(Context c) {
    for (String testingID : testingDeviceIDs)
        if (getDeviceID(c).equals(testingID))
            return true;
    return false;
}

Lastly, here's the main function that would check for testing devices before firing events to Firebase.

最后,这里是在向Firebase触发事件之前检查测试设备的主要功能。

static void logFirebaseEvent(Context c, String name) {
    if (!isDeviceForTesting(c))
        FirebaseAnalytics.getInstance(c).logEvent(name, null);
}