Skip to main content

Events

You can attach and subscribe to events in order to run logic in your app when something happens in Stories. This way, you can:

  • control what is shown,
  • override events from StorifyMe stories,
  • intercept click events and run internal logic,
  • and many other use cases

Example code

The best way to attach listeners is by extending StorifyMeStoryEventProtocol class:

import StorifyMe
class ViewController : UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
}
}

extension ViewController : StorifyMeStoryEventProtocol {
func onLoad(stories: [StorifyMeStoryModel]) {
print("Widget is loaded with \(stories.count) stories")
}

func onFail(error: StorifyMeError) {
print("Error loading widget \(error)")
}

func onStoryOpened(story: StorifyMeStoryModel?, index: Int) {
print("Opened story \(String(describing: story)) with index \(index)")
}

func onStoryClose(story: StorifyMeStoryModel?) {
print("Close story \(String(describing: story))")
}

func onAction(type: String, data: [String: Any]?) {
print("On action : \(type) , data : \(String(describing: data))")
}

func onEvent(type: String, data: [String: Any]?) {
print("On event : \(type) , event : \(String(describing: data))")
}

func onStoryDeeplinkTriggered(story: StorifyMeStoryModel, completion: @escaping((StorifyMeStoryDeeplinkTriggerCompletion) -> Void)) {
if (checkSomeCondition) {
// Don't launch story viewer
completion(.ignorePresentingStory)
} else {
// Continue with default behaviour
completion(.openStoryByDefault)
}
}
}

onLoad()

onLoad method is invoked when the widget loads successfully.

extension ViewController : StorifyMeStoryEventProtocol {
func onLoad(stories: [StorifyMeStoryModel]) {
print("Widget is loaded with \(stories.count) stories")
}
}

onFail()

onFail method is invoked when the widget loading fails. Use this even to for example hide the widget, or re-try loading.

extension ViewController : StorifyMeStoryEventProtocol {
func onFail(error: StorifyMeError) {
print("Error loading widget \(error)")
}
}

onStoryOpened()

onStoryOpened method is invoked any time a user opens a story.

extension ViewController : StorifyMeStoryEventProtocol {
func onStoryOpened(story: StorifyMeStoryModel, index: Int) {
print("Story opened \(story) with index \(index)")
}
}

onStoryClosed()

onStoryClosed method is invoked when the user closes the story and the full screen preview.

extension ViewController : StorifyMeStoryEventProtocol {
func onStoryClosed(story: StorifyMeStoryModel) {
print("Story closed \(story)")
}
}

onAction()

onAction method is invoked when the user engages with one of the engaging components. This way you can override for example a button click event and instead of opening the product details page on the website, you can open it in the app itself.

Or when they answer on the quiz question, based on the answer, you might want to send them to some specific page.

extension ViewController : StorifyMeStoryEventProtocol {
func onAction(type: String, slide: Any, action: String) {
print("Action happened \(type)")
}
}

onEvent()

onEvent method is invoked every time some analytics even is happening. It can be a slide view event, engaging element answer or similar.

Adding different analytics

While this method can be used to collect analytics on your side, we suggest adding the additional analytics processors in StorifyMe admin app under integrations.

extension ViewController : StorifyMeStoryEventProtocol {
func onEvent(event: String, data: Map<String, Any>) {
print("Event happened \(event)")
}
}

onStoryShared()

onStoryShared method is invoked when the user shares the story.

extension ViewController : StorifyMeStoryEventProtocol {
func onStoryShared(story: StorifyMeStoryModel) {
print("Story shared \(story)")
}
}

onStoryDeeplinkTriggered()

The onStoryDeeplinkTriggered method is designed to handle deep links triggered from a Story within the StorifyMe widget. This function provides developers with the ability to customize the behavior when a Story deep link is activated.

extension ViewController : StorifyMeStoryEventProtocol {
func onStoryDeeplinkTriggered(story: StorifyMeStoryModel, completion: @escaping((StorifyMeStoryDeeplinkTriggerCompletion) -> Void)) {
if (checkSomeCondition) {
// Don't launch story viewer
completion(.ignorePresentingStory)
} else {
// Continue with default behaviour
completion(.openStoryByDefault)
}
}
}