Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldambra committed Nov 8, 2024
1 parent 2a209d2 commit 841832b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/__tests__/heatmaps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,10 @@ describe('heatmaps', () => {
}
)
})

it('starts dead clicks autocapture with the correct config', () => {
const heatmapsDeadClicksInstance = posthog.heatmaps['deadClicksCapture']
expect(heatmapsDeadClicksInstance.isEnabled(heatmapsDeadClicksInstance)).toBe(true)
expect(heatmapsDeadClicksInstance.onCapture).toBe(posthog.heatmaps['_onDeadClick'])
})
})
17 changes: 12 additions & 5 deletions src/extensions/dead-clicks-autocapture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DEAD_CLICKS_ENABLED_SERVER_SIDE } from '../constants'
import { isBoolean, isObject } from '../utils/type-utils'
import { assignableWindow, document, LazyLoadedDeadClicksAutocaptureInterface } from '../utils/globals'
import { logger } from '../utils/logger'
import { DecideResponse } from '../types'
import { DeadClicksAutoCaptureConfig, DecideResponse } from '../types'

const LOGGER_PREFIX = '[Dead Clicks]'

Expand All @@ -23,7 +23,11 @@ export class DeadClicksAutocapture {

private _lazyLoadedDeadClicksAutocapture: LazyLoadedDeadClicksAutocaptureInterface | undefined

constructor(readonly instance: PostHog, readonly isEnabled: (dca: DeadClicksAutocapture) => boolean) {
constructor(
readonly instance: PostHog,
readonly isEnabled: (dca: DeadClicksAutocapture) => boolean,
readonly onCapture: DeadClicksAutoCaptureConfig['__onCapture']
) {
this.startIfEnabled()
}

Expand Down Expand Up @@ -72,11 +76,14 @@ export class DeadClicksAutocapture {
!this._lazyLoadedDeadClicksAutocapture &&
assignableWindow.__PosthogExtensions__?.initDeadClicksAutocapture
) {
const config = isObject(this.instance.config.capture_dead_clicks)
? this.instance.config.capture_dead_clicks
: {}
config.__onCapture = this.onCapture

this._lazyLoadedDeadClicksAutocapture = assignableWindow.__PosthogExtensions__.initDeadClicksAutocapture(
this.instance,
isObject(this.instance.config.capture_dead_clicks)
? this.instance.config.capture_dead_clicks
: undefined
config
)
this._lazyLoadedDeadClicksAutocapture.start(document)
logger.info(`${LOGGER_PREFIX} starting...`)
Expand Down
18 changes: 11 additions & 7 deletions src/heatmaps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { includes, registerEvent } from './utils'
import RageClick from './extensions/rageclick'
import { DecideResponse, Properties } from './types'
import { DeadClickCandidate, DecideResponse, Properties } from './types'
import { PostHog } from './posthog-core'

import { document, window } from './utils/globals'
Expand Down Expand Up @@ -118,6 +118,10 @@ export class Heatmaps {
return buffer
}

private _onDeadClick(click: DeadClickCandidate): void {
this._onClick(click.originalEvent, 'deadclick')
}

private _setupListeners(): void {
if (!window || !document) {
return
Expand All @@ -126,12 +130,12 @@ export class Heatmaps {
registerEvent(document, 'click', (e) => this._onClick((e || window?.event) as MouseEvent), false, true)
registerEvent(document, 'mousemove', (e) => this._onMouseMove((e || window?.event) as MouseEvent), false, true)

this.deadClicksCapture = new DeadClicksAutocapture(this.instance, isDeadClicksEnabledForHeatmaps)
this.deadClicksCapture.startIfEnabled({
onCapture: (click) => {
this._onClick(click.originalEvent, 'deadclick')
},
})
this.deadClicksCapture = new DeadClicksAutocapture(
this.instance,
isDeadClicksEnabledForHeatmaps,
this._onDeadClick.bind(this)
)
this.deadClicksCapture.startIfEnabled()

this._initialized = true
}
Expand Down

0 comments on commit 841832b

Please sign in to comment.