Skip to content

Commit

Permalink
add deserialization analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
simond-stripe committed Jan 8, 2025
1 parent bc80c32 commit 3dc0efa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -357,29 +357,41 @@ internal class StripeConnectWebViewContainerImpl<Listener, Props>(

@JavascriptInterface
fun onSetterFunctionCalled(message: String) {
val parsed = ConnectJson.decodeFromString<SetterFunctionCalledMessage>(message)
val parsed = tryDeserializeWebMessage<SetterFunctionCalledMessage>(
webFunctionName = "onSetterFunctionCalled",
message = message,
) ?: return
logger.debug("Setter function called: $parsed")

controller?.onReceivedSetterFunctionCalled(parsed)
}

@JavascriptInterface
fun openSecureWebView(message: String) {
val secureWebViewData = ConnectJson.decodeFromString<SecureWebViewMessage>(message)
val secureWebViewData = tryDeserializeWebMessage<SecureWebViewMessage>(
webFunctionName = "openSecureWebView",
message = message,
)
logger.debug("Open secure web view with data: $secureWebViewData")
}

@JavascriptInterface
fun pageDidLoad(message: String) {
val pageLoadMessage = ConnectJson.decodeFromString<PageLoadMessage>(message)
val pageLoadMessage = tryDeserializeWebMessage<PageLoadMessage>(
webFunctionName = "pageDidLoad",
message = message,
) ?: return
logger.debug("Page did load: $pageLoadMessage")

controller?.onReceivedPageDidLoad(pageLoadMessage.pageViewId)
}

@JavascriptInterface
fun accountSessionClaimed(message: String) {
val accountSessionClaimedMessage = ConnectJson.decodeFromString<AccountSessionClaimedMessage>(message)
val accountSessionClaimedMessage = tryDeserializeWebMessage<AccountSessionClaimedMessage>(
webFunctionName = "accountSessionClaimed",
message = message,
) ?: return
logger.debug("Account session claimed: $accountSessionClaimedMessage")

controller?.onMerchantIdChanged(accountSessionClaimedMessage.merchantId)
Expand All @@ -393,6 +405,22 @@ internal class StripeConnectWebViewContainerImpl<Listener, Props>(
}
}

private inline fun <reified T> tryDeserializeWebMessage(
webFunctionName: String,
message: String,
): T? {
return try {
ConnectJson.decodeFromString<T>(message)
} catch (e: IllegalArgumentException) {
controller?.onErrorDeserializingWebMessage(
webMessage = message,
error = "Unable to deserialize message from $webFunctionName",
errorMessage = e.message,
)
null
}
}

private fun WebView.evaluateSdkJs(function: String, payload: JsonObject) {
val command = "$ANDROID_JS_INTERFACE.$function($payload)"
post {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ internal class StripeConnectWebViewContainerController<Listener : StripeEmbedded
}
}

fun onErrorDeserializingWebMessage(
webMessage: String,
error: String,
errorMessage: String?,
) {
analyticsService.track(ConnectAnalyticsEvent.WebErrorDeserializeMessage(
message = webMessage,
error = error,
errorDescription = errorMessage,
pageViewId = stateFlow.value.pageViewId,
))
}

/**
* Callback whenever the merchant ID changes, such as in the
*/
Expand Down

0 comments on commit 3dc0efa

Please sign in to comment.