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: Don't track heatmaps of toolbar #1152

Merged
merged 3 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 25 additions & 8 deletions src/__tests__/heatmaps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,11 @@ describe('heatmaps', () => {
let posthog: PostHog
let onCapture = jest.fn()

const mockClickEvent = {
target: document.body,
clientX: 10,
clientY: 20,
} as unknown as MouseEvent

const createMockMouseEvent = (props: Partial<MouseEvent> = {}) =>
({
target: document.body,
clientX: 10,
clientY: 10,
clientY: 20,
...props,
} as unknown as MouseEvent)

Expand All @@ -27,7 +21,7 @@ describe('heatmaps', () => {
})

it('should include generated heatmap data', async () => {
posthog.heatmaps?.['_onClick']?.(mockClickEvent as MouseEvent)
posthog.heatmaps?.['_onClick']?.(createMockMouseEvent())
posthog.capture('test event')

expect(onCapture).toBeCalledTimes(1)
Expand Down Expand Up @@ -88,4 +82,27 @@ describe('heatmaps', () => {
expect(onCapture.mock.lastCall).toMatchObject(['$snapshot', {}])
expect(onCapture.mock.lastCall[1].properties).not.toHaveProperty('$heatmap_data')
})

it('should ignore clicks if they come from the toolbar', async () => {
posthog.heatmaps?.['_onClick']?.(
createMockMouseEvent({
target: { id: '__POSTHOG_TOOLBAR__' } as Element,
})
)
expect(posthog.heatmaps?.['buffer']).toEqual(undefined)

posthog.heatmaps?.['_onClick']?.(
createMockMouseEvent({
target: { closest: () => ({}) } as unknown as Element,
})
)
expect(posthog.heatmaps?.['buffer']).toEqual(undefined)

posthog.heatmaps?.['_onClick']?.(
createMockMouseEvent({
target: document.body,
})
)
expect(posthog.heatmaps?.['buffer']).not.toEqual(undefined)
})
})
12 changes: 12 additions & 0 deletions src/heatmaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ function elementOrParentPositionMatches(el: Element, matches: string[], breakOnE
return false
}

const TOOLBAR_ID = '__POSTHOG_TOOLBAR__'

function elementInToolbar(el: Element): boolean {
return el.id === TOOLBAR_ID || !!el.closest('#__POSTHOG_TOOLBAR__')
}

export class Heatmaps {
instance: PostHog
rageclicks = new RageClick()
Expand Down Expand Up @@ -91,6 +97,9 @@ export class Heatmaps {
}

private _onClick(e: MouseEvent): void {
if (elementInToolbar(e.target as Element)) {
return
}
const properties = this._getProperties(e, 'click')

if (this.rageclicks?.isRageClick(e.clientX, e.clientY, new Date().getTime())) {
Expand All @@ -106,6 +115,9 @@ export class Heatmaps {
}

private _onMouseMove(e: Event): void {
if (elementInToolbar(e.target as Element)) {
return
}
clearTimeout(this._mouseMoveTimeout)

this._mouseMoveTimeout = setTimeout(() => {
Expand Down
Loading