Exclude testing device from Firebase Analytics logging
Asked Answered
B

6

18

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.

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

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.

Is there a foolproof method to do just this?

Balaam answered 4/12, 2016 at 14:57 Comment(0)
B
8

A solution using the ANDROID_ID

1) Get ANDROID_ID of all your testing devices (This is a 64-bit unique ID generated when the user first sets up the device. It remains constant thereafter.)

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

2) Add these IDs to an array:

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

3) Check if the current device is one of the testing devices.

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

4) Finally, log Firebase events only if the device is not a testing device.

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

UPSIDE: Unlike Firebase's provision of controlling analytics collection, this method will also work on Release builds/APKs.

Balaam answered 20/10, 2017 at 20:13 Comment(0)
S
32

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

<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:

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

    release {
        manifestPlaceholders = [analytics_deactivated: "false"]
        //...
    }
Squireen answered 4/12, 2016 at 17:23 Comment(3)
That's it, Thanks.Pyelonephritis
but what if i don't want to log events from specific devices even when it is signed APK downloaded from Play Store? Our team often performs tests on live application.Tinhorn
@Tinhorn you can look to my solution a little below. I believe it will work out for your use case.Clyve
B
8

A solution using the ANDROID_ID

1) Get ANDROID_ID of all your testing devices (This is a 64-bit unique ID generated when the user first sets up the device. It remains constant thereafter.)

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

2) Add these IDs to an array:

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

3) Check if the current device is one of the testing devices.

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

4) Finally, log Firebase events only if the device is not a testing device.

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

UPSIDE: Unlike Firebase's provision of controlling analytics collection, this method will also work on Release builds/APKs.

Balaam answered 20/10, 2017 at 20:13 Comment(0)
F
4

Should be able to do something like following:

    if (BuildConfig.DEBUG) {
        FirebaseAnalytics.getInstance(getApplicationContext()).setAnalyticsCollectionEnabled(false);
     }
Foreandafter answered 4/12, 2016 at 15:33 Comment(1)
but what if i don't want to log events from specific devices even when it is signed APK downloaded from Play Store? Our team often performs tests on live application.Tinhorn
C
3

If what you were looking for, it was to disable the Firebase Analytics / tracking for the test devices that run the Firebase tests or the Play Console Pre-launch reports (which are actually Firebase tests), then you need to disable them based on a system string:

String testLabSetting = Settings.System.getString(context.getContentResolver(), "firebase.test.lab");
if ("true".equals(testLabSetting)) {
    FirebaseAnalytics.getInstance(context).setAnalyticsCollectionEnabled(false);
}

So basically, it's a combination of checking if your device is a Firebase Test device, and then you disable the FirebaseAnalytics for it.

However, it won't entirely stop Firebase Analytics, and the event session_start will keep showing up.

Clyve answered 2/1, 2019 at 9:7 Comment(0)
D
1

The firebase docs only describe that it can be disabled from within the apk.

AndroidManifest.xml

<meta-data android:name="firebase_analytics_collection_enabled" android:value="false" />

Activity.java

setAnalyticsCollectionEnabled(false);

source: https://firebase.google.com/support/guides/disable-analytics

Doane answered 16/11, 2018 at 17:9 Comment(0)
B
1

You can disable event logging for a testing device by running the following:

adb shell setprop debug.firebase.analytics.app package_name

To re-enable logging run:

adb shell setprop debug.firebase.analytics.app .none.

From Firebase itself:

To prevent your testing and development from affecting your measurements, events logged while in debug mode will be excluded from your overall Analytics data, and will not be included in your daily BigQuery export.

Burstone answered 29/11, 2021 at 1:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.