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 onShopping(type: String, data: [String: Any]?) {
print("Shopping action happened \(type)")
}

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

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

func onLinkOpenTriggered(url: String, completion: @escaping ((StorifyMeLinkTriggerCompletion) -> Void)) {
if (checkSomeCondition) {
// Prevent opening the link and handle manual link opening
completion(.ignorePresentingLink)
} else {
// Proceed with the default link handling
completion(.openLinkByDefault)
}
}
}

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)")
}
}

onShopping()

onShopping method is invoked every time some cart manipulation is happening. Supported types:

  • CART_UPDATED - Happens every time cart is changed
  • CART_ITEM_ADDED - Happens when a product is added to cart
  • CART_ITEM_REMOVED - Happens when a product is removed from the cart
  • CHECKOUT - Happens when user clicks on Checkout button in cart
extension ViewController : StorifyMeStoryEventProtocol {
func onShopping(type: String, data: [String: Any]?) {
print("Shopping action happened \(type)")
}
}

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)
}
}
}

onLinkOpenTriggered()

The onLinkOpenTriggered method handles link openings within a Story, including links from Call to Action (CTA) buttons, product tags, deep links, and swipe-up actions. This function allows developers to customize how these links are processed when activated.

extension ViewController : StorifyMeStoryEventProtocol {
func onLinkOpenTriggered(url: String, completion: @escaping ((StorifyMeLinkTriggerCompletion) -> Void)) {
if (checkSomeCondition) {
// Prevent opening the link and handle manual link opening
completion(.ignorePresentingLink)
} else {
// Proceed with the default link handling
completion(.openLinkByDefault)
}
}
}

If you need additional information about the link, you can retrieve it from the onAction event. For example, if you want to identify whether the triggered link is a CTA button, a product tag, or another link type, you can access this information in the onAction event. The onAction event is triggered first, followed by the onLinkOpenTriggered event.

Additionally, if you don't specify any conditions, the settings will apply to all links. For example, if you set completion(.ignorePresentingLink) without any conditions, this will prevent all links from being opened within the story, regardless of their type.

Below is an example of how to get the type of the triggered element.

Click to view full code
var elementType: String?
var buttonValue: String?

func onAction(type: String, data: [String: Any]?) {
if let data = data {
// Extract value
if let buttonData = data["data"] as? [String: Any] {
buttonValue = buttonData["value"] as? String
print("Value: \(String(describing: buttonValue))")
}

// Extract the type of the element (e.g., BUTTON, PRODUCT_TAG, etc.)
if let extractedType = data["type"] as? String {
elementType = extractedType
print("Element Type: \(extractedType)")
}
}
}

func onLinkOpenTriggered(url: String, completion: @escaping ((StorifyMeLinkTriggerCompletion) -> Void)) {
// This condition is just an example. You can replace it with any other condition that fits your use case.
// For example:
// let checkSomeCondition = url.contains("example.com") // A condition to check if the URL contains "example.com"
// Check if the element type is "BUTTON" and if the button's value matches the URL.
let checkSomeCondition = (elementType == "BUTTON" && buttonValue == url) // Example condition, can be replaced with your own logic

if checkSomeCondition {
// Prevent opening the link and handle manual link opening
completion(.ignorePresentingLink)
} else {
// Proceed with the default link handling
completion(.openLinkByDefault)
}

// Reset values after handling the link open trigger
// This ensures the elementType and buttonValue are cleared for the next event or action.
elementType = nil
buttonValue = nil
}