Firebase Analytics and iOS Widgets in iOS14

Geert Michiels
4 min readMar 14, 2021

I recently written an iOS14 widget for my https://snookercoaches.com app, you can check it out here: iOS widget with firebase auth and realtime database

Having this widget is one thing, knowing how many users are actually using it is another. As mobile solution architect, I get this question a lot : “how many people use widgets (in our app)?”. It tends to be a big motivation to invest or not invest in the effort of adding a widget to a mobile application.

When this talk pops up with a customer, I confirm that indeed, the amount of users using widgets (in your app; remember is can vary a lot per app!) is certainly a factor to consider in investing in Widgets. However, I always immediatly explain that both Android and Apple are making big efforts to break out of the standard “app square icon”. Widgets, App Clips, Siri integration, sharing extensions… they are all evidence that Apple sees an app as an important part of a much bigger eco system. These functionalities should not be neglected and it’s wise to invest in them (if they fit your app).

The SnookerCoach 147 app has already an integration with Firebase Analytics, in this article I will try to answer the following questions:

  • can the iOS app know that its widgets are in use?
  • if the main iOS app knows widget usage, surely we can post some sort of analytics back to Firebase Analytics?
  • Analytics about user taps on the widget (links)?
  • is it possible to get Firebase Analytics events directly out of the iOS14/Widget extension (without passing via the main app) ?

Can the iOS app know that its widgets are in use?

WidgetCenter.share.getCurrentConfigurations

Using the WidgetCenter.shared.getCurrentConfigurations the main app can ask iOS which widgets are currently in operation: kind of widget, size (small, medium…) and user configuration. When I logged the information in my SnookerCoach 147 app for 2 configured widgets I got the following:

success([
WidgetInfo:
- configuration: nil
- family: systemSmall
- kind: dev.widget.high_break_graph,
WidgetInfo:
- configuration: nil
- family: systemMedium
- kind: dev.widget.high_break_graph
])

This matches up: I curently have only 1 type of widget available (.high_break_graph) and I have added it both as systemSmall and systemMedium :

In addition, the widget has no user configurable settings that is why it is nil.

Posting information back from the widget to the main iOS app

The above solution is great, and works out of the box! However, I wanted to also know when the widget’s information was refreshed for the last time. Or more technically when was the last time getTimeline was requested from the TimelineProvider.

By setting up a shared app group to exchange information between the widget extension and the main app this can easiliy be accomplished.

Shared between app and widget extension:

In the widget extension:

The main app:

And its even possible this way, to know if the system requested a placeholder or getSnapshot to get an insight if maybe the user looked at the widget in the widget center but decided not to add it. Just insert simular code in these TimelineProvider entry functions.

Post back widget usage information back to Firebase Analytis from main app

Firebase Analytics works with events and user properties. I thought it might be interesting to know how many of the users have widgets configured, and if so how many widgets. So I created an extra user property called “numberOfWidgets”.

Each time the app enters the foreground, the widget information is retrieved and posted to Firebase Analytics:

Using the Firebase DebugView you can see the user property being correctly transmitted to Firebase:

Analytics of the user taps on your widget (links)

IOS supports 2 user interactions for a widget:

  • user taps on any part of the widget (set a widgetUrl)
  • user taps on a link (specific area) in your widget

Both interactions open the main app and call the appdelegate with a specific deeplink url. It is therefore straight forward to have the app send a Firebase event specific for the tap action and track how many users tap on what part of the widget.

Is it possible to get Firebase Analytics directly out of the iOS14/Widget extension (without passing via the main app)?

A short answer: no its not possible as stated in this github post.

Also given the fact the Apple clearly states that widgets offer no user interaction (beyond just pressing on a link in the widget and having the main app openend to possibly a specific location) it does not really makes much sense appart from maybe screen views (which would be nice but probably does not play well with GDPR…).

I did however try it but indeed, never got anything visible in the Firebase Analytics DebugView, this is what I tried:

  • added Firebase frameworks to the Widget app extension target
  • initialised Firebase in widget.init() (as also required for using Auth in the extension which DOES work as you can see in my previous article).
  • in the getTimeline of the TimelineProvider fired the analytics event “did.refresh.widget” (just a name I choose)
  • I never got this event to show up in the Firebase Analytics DebugView. I assume Firebase is probably doing some background task to upload the events to the backend which for extensions is not possible.

Conclusion

It is certainly possible to get widget usage analytics in Firebase Analytics both for knowing (1) which widgets have been added by the user and (2) on what the user is pressing in the widget(s).

Just have the main app interrogate iOS for the usage and post it back to Firebase at each app launch. Integration of the widget extension directly with Firebase Analytics is not working.

--

--

Geert Michiels

Engineer that only sees solutions never a problem. IT prof with over 30 years experience: dev, pm, cto, psm and solution architect. Frontend, mobile, web, iot.