Skip to content

Commit

Permalink
fix: update the logic of tag_name to prefer a ph-label from a parent …
Browse files Browse the repository at this point in the history
…over a display name or similar
  • Loading branch information
Steffan Levet committed Jan 11, 2024
1 parent 20ea30d commit fe2c8bc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
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

0 comments on commit fe2c8bc

Please sign in to comment.