Skip to content

Commit ae60ef4

Browse files
authored
fix: capture customLabelProp if set (#302)
1 parent b853833 commit ae60ef4

File tree

5 files changed

+42
-13
lines changed

5 files changed

+42
-13
lines changed

examples/example-expo-latest/App.tsx

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useState } from 'react'
22
import { StatusBar } from 'expo-status-bar'
33
import { StyleSheet, Text, View } from 'react-native'
4-
import PostHog from 'posthog-react-native'
4+
import PostHog, { PostHogProvider } from 'posthog-react-native'
55

66
export const posthog = new PostHog('phc_QFbR1y41s5sxnNTZoyKG2NJo2RlsCIWkUfdpawgb40D', {
77
host: 'https://us.i.posthog.com',
@@ -10,6 +10,23 @@ export const posthog = new PostHog('phc_QFbR1y41s5sxnNTZoyKG2NJo2RlsCIWkUfdpawgb
1010
})
1111
posthog.debug(true)
1212

13+
export const SharedPostHogProvider = (props: any) => {
14+
return (
15+
<PostHogProvider
16+
client={posthog}
17+
autocapture={{
18+
captureLifecycleEvents: true,
19+
captureScreens: true,
20+
captureTouches: true,
21+
customLabelProp: 'ph-my-label',
22+
}}
23+
debug
24+
>
25+
{props.children}
26+
</PostHogProvider>
27+
)
28+
}
29+
1330
export default function App() {
1431
const [buttonText, setButtonText] = useState('Open up App.js to start working on your app!')
1532

@@ -19,10 +36,14 @@ export default function App() {
1936
}
2037

2138
return (
22-
<View style={styles.container}>
23-
<Text onPress={handleClick}>{buttonText}</Text>
24-
<StatusBar style="auto" />
25-
</View>
39+
<SharedPostHogProvider>
40+
<View style={styles.container}>
41+
<Text ph-my-label="special-text-changed" onPress={handleClick}>
42+
{buttonText}
43+
</Text>
44+
<StatusBar style="auto" />
45+
</View>
46+
</SharedPostHogProvider>
2647
)
2748
}
2849

posthog-react-native/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Next
22

3+
# 3.3.10 - 2024-11-04
4+
5+
1. fix: capture customLabelProp if set
6+
37
# 3.3.9 - 2024-10-26
48

59
1. fix: rollback module to ESNext

posthog-react-native/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "posthog-react-native",
3-
"version": "3.3.9",
3+
"version": "3.3.10",
44
"main": "lib/posthog-react-native/index.js",
55
"files": [
66
"lib/"

posthog-react-native/src/PostHogProvider.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { useNavigationTracker } from './hooks/useNavigationTracker'
66
import { useLifecycleTracker } from './hooks/useLifecycleTracker'
77
import { PostHogContext } from './PostHogContext'
88
import { PostHogAutocaptureOptions } from './types'
9+
import { defaultPostHogLabelProp } from './autocapture'
910

1011
export interface PostHogProviderProps {
1112
children: React.ReactNode
@@ -70,6 +71,7 @@ export const PostHogProvider = ({
7071
const captureScreens = !captureNone && posthog && (captureAll || (autocaptureOptions?.captureScreens ?? true)) // Default to true if not set
7172
const captureLifecycle =
7273
!captureNone && posthog && (captureAll || (autocaptureOptions?.captureLifecycleEvents ?? true)) // Default to true if not set
74+
const phLabelProp = autocaptureOptions?.customLabelProp || defaultPostHogLabelProp
7375

7476
useEffect(() => {
7577
posthog.debug(debug)
@@ -91,7 +93,7 @@ export const PostHogProvider = ({
9193

9294
return (
9395
<View
94-
ph-label="PostHogProvider"
96+
{...{ [phLabelProp]: 'PostHogProvider' }} // Dynamically setting customLabelProp (default: ph-label)
9597
style={style || { flex: 1 }}
9698
onTouchEndCapture={captureTouches ? (e) => onTouch('end', e) : undefined}
9799
>

posthog-react-native/src/autocapture.tsx

+8-6
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ const sanitiseLabel = (label: string): string => {
3939
return label.replace(/[^a-z0-9]+/gi, '-')
4040
}
4141

42+
export const defaultPostHogLabelProp = 'ph-label'
43+
4244
export const autocaptureFromTouchEvent = (e: any, posthog: PostHog, options: PostHogAutocaptureOptions = {}): void => {
4345
const {
4446
noCaptureProp = 'ph-no-capture',
45-
customLabelProp = 'ph-label',
47+
customLabelProp = defaultPostHogLabelProp,
4648
maxElementsCaptured = 20,
4749
ignoreLabels = [],
48-
propsToCapture = ['style', 'testID', 'accessibilityLabel', 'ph-label', 'children'],
4950
} = options
51+
const propsToCapture = ['style', 'testID', 'accessibilityLabel', customLabelProp, 'children']
5052

5153
if (!e._targetInst) {
5254
return
@@ -105,16 +107,16 @@ export const autocaptureFromTouchEvent = (e: any, posthog: PostHog, options: Pos
105107
}
106108

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

114116
for (let i = elements.length - 1; i >= 0; i--) {
115117
const element = elements[i]
116118
if (element[elAttrLabelKey]) {
117-
// this element had a ph-label set, promote it to the lastLabel
119+
// this element had a customLabelProp (default: ph-label) set, promote it to the lastLabel
118120
lastLabel = element[elAttrLabelKey]
119121
}
120122

0 commit comments

Comments
 (0)