Track analytics for a user adding an iOS widget to their Home Screen

Viewed 756

We know it's possible to track user interaction from clicking on the Widget or deep linking, but is it possible to track the user interacting with your app's widget from the Widget library and adding it to their Home Screen? For example, this will help us understand how many people have added our containing app's widget to their Home Screen

2 Answers

There is no callback or anything similar that is executed when a user adds a widget. You could track widget timeline refresh but that will trigger a lot of analytics events.

A better solution would be to use WidgetKit.getCurrentConfigurations(_:) at launch, track which widgets have been added/removed by comparing to a list stored in UserDefaults, and send the corresponding analytics events.

You can use getCurrentConfigurations(_:) to retrieve information about user-configured widgets.

            WidgetCenter.shared.getCurrentConfigurations { widgetInfos in
                guard case .success(let infos) = widgetInfos else { return }
                if let info = infos.first(
                    where: { info in
                      // OR compare is against a UserDefault property that you can set in getTimeline(for:in:completion:).  
                      return info.kind == "SOME_KIND"
                       
                    }
                ) {
                  // Do here whatever you want to do. 
                }
            }

widgetInfos is a list of WidgetInfo

https://developer.apple.com/documentation/widgetkit/widgetinfo

https://developer.apple.com/documentation/widgetkit/widgetcenter/getcurrentconfigurations(_:)

https://developer.apple.com/documentation/widgetkit/intenttimelineprovider/gettimeline(for:in:completion:)-9oqa4

Related