-
Notifications
You must be signed in to change notification settings - Fork 61
RUMS-5681: Add support for Android heatmaps #1341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
packages/core/android/src/main/kotlin/com/datadog/reactnative/HeatmapActionHandler.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| package com.datadog.reactnative | ||
|
|
||
| import android.os.Handler | ||
| import android.os.Looper | ||
| import com.datadog.android.api.InternalLogger | ||
| import com.datadog.android.rum.RumActionType | ||
| import com.datadog.android.rum._RumInternalProxy | ||
| import com.facebook.react.bridge.ReadableMap | ||
|
|
||
| /** | ||
| * Decides whether an `addAction` call is eligible for heatmap tracking and, if so, resolves and | ||
| * attaches the heatmap data via [HeatmapTouchResolver]. Split into two steps so the caller can | ||
| * resolve its own promise between them, before the heatmap work dispatches. | ||
| */ | ||
| class HeatmapActionHandler internal constructor( | ||
| private val heatmapTouchResolver: HeatmapTouchResolver = HeatmapTouchResolver(), | ||
| private val mainThreadExecutor: (() -> Unit) -> Unit = defaultMainThreadExecutor() | ||
| ) { | ||
|
|
||
| internal data class EligibleAction( | ||
| val internalProxy: _RumInternalProxy, | ||
| val viewUrl: String, | ||
| val reactTag: Int, | ||
| val positionX: Long, | ||
| val positionY: Long | ||
| ) | ||
|
|
||
| internal fun resolveEligibility( | ||
| datadog: DatadogWrapper, | ||
| type: String, | ||
| name: String, | ||
| touch: ReadableMap? | ||
| ): EligibleAction? { | ||
| if (!heatmapsEnabled || touch == null || !type.equals("tap", ignoreCase = true)) { | ||
| return null | ||
| } | ||
|
|
||
| val internalProxy = datadog.getRumMonitor()._getInternal() | ||
| // Read now — the view can transition asynchronously right after a tap. | ||
| val viewUrl = internalProxy?.getCurrentViewUrl() | ||
| val touchFields = touch.toTouchFieldsOrNull(name) | ||
|
|
||
| return if (internalProxy != null && viewUrl != null && touchFields != null) { | ||
| val (reactTag, positionX, positionY) = touchFields | ||
| EligibleAction(internalProxy, viewUrl, reactTag, positionX, positionY) | ||
| } else { | ||
| null | ||
| } | ||
| } | ||
|
|
||
| internal fun attachHeatmapData( | ||
| eligibleAction: EligibleAction, | ||
| name: String, | ||
| attributes: Map<String, Any?>, | ||
| fallback: () -> Unit | ||
| ) { | ||
| mainThreadExecutor { | ||
| val heatmapData = heatmapTouchResolver.resolveHeatmapActionData( | ||
| eligibleAction.reactTag, | ||
| eligibleAction.positionX, | ||
| eligibleAction.positionY, | ||
| eligibleAction.viewUrl | ||
| ) | ||
| if (heatmapData != null) { | ||
| eligibleAction.internalProxy.addActionWithHeatmap( | ||
| type = RumActionType.TAP, | ||
| name = name, | ||
| crossPlatformHeatmapActionData = heatmapData, | ||
| attributes = attributes | ||
| ) | ||
| } else { | ||
| fallback() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Missing fields are not warned on: `actionContext` is an optional addAction parameter, and | ||
| // a caller intentionally tracking a TAP action without real touch coordinates is valid usage. | ||
| private fun ReadableMap.toTouchFieldsOrNull(actionName: String): Triple<Int, Long, Long>? { | ||
| if (!hasKey("reactTag") || !hasKey("x") || !hasKey("y")) return null | ||
| return runCatching { | ||
| Triple(getInt("reactTag"), getDouble("x").toLong(), getDouble("y").toLong()) | ||
| }.onFailure { | ||
| InternalLogger.UNBOUND.log( | ||
| InternalLogger.Level.WARN, | ||
| InternalLogger.Target.USER, | ||
| { | ||
| "addAction(\"$actionName\"): heatmap tracking requires actionContext's " + | ||
| "nativeEvent.target/locationX/locationY to be numbers, as produced by a " + | ||
| "standard onPress GestureResponderEvent. The value passed for this " + | ||
| "action didn't match that shape, so heatmap tracking was skipped for it " + | ||
| "— this is expected if actionContext came from a non-standard event " + | ||
| "source (e.g. a different gesture library)." | ||
| }, | ||
| throwable = it, | ||
| onlyOnce = true | ||
| ) | ||
| }.getOrNull() | ||
| } | ||
|
|
||
| @Suppress("UndocumentedPublicClass") | ||
| companion object { | ||
| private fun defaultMainThreadExecutor(): ((() -> Unit) -> Unit) { | ||
| val handler by lazy { Handler(Looper.getMainLooper()) } | ||
| return { action -> handler.post(action) } | ||
| } | ||
|
|
||
| /** | ||
| * Whether heatmap data should be attached to TAP actions. Set by | ||
| * [com.datadog.reactnative.sessionreplay.DdSessionReplayImplementation.enable]. | ||
| */ | ||
| @Volatile | ||
| var heatmapsEnabled: Boolean = false | ||
| } | ||
| } | ||
108 changes: 108 additions & 0 deletions
108
packages/core/android/src/main/kotlin/com/datadog/reactnative/HeatmapTouchResolver.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| /* | ||
| * Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
| * This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| * Copyright 2016-Present Datadog, Inc. | ||
| */ | ||
|
|
||
| package com.datadog.reactnative | ||
|
|
||
| import android.content.res.Resources | ||
| import android.view.View | ||
| import android.view.ViewGroup | ||
| import com.datadog.android.heatmaps.CrossPlatformHeatmapActionData | ||
|
|
||
| /** | ||
| * Resolves a React Native touch into [CrossPlatformHeatmapActionData] by walking the view | ||
| * hierarchy to the nearest clickable-and-visible ancestor — matching Session Replay's own | ||
| * `HeatmapIdentifierResolver` rule — and building its element path using the same convention, so | ||
| * the resulting hash matches the `permanentId` SR assigned to that view's wireframe. | ||
| */ | ||
| internal class HeatmapTouchResolver( | ||
| private val viewResolver: (Int) -> View? = { null }, | ||
| private val telemetry: DdTelemetry = DdTelemetry() | ||
| ) { | ||
|
|
||
| /** Returns null if [reactTag] doesn't resolve to a valid tap target. */ | ||
| fun resolveHeatmapActionData( | ||
| reactTag: Int, | ||
| positionX: Long, | ||
| positionY: Long, | ||
| viewUrl: String | ||
| ): CrossPlatformHeatmapActionData? = runCatching { | ||
| val view = viewResolver(reactTag)?.let { clickableVisibleAncestorOf(it) } | ||
| val elementPath = view?.let { elementPathFromRootTo(it) } | ||
|
|
||
| if (view != null && !elementPath.isNullOrEmpty()) { | ||
| val density = view.resources?.displayMetrics?.density ?: 1f | ||
| val targetWidth = (view.width / density).toLong().takeIf { it > 0 } | ||
| val targetHeight = (view.height / density).toLong().takeIf { it > 0 } | ||
|
|
||
| CrossPlatformHeatmapActionData( | ||
| elementPath = elementPath, | ||
| viewUrl = viewUrl, | ||
| positionX = positionX, | ||
| positionY = positionY, | ||
| targetWidth = targetWidth, | ||
| targetHeight = targetHeight | ||
| ) | ||
| } else { | ||
| null | ||
| } | ||
| }.onFailure { | ||
| telemetry.telemetryError("Failed to resolve heatmap action data", it) | ||
| }.getOrNull() | ||
|
|
||
| // region Private helpers | ||
|
|
||
| private fun clickableVisibleAncestorOf(view: View): View? { | ||
| var current: View? = view | ||
| while (current != null) { | ||
| if (current.isClickable && current.visibility == View.VISIBLE) { | ||
| return current | ||
| } | ||
| current = current.parent as? View | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| private fun elementPathFromRootTo(view: View): List<String> { | ||
| val path = mutableListOf<String>() | ||
| var current: View? = view | ||
| while (current != null) { | ||
| val parent = current.parent as? ViewGroup | ||
| val typeIndex = if (parent != null) computeTypeIndex(current, parent) else 0 | ||
| path.add(pathComponentFor(current, typeIndex)) | ||
| current = parent | ||
| } | ||
| path.reverse() | ||
| return path | ||
| } | ||
|
|
||
| private fun computeTypeIndex(view: View, parent: ViewGroup): Int { | ||
| val cls = view.javaClass | ||
| var index = 0 | ||
| for (i in 0 until parent.childCount) { | ||
| val child = parent.getChildAt(i) ?: continue | ||
| if (child === view) break | ||
| if (child.javaClass === cls) index++ | ||
| } | ||
| return index | ||
| } | ||
|
|
||
| private fun pathComponentFor(view: View, typeIndex: Int): String { | ||
| val viewId = view.id | ||
| if (viewId != View.NO_ID) { | ||
| try { | ||
| @Suppress("UnsafeThirdPartyFunctionCall") | ||
| val name = view.resources?.getResourceName(viewId) | ||
| if (!name.isNullOrEmpty()) { | ||
| return "$name#$typeIndex" | ||
| } | ||
| } catch (_: Resources.NotFoundException) { | ||
| } | ||
| } | ||
| return "cls:${view.javaClass.name}#$typeIndex" | ||
| } | ||
|
|
||
| // endregion | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.