urbanairship / ios-library

Urban Airship iOS SDK
http://urbanairship.com
Apache License 2.0
478 stars 265 forks source link

In-App Automation - Screenview trigger event #370

Closed mmdumi closed 9 months ago

mmdumi commented 1 year ago

Preliminary Info

What Airship dependencies are you using?

Airship Core, Basement, Automation - Version 17.0.2 - xcframework

What are the versions of any relevant development tools you are using?

xcode 14.3.1 - iOS 16.5.1

Report

What unexpected behaviour are you seeing?

In-App Automation doesn't get trigger on the app open when the trigger event is met. I need to take the app in background and back to foreground to get the aumation to show on screen.

What is the expected behaviour?

Please have a look at this automation: https://go.airship.eu/apps/Ks9j3GltSuKMeti3XBezhQ/composer/in-app-automation/166Z2EdfTiCZpmZBKhqUlQ/ It has "Screenview" as a trigger. Each time the app is opened I'm setting "MapScreen" as the current screen and the automation should be triggered. Unfortunately I need to open the app twice in order for the automation to be displayed.

What are the steps to reproduce the unexpected behavior?

Create an In-App Automation with a screen view trigger then open your app and set the current screen to be the triggered screen. Airship.analytics.trackScreen("ScreenName")

rlepinski commented 1 year ago

Where are you making the trackScreen call?

mmdumi commented 1 year ago

When opening the map screen in viewDidLoad, on the main thread.

rlepinski commented 1 year ago

I think whats happening is the IAA needs to be fetched and scheduled before the screen event happens. Since this seems like its on your first screen, it might be better to use an open trigger with a current screen condition

mmdumi commented 1 year ago
Screenshot 2023-07-12 at 13 49 55

Could you please try this behaviour on your side also ? I've just tested with app open trigger + current screen and the behaviour is the same: the in app message is being shown the second time the app is opened. This looks like a bug from my side :|

rlepinski commented 1 year ago

I was able to get it to work just fine. The app screen trigger only worked if the message was already received on the device but I had no issue with the app open + screen condition.

Could you add a breakpoint here - https://github.com/urbanairship/ios-library/blob/main/Airship/AirshipAutomation/Source/UAAutomationEngine.m#L636 to verify the screen is being set properly

If that looks good, verbose logs would be helpful. You can send them to me directly ryan.lepinski@airship.com if you want to avoid filtering out any keys/secrets.

rlepinski commented 1 year ago

Looks like you are using xcframeworks so the breakpoint wont be that easy to set. You can use this to listen for the NSNotification that feeds the automation engine:

        NotificationCenter.default.addObserver(forName: AirshipAnalytics.screenTracked, object: nil, queue: nil) { notification in
            let screen = notification.userInfo?[AirshipAnalytics.screenKey] as? String
            print("Screen \(screen ?? "")")
        }
mmdumi commented 1 year ago

Did the above and found that AirshipAnalytics.screenTracked notification behaved like this:

rlepinski commented 1 year ago

I think whats happening is you are calling trackScreen before the application did become active notification, where we try to restore the previously tracked screen from the background. In this case it would be nil. So I think its:

  1. trackScreen(mapView)
  2. application becomes active
  3. trackScreen(previousScreen) // nil

And things don't work. We should probably look to see a current screen is not set before doing that in the active/foreground event, but that will take some investigating that we wont break something else by doing that

rlepinski commented 9 months ago

Should be fixed in SDK 18.0.0-beta.2