The missing piece in integrating RevenueCat, Firebase, and Flutter
1 min read

The missing piece in integrating RevenueCat, Firebase, and Flutter

RevenueCat's latest integration with Firebase significantly improved their already invaluable service. Using Flutter, however, I found that I wasn't able to easily set the firebaseAppInstanceId property as a subscriber attribute. Here's how I solved this issue.
The missing piece in integrating RevenueCat, Firebase, and Flutter
💡
Update: Firebase Analytics' Flutter plugin now exposes firebaseAppInstanceId. PR here.

RevenueCat's latest integration with Firebase significantly improved their already invaluable service.

We use both services in Clearful, and this integration immediately gave us several key benefits:

  1. More granular subscription events to be used in our A/B testing.
  2. Querying subscription data became faster and more accessible.
  3. It allowed us to more easily create subscription lifecycle events such as a free trial ending push notification, among others.

When following the integration documentation, however, I learned that the firebaseAppInstanceId property hasn't yet been exposed by the Firebase team in the Flutter/Firebase Analytics plugin.

I briefly considered writing a Flutter plugin myself to grab the firebaseAppInstanceId from Firebase Analytics' iOS and Android plugins, but thankfully common sense prevailed, and I first decided to search and see if anyone else had already done this. And voila! Enter firebase_instance_id - a simple plugin that does precisely what I needed and nothing else.

Once I found this plugin, setting firebaseAppInstanceId as a subscriber attribute in RevenueCat was simple:

  Future<void> init() async {
    Purchases.setDebugLogsEnabled(true);
    Purchases.addPurchaserInfoUpdateListener(_purchaserInfoUpdateListener);
    Purchases.addReadyForPromotedProductPurchaseListener(_readyForPromotedProductPurchaseListener);

    await Purchases.setup(Global.revenueCatApiKey);
    await _setFirebaseAppInstanceId();
  }

  Future<void> _setFirebaseAppInstanceId() async {
    final appInstanceId = await FirebaseInstanceId.appInstanceId ?? '';
    if (appInstanceId.isNotEmpty) {
      final attribute = {'\$firebaseAppInstanceId': appInstanceId};
      await Purchases.setAttributes(attribute);
    }
  }
Sending Firebase firebaseAppInstanceId to RevenueCat in Flutter