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: capture customLabelProp if set #302

Merged
merged 5 commits into from
Nov 5, 2024
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,5 +1,9 @@
# Next

# 3.3.10 - 2024-11-04

1. fix: capture customLabelProp if set

# 3.3.9 - 2024-10-26

1. fix: rollback module to ESNext
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": "3.3.9",
"version": "3.3.10",
"main": "lib/posthog-react-native/index.js",
"files": [
"lib/"
Expand Down
4 changes: 3 additions & 1 deletion posthog-react-native/src/PostHogProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useNavigationTracker } from './hooks/useNavigationTracker'
import { useLifecycleTracker } from './hooks/useLifecycleTracker'
import { PostHogContext } from './PostHogContext'
import { PostHogAutocaptureOptions } from './types'
import { defaultPostHogLabelProp } from './autocapture'

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

useEffect(() => {
posthog.debug(debug)
Expand All @@ -91,7 +93,7 @@ export const PostHogProvider = ({

return (
<View
ph-label="PostHogProvider"
{...{ [phLabelProp]: 'PostHogProvider' }} // Dynamically setting customLabelProp (default: ph-label)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is chatgpt code :D

style={style || { flex: 1 }}
onTouchEndCapture={captureTouches ? (e) => onTouch('end', e) : undefined}
>
Expand Down
14 changes: 8 additions & 6 deletions posthog-react-native/src/autocapture.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ const sanitiseLabel = (label: string): string => {
return label.replace(/[^a-z0-9]+/gi, '-')
}

export const defaultPostHogLabelProp = 'ph-label'

export const autocaptureFromTouchEvent = (e: any, posthog: PostHog, options: PostHogAutocaptureOptions = {}): void => {
const {
noCaptureProp = 'ph-no-capture',
customLabelProp = 'ph-label',
customLabelProp = defaultPostHogLabelProp,
maxElementsCaptured = 20,
ignoreLabels = [],
propsToCapture = ['style', 'testID', 'accessibilityLabel', 'ph-label', 'children'],
} = options
const propsToCapture = ['style', 'testID', 'accessibilityLabel', customLabelProp, 'children']

if (!e._targetInst) {
return
Expand Down Expand Up @@ -105,16 +107,16 @@ 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
// The element that was tapped, may be a child (or grandchild of an element with a customLabelProp (default: ph-label))
// In this case, the current labels applied obscure the customLabelProp (default: ph-label)
// To correct this, loop over the elements in reverse, and promote the customLabelProp (default: 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
// this element had a customLabelProp (default: ph-label) set, promote it to the lastLabel
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just iterate over normally here with early exit instead of reverse?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the reason why it is reversed is in the comment above.
I'd not change what's working already as well, the change is about using the wrong label name only.

lastLabel = element[elAttrLabelKey]
}

Expand Down
Loading