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 running:
StorifyMe.instance.eventListener = object : StorifyMeEventListener() {
override fun onLoad(widgetId: Long, stories: List<StoryWithSeen>) {
Log.d(TAG, "onLoad() - widgetId: $widgetId - ${stories.joinToString()}")
}
override fun onFail(error: StorifyMeError) {
Log.d(TAG, "onFail() - ${error.message} - ${error.type}")
}
override fun onStoryOpened(story: StoryWithSeen, index: Int) {
Log.d(TAG, "onStoryOpened() $story : index: $index")
}
override fun onStoryClosed(story: StoryWithSeen) {
Log.d(TAG, "onStoryClosed() $story")
}
override fun onAction(type: String, data: JSONObject) {
Log.d(TAG, "onAction() - type: $type - data: $action")
}
override fun onEvent(type: String, data: JSONObject) {
Log.d(TAG, "onEvent() - type: $type - data: $data")
}
override fun onShopping(type: String, data: JSONObject) {
Log.d(TAG, "onShopping() - type: $type - data: $data")
}
override fun onStoryShared(story: StoryWithSeen) {
Log.d(TAG, "onStoryShared() $story")
}
override fun onStoryDeeplinkTriggered(
story: StoryWithSeen,
completion: (StorifyMeStoryDeeplinkTriggerCompletion) -> Unit
) {
if (checkSomeCondition) {
// Don't launch story viewer
completion(StorifyMeStoryDeeplinkTriggerCompletion.IGNORE_PRESENTING_STORY)
} else {
// Continue with default behaviour
completion(StorifyMeStoryDeeplinkTriggerCompletion.OPEN_STORY_BY_DEFAULT)
}
}
override fun onLinkOpenTriggered(
url: String,
completion: (StorifyMeLinkTriggerCompletion) -> Unit
) {
if (checkSomeCondition) {
// Prevent opening the link and handle manual link opening
completion(StorifyMeLinkTriggerCompletion.IGNORE_PRESENTING_LINK)
} else {
// Proceed with the default link handling
completion(StorifyMeLinkTriggerCompletion.OPEN_LINK_BY_DEFAULT)
}
}
}
onLoad()
onLoad
method is invoked when the widget loads successfully.
StorifyMe.instance.eventListener = object : StorifyMeEventListener() {
override fun onLoad(widgetId: Long, stories: List<StoryWithSeen>) {
Log.d(TAG, "onLoad() ${stories.joinToString()}")
}
}
onFail()
onFail
method is invoked when the widget loading fails. Use this even to for example hide the widget, or re-try loading.
StorifyMe.instance.eventListener = object : StorifyMeEventListener() {
override fun onFail(error: StorifyMeError) {
Log.d(TAG, "onFail() ${error.message}")
}
}
onStoryOpened()
onStoryOpened
method is invoked any time a user opens a story.
StorifyMe.instance.eventListener = object : StorifyMeEventListener() {
override fun onStoryOpened(story: StoryWithSeen, index: Int) {
Log.d(TAG, "onStoryOpened() $story : index: $index")
}
}
onStoryClosed()
onStoryClosed
method is invoked when the user closes the story and the full screen preview.
StorifyMe.instance.eventListener = object : StorifyMeEventListener() {
override fun onStoryClosed(story: StoryWithSeen) {
Log.d(TAG, "onStoryClosed() $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.
StorifyMe.instance.eventListener = object : StorifyMeEventListener() {
override fun onAction(type: String, data: JSONObject) {
Log.d(TAG, "onAction() - type: $type - data: $action")
}
}
onEvent()
onEvent
method is invoked every time some analytics even is happening. It can be a slide view event, engaging element answer or similar.
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.
StorifyMe.instance.eventListener = object : StorifyMeEventListener() {
override fun onEvent(type: String, data: JSONObject) {
Log.d(TAG, "onEvent() - type: $type - data: $data")
}
}
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
StorifyMe.instance.eventListener = object : StorifyMeEventListener() {
override fun onShopping(type: String, data: JSONObject) {
Log.d(TAG, "onShopping() - type: $type - data: $data")
}
}
onStoryShared()
onStoryShared
method is invoked when the user shares the story.
StorifyMe.instance.eventListener = object : StorifyMeEventListener() {
override fun onStoryShared(story: StoryWithSeen) {
Log.d(TAG, "onStoryShared() $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.
StorifyMe.instance.eventListener = object : StorifyMeEventListener() {
override fun onStoryDeeplinkTriggered(
story: StoryWithSeen,
completion: (StorifyMeStoryDeeplinkTriggerCompletion) -> Unit
) {
if (checkSomeCondition) {
// Don't launch story viewer
completion(StorifyMeStoryDeeplinkTriggerCompletion.IGNORE_PRESENTING_STORY)
} else {
// Continue with default behavior
completion(StorifyMeStoryDeeplinkTriggerCompletion.OPEN_STORY_BY_DEFAULT)
}
}
}
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.
StorifyMe.instance.eventListener = object : StorifyMeEventListener() {
override fun onLinkOpenTriggered(
url: String,
completion: (StorifyMeLinkTriggerCompletion) -> Unit
) {
if (checkSomeCondition) {
// Prevent opening the link and handle manual link opening
completion(StorifyMeLinkTriggerCompletion.IGNORE_PRESENTING_LINK)
} else {
// Proceed with the default link handling
completion(StorifyMeLinkTriggerCompletion.OPEN_LINK_BY_DEFAULT)
}
}
}
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(StorifyMeLinkTriggerCompletion.IGNORE_PRESENTING_LINK)
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
private var elementType: String? = null
private var buttonValue: String? = null
StorifyMe.instance.eventListener = object : StorifyMeEventListener() {
override fun onAction(type: String, data: JSONObject?) {
// Extract "data" object
val buttonData = data.optJSONObject("data")
// Extract button value
buttonValue = buttonData?.optString("value", "")
// Extract the element type (e.g., BUTTON, PRODUCT_TAG, etc.)
elementType = data.optString("type", "")
}
override fun onLinkOpenTriggered(
url: String,
completion: (StorifyMeLinkTriggerCompletion) -> Unit
) {
// This condition is just an example. You can replace it with any other condition that fits your use case.
// For example:
// val 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.
val 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(StorifyMeLinkTriggerCompletion.IGNORE_PRESENTING_LINK)
} else {
// Proceed with the default link handling
completion(StorifyMeLinkTriggerCompletion.OPEN_LINK_BY_DEFAULT)
}
// Reset values after handling the link open trigger
// This ensures the elementType and buttonValue are cleared for the next event or action.
elementType = null
buttonValue = null
}
}