Skip to content

Commit fd5734d

Browse files
evan-masseauclaude
andauthored
docs(forms): add form lifecycle events section to README (#446)
* docs(forms): add form lifecycle events section to README Documents the FormLifecycleEvent API introduced in PR #434, including registration/unregistration, threading guarantee, and per-event semantics. Adds a feature matrix row and TOC entry. Example verified via :sample:assembleDebug. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * docs(forms): drop edge-case carve-outs from lifecycle events section Trim 'fires only when CTA has deep link' and 'does not fire on internal teardown' notes — these are translation-layer artifacts, not behavior worth highlighting in docs aimed at integrators. --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent cfff6d6 commit fd5734d

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ send them timely push notifications via [FCM (Firebase Cloud Messaging)](https:/
3838
- [Setup](#setup-1)
3939
- [In-App Forms Session Configuration](#in-app-forms-session-configuration)
4040
- [Unregistering from In-App Forms](#unregistering-from-in-app-forms)
41+
- [Monitoring Form Lifecycle Events](#monitoring-form-lifecycle-events)
4142
- [Geofencing](#geofencing)
4243
- [Setup](#setup-2)
4344
- [Requesting Permissions](#requesting-permissions)
@@ -701,6 +702,7 @@ See the table below to understand available features by SDK version.
701702
| Time Delay | 4.0.0 |
702703
| Audience Targeting | 4.0.0 |
703704
| Event Triggers | 4.1.0 |
705+
| Form Lifecycle Hooks | 4.4.0 |
704706

705707
### Setup
706708
To begin, call `Klaviyo.registerForInAppForms()` after initializing the SDK with your public API key.
@@ -793,6 +795,76 @@ object to the `registerForInAppForms()` method. For example, to set a session ti
793795

794796
**Note:** After unregistering, the next call to `registerForInAppForms()` will be considered a new session by the SDK.
795797

798+
### Monitoring Form Lifecycle Events
799+
800+
> Form lifecycle events are available in SDK version 4.4.0 and higher.
801+
802+
You can register a handler to receive callbacks whenever a form is shown, dismissed, or a CTA button is tapped.
803+
This is useful for forwarding engagement data to a third-party analytics platform such as Amplitude, Segment, or Mixpanel.
804+
805+
The handler is invoked on the **main thread**, so avoid performing long-running or blocking work inside it.
806+
807+
<details open>
808+
<summary>Kotlin</summary>
809+
810+
```kotlin
811+
import com.klaviyo.analytics.Klaviyo
812+
import com.klaviyo.forms.FormLifecycleEvent.FormCtaClicked
813+
import com.klaviyo.forms.FormLifecycleEvent.FormDismissed
814+
import com.klaviyo.forms.FormLifecycleEvent.FormShown
815+
import com.klaviyo.forms.registerFormLifecycleHandler
816+
import com.klaviyo.forms.unregisterFormLifecycleHandler
817+
818+
Klaviyo.registerFormLifecycleHandler { event ->
819+
when (event) {
820+
is FormShown -> {
821+
// e.g. myAnalytics.track("Form Shown", mapOf("formId" to event.formId, "formName" to event.formName))
822+
}
823+
is FormDismissed -> {
824+
// e.g. myAnalytics.track("Form Dismissed", mapOf("formId" to event.formId, "formName" to event.formName))
825+
}
826+
is FormCtaClicked -> {
827+
// e.g. myAnalytics.track("Form CTA Clicked", mapOf(
828+
// "formId" to event.formId,
829+
// "formName" to event.formName,
830+
// "buttonLabel" to event.buttonLabel,
831+
// "deepLinkUrl" to event.deepLinkUrl.toString()
832+
// ))
833+
}
834+
}
835+
}
836+
837+
// To stop receiving events, unregister the handler
838+
Klaviyo.unregisterFormLifecycleHandler()
839+
```
840+
</details>
841+
842+
<details>
843+
<summary>Java</summary>
844+
845+
```java
846+
import com.klaviyo.forms.FormLifecycleEvent;
847+
import com.klaviyo.forms.KlaviyoForms;
848+
849+
KlaviyoForms.registerFormLifecycleHandler(event -> {
850+
if (event instanceof FormLifecycleEvent.FormShown shown) {
851+
// e.g. myAnalytics.track("Form Shown", ...)
852+
} else if (event instanceof FormLifecycleEvent.FormDismissed dismissed) {
853+
// e.g. myAnalytics.track("Form Dismissed", ...)
854+
} else if (event instanceof FormLifecycleEvent.FormCtaClicked ctaClicked) {
855+
// e.g. myAnalytics.track("Form CTA Clicked", ...)
856+
}
857+
});
858+
859+
// To stop receiving events, unregister the handler
860+
KlaviyoForms.unregisterFormLifecycleHandler();
861+
```
862+
</details>
863+
864+
Registering a lifecycle handler is optional and does not affect normal form behavior — forms are displayed and dismissed
865+
regardless of whether a handler is registered. Only one handler can be registered at a time; calling
866+
`registerFormLifecycleHandler` again replaces the previous registration.
867+
796868
## Geofencing
797869

798870
[Geofencing](https://help.klaviyo.com/hc/en-us/articles/45194892526747) allows you to trigger events when users enter or exit geographic regions defined in your Klaviyo account.

0 commit comments

Comments
 (0)