Skip to content
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

fix: update the logic of tag_name to prefer a ph-label from a parent … #154

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions posthog-react-native/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.10.1 - 2024-01-12
marandaneto marked this conversation as resolved.
Show resolved Hide resolved

1. The `tag_name` property of auto-captured events now uses the nearest `ph-label` from parent elements, if present.

# 2.10.0 - 2024-01-08

1. `$device_type` is now set to `Mobile`, `Desktop`, or `Web` for all events
Expand Down
2 changes: 1 addition & 1 deletion posthog-react-native/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "posthog-react-native",
"version": "2.10.0",
"version": "2.10.1",
"main": "lib/posthog-react-native/index.js",
"files": [
"lib/"
Expand Down
18 changes: 18 additions & 0 deletions posthog-react-native/src/autocapture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,24 @@ export const autocaptureFromTouchEvent = (e: any, posthog: PostHog, options: Pos
}

if (elements.length) {
// The element that was tapped, may be a child (or grandchild of an element with a ph-label)
// In this case, the current labels applied obscure the ph-label
// To correct this, loop over the elements in reverse, and promote the ph-label
const elAttrLabelKey = `attr__${customLabelProp}`
let lastLabel: string | undefined = undefined

for (let i = elements.length - 1; i >= 0; i--) {
const element = elements[i]
if (element[elAttrLabelKey]) {
// this element had a ph-label set, promote it to the lastLabel
lastLabel = element[elAttrLabelKey]
}

// if lastLabel is set, update this elements tag_name
if (lastLabel) {
element['tag_name'] = lastLabel
}
}
posthog.autocapture('touch', elements, {
$touch_x: e.nativeEvent.pageX,
$touch_y: e.nativeEvent.pageY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ Array [
},
Object {
"$el_text": "I have the property \\"ph-label\\" which means touching me will be autocaptured with a specific label",
"attr__ph-label": "special-text",
"attr__testID": "example-ph-label",
"tag_name": "special-text",
"tag_name": "higher-level-element",
},
Object {
"attr__style": "backgroundColor:rgba(0,0,0,.1);padding:15;borderRadius:10;marginBottom:10",
"tag_name": "View",
"tag_name": "higher-level-element",
},
Object {
"tag_name": "View",
"attr__ph-label": "higher-level-element",
"tag_name": "higher-level-element",
},
Object {
"attr__style": "flex:1",
Expand Down
5 changes: 3 additions & 2 deletions posthog-react-native/test/data/autocapture-event.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"elementType": { "displayName": "Text" },
"memoizedProps": {
"testID": "example-ph-label",
"ph-label": "special-text",
"children": "I have the property \"ph-label\" which means touching me will be autocaptured with a specific label"
},
"return": {
Expand All @@ -40,7 +39,9 @@
"memoizedProps": { "value": false },
"return": {
"elementType": { "displayName": "View" },
"memoizedProps": {},
"memoizedProps": {
"ph-label": "higher-level-element"
},
"return": {
"elementType": {},
"memoizedProps": {},
Expand Down