Reacting to Survey Lifecycle Events

The InMoment SDK provides the ability to receive and react to events that occur during the lifecycle of a Survey. Some use cases include:

  • Customizing the colors, fonts, and styles of the survey prior to presentation
  • Handling errors
  • Rewarding the user for completing a survey
  • Automatically dismissing the survey when the user has arrived on the last page
  • Throttling
  • Logging

Using a Feedback Manager Delegate

A FeedbackManagerDelegate can be used to receive and react to events that occur during the lifecycle of a Survey. To start receiving events, implement the FeedbackManagerDelegate protocol and set FeedbackManager.delegate to your instance:

import InMoment

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, FeedbackManagerDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        FeedbackManager.shared.delegate = self
        return true
    }

    func feedbackManager(shouldPresent survey: Survey, _ animated: Bool) -> Bool {
        // Insert your code here. This method is optional.
        return true
    }

    func feedbackManager(willPresent survey: Survey, _ animated: Bool) {
        // Insert your code here. This method is optional.
    }

    func feedbackManager(surveyDidPassCompletionPoint survey: Survey) {
        // Insert your code here. This method is optional.
    }

    func feedbackManager(surveyDidArriveOnLastPage survey: Survey) {
        // Insert your code here. This method is optional.
    }

    func feedbackManager(surveyDidFail survey: Survey, withError error: Error) {
        // Insert your code here. This method is optional.
    }

    // Additional lifecycle methods...

}

For a complete list of lifecycle methods and their descriptions, take a look at the reference documentation for FeedbackManagerDelegate.

React to Survey Completion

There are two ways to record survey completion: once the user passes the completion point of the survey, and once the user arrives on the last page of the survey. The former is appropriate for determining when the survey response is considered complete according to the InMoment platform and will be available in reports. The latter is more appropriate for determining when no other survey pages exist, and for recording information like the survey’s redemption code.

After Passing the Completion Point:

This method is called when the user arrives at the page immediately following the survey completion point. Once this happens, the survey response will be available in InMoment reports and in Hub 2.0™, (even if the user doesn’t continue until the very last page of the survey). Implement this method in your application’s FeedbackManagerDelegate to perform actions such as recording that the user has finished taking the survey or giving the user a reward:

func feedbackManager(surveyDidPassCompletionPoint survey: Survey) {
    myAppUser.points.increment(by: 100)
}

Upon Arrival on Last Page:

This method is called when the user arrives at the last page of the survey. Implement this method in your application’s FeedbackManagerDelegate to perform actions such as recording the redemption and dismissing the survey:

func feedbackManager(surveyDidArriveOnLastPage survey: Survey) {
    DispatchQueue.main.async {
        let alert = UIAlertController(title: "Thank you!", message: "Your feedback has been recorded.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in survey.dismiss(animated: true) }))
        survey.present(alert, animated: true)
    }
}

Handle Errors

This method is called when an error occurs while loading or during the survey. Implement this method in your application’s FeedbackManagerDelegate to perform actions such as attempting to restart the survey or logging the error and dismissing the survey.

func feedbackManager(surveyDidFail survey: Survey, withError error: Error) {
    NSLog(error.localizedDescription)
    NSLog(error.localizedFailureReason ?? "")
    NSLog(error.localizedRecoverySuggestion ?? "")
    DispatchQueue.main.async {
        let alert = UIAlertController(title: "Oops!", message: "The survey could not be presented at this time. Please try again later.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { _ in survey.dismiss(animated: true) }))
        alert.addAction(UIAlertAction(title: "Reload", style: .default, handler: { _ in survey.startOver() }))
        survey.present(alert, animated: true)
    }
}

Next Steps