25

I know Firebase in general works offline, and syncs whenever an opportunity. Does the same thing apply to Firebase-Analytics for mobile (Android, iOS) apps?

If yes (or partially yes) - how long can the user be offline?

Ronen Rabinovici
  • 8,680
  • 5
  • 34
  • 46

5 Answers5

40

Yes, events are stored locally and uploaded when the radio is turned on. Events which arrive more than 72 hours late will be ignored by our server.

Steve Ganem
  • 10,591
  • 2
  • 39
  • 32
  • 3
    is there any way to extend this time limit i.e. for logging stale or older events which arrive more than 72 hours late on Firebase Server. – Narendra Mar 06 '17 at 12:31
  • 3
    When building for billions (India!), we shouldn't be surprised to see user coming online only every few days. So many useful events for an utility app will be lost due to this firebase policy, assuming a typical utility app like calculator doesn't require internet to use. – rpattabi Sep 01 '17 at 13:39
  • Is there any way to access the locally stored events directly? – antihero989 Jul 02 '18 at 17:45
  • 9
    Can anyone share a link to a google document about this limit. – Alborz Nov 26 '18 at 10:34
  • 5
    I think google firebase team should put this limitation in document since I cannot see it in official document. We're confused when comparing our system transactions and firebase – Hieu Le Apr 09 '19 at 16:15
  • @steve-ganem does the same apply to Firebase Analytics for web? – Max Jul 10 '20 at 06:50
  • Are these events also still in the cache after a device reboot? – Emil S. Jan 23 '23 at 15:02
11

Firebase Analytics will store logged evens locally on the device (online or offline). When its time to upload the data and the devices has network connection Firebase Analytics will batch the data in as fewer uploads as possible, compress it and attempt to upload the data.

When the upload is successful the data is deleted from the device. When the upload fails a new attempt is scheduled with progressive back-off. If the device is offline the app will wait for connectivity before attempting upload.

Data older than 72 hours will be ignored. There are safety limits on how much data can be stored on the device (to limit disk usage). Logging excessive data on device that is offline for long period of time might lead to data loss.

WSBT
  • 33,033
  • 18
  • 128
  • 133
djabi
  • 5,601
  • 18
  • 25
  • 4
    Thanks for the answer. I'm having trouble finding this information in the Firebase documentation. Do you think you could provide a link to the page that talks about offline data expiration? – Esper Feb 21 '18 at 21:35
  • thanks a lots, but I can't find official document, so where did you get these information? – Luan Si Ho Jan 06 '20 at 08:59
  • @djabi Does this syncing done by firebase automatically or should I do it one my own? – Sakhawat Hossain Jun 11 '20 at 08:07
  • @djabi Does the same apply to Firebase Analytics for Web? – Max Jul 10 '20 at 07:16
4

Sept 2019 source: Firebase offline: What works, what doesn't, and what you need to know (Firebase Summit 2019)

Performance Monitoring & Firebase Analytics discards data that is generally >72 hours old

Measurement tools (Perf, Analytics, Crashlytics) are suspicious of data that looks too old - Todd Kerpelman @ Firebase Summit 2019

Notably, this 72hr limit is not a hard limit like the cache size and event limits are, so there are times where more than 72hrs can be cached offline and reported back to the servers though this is not guaranteed.

Summary of Perf, Analytics, Crashlytics in above video:

  • Performance Monitoring
    • Android: 10MB cache across all apps
    • iOS: 10MB cache across each app
    • oldest data is purged if limit is reached
    • discards data that is generally >72 hours old
  • Analytics:
    • 100,000 events
    • most recent data gets eliminated
    • discards data that is generally >72 hours old if event limit is reached
  • Crashlytics:
    • 9 crashed (up to 150K each)
    • re-writes timestamp of older data that it doesn't recognize to today as you generally want crash data

All subject to change - Todd Kerpelman @ Firebase Summit 2019

Other information:

  • Data is uploaded using exponential backoff and retry methods
  • Android uploads data via Google Play Services and so can send the data even when the app is not running (data is uploaded on crash) vs iOS where data is uploaded on next app start

I still cannot find a source for this information in the official Firebase documentation.

jthegedus
  • 705
  • 1
  • 7
  • 16
1

It's possible to bypass the 72 hours time limit on analytics. First, you need to add the permission

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

in your manifest file. Before logging events, check if internet is available using this method:

private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

using the method above, check if the user is online if so, log events as usual, if not, don't log it right now but save the event name and its parameters as string, using SharedPreferences.

if(isNetworkAvailable()){
// the user is online, log the events
}else{
// Don't log the events, save the event name and its parameters using SharedPreferences
}

Register BroadcastReceiver listening connectivity change events, add the following in your manifest file:

<receiver
android:name="type your class directory extending BroadcastReceiver here"
        android:enabled="true"
        android:exported="true">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
</receiver>

In the class extending BroadcastReceiver, check if network is available and log the events saved by SharedPreferences

public class NetworkEnabledBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(isNetworkAvailable(context)){
        //Check if there are saved events and parameters using the same named
        //SharedPreferences used for saving the events and parameters, log if 
        //so, then clear the data. 
        }
    }
    private boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager
                = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
}
Ozan
  • 1,381
  • 2
  • 10
  • 13
  • This will not work probably because the actual time the data was logged in the device will not correspond to the time it will be logged in the Analytics. – NullByte08 Mar 29 '20 at 13:58
0

From Firebase Blog, analytics data is batched up and sent down when:

  • The client library sees that there's any local data that's an hour old.
  • If your user triggers a conversion event (like making an in-app purchase).
  • Whenever your app goes into the background.

So if a user tries your app for 20 minutes and then uninstalls it, that session data will still be received, because your app will have sent down the data the moment the user moved your app to the background.

The one corner case where analytics data would actually get lost on iOS would be if your app crashed and then your user immediately uninstalled it.

Document: https://firebase.googleblog.com/2016/11/how-long-does-it-take-for-my-firebase-analytics-data-to-show-up.html

Luan Si Ho
  • 1,438
  • 2
  • 13
  • 17