Skip to content

Commit 63552c0

Browse files
authored
set preloading tag on instrumentation event (#117)
1 parent 6cb7598 commit 63552c0

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/src/main/java/com/shopify/checkoutsheetkit/CheckoutWebView.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = n
4242
override val recoverErrors = true
4343
override val variant = "standard"
4444
override val cspSchema = CheckoutBridge.SCHEMA_VERSION_NUMBER
45+
var isPreload = false
4546

4647
private val checkoutBridge = CheckoutBridge(CheckoutWebViewEventProcessor(NoopEventProcessor()))
4748
private var loadComplete = false
@@ -95,6 +96,7 @@ internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = n
9596

9697
fun loadCheckout(url: String, isPreload: Boolean) {
9798
initLoadTime = System.currentTimeMillis()
99+
this.isPreload = isPreload
98100
Handler(Looper.getMainLooper()).post {
99101
val headers = if (isPreload) mutableMapOf("Sec-Purpose" to "prefetch") else mutableMapOf()
100102
loadUrl(url, headers)
@@ -114,7 +116,10 @@ internal class CheckoutWebView(context: Context, attributeSet: AttributeSet? = n
114116
val timeToLoad = System.currentTimeMillis() - initLoadTime
115117
checkoutBridge.sendMessage(view, CheckoutBridge.SDKOperation.Instrumentation(
116118
InstrumentationPayload(
117-
"checkout_finished_loading", timeToLoad, histogram, mapOf()
119+
name= "checkout_finished_loading",
120+
value= timeToLoad,
121+
type = histogram,
122+
tags = mapOf("preloading" to isPreload.toString()),
118123
)
119124
))
120125
getEventProcessor().onCheckoutViewLoadComplete()

lib/src/test/java/com/shopify/checkoutsheetkit/CheckoutWebViewTest.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import org.robolectric.Robolectric
4343
import org.robolectric.RobolectricTestRunner
4444
import org.robolectric.Shadows.shadowOf
4545
import org.robolectric.shadows.ShadowLooper
46+
import java.util.regex.Pattern
4647

4748
@RunWith(RobolectricTestRunner::class)
4849
class CheckoutWebViewTest {
@@ -140,6 +141,40 @@ class CheckoutWebViewTest {
140141
}
141142
}
142143

144+
@Test
145+
fun `records checkout_finished_loading instrumentation event on page finished - preloading`() {
146+
withPreloadingEnabled {
147+
val isPreload = true
148+
val view = CheckoutWebView.cacheableCheckoutView(URL, activity, isPreload)
149+
val shadow = shadowOf(view)
150+
shadow.webViewClient.onPageFinished(view, URL)
151+
152+
val regex = Pattern.compile(
153+
@Suppress("MaxLineLength")
154+
""".*\.dispatchMessage\('instrumentation', \{"detail":\{"name":"checkout_finished_loading","value":\d*,"type":"histogram","tags":\{"preloading":"true"}}}\).*""",
155+
Pattern.DOTALL
156+
)
157+
assertThat(shadow.lastEvaluatedJavascript).matches(regex)
158+
}
159+
}
160+
161+
@Test
162+
fun `records checkout_finished_loading instrumentation event on page finished - presenting`() {
163+
withPreloadingEnabled {
164+
val isPreload = false
165+
val view = CheckoutWebView.cacheableCheckoutView(URL, activity, isPreload)
166+
val shadow = shadowOf(view)
167+
shadow.webViewClient.onPageFinished(view, URL)
168+
169+
val regex = Pattern.compile(
170+
@Suppress("MaxLineLength")
171+
""".*\.dispatchMessage\('instrumentation', \{"detail":\{"name":"checkout_finished_loading","value":\d*,"type":"histogram","tags":\{"preloading":"false"}}}\).*""",
172+
Pattern.DOTALL
173+
)
174+
assertThat(shadow.lastEvaluatedJavascript).matches(regex)
175+
}
176+
}
177+
143178
@Test
144179
fun `does not send prefetch header for preloads`() {
145180
val isPreload = false

0 commit comments

Comments
 (0)