Skip to content

Commit

Permalink
fix: Don't track heatmaps of toolbar (#1152)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjackwhite authored Apr 24, 2024
1 parent e49ed4a commit 221f4b1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
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)
})
})
13 changes: 13 additions & 0 deletions src/heatmaps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ function elementOrParentPositionMatches(el: Element, matches: string[], breakOnE
return false
}

const TOOLBAR_ID = '__POSTHOG_TOOLBAR__'

function elementInToolbar(el: Element): boolean {
// NOTE: .closest is not supported in IE11 hence the operator check
return el.id === TOOLBAR_ID || !!el.closest?.('#__POSTHOG_TOOLBAR__')
}

export class Heatmaps {
instance: PostHog
rageclicks = new RageClick()
Expand Down Expand Up @@ -91,6 +98,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 +116,9 @@ export class Heatmaps {
}

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

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

0 comments on commit 221f4b1

Please sign in to comment.