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

feat: add url_ignorelist for autocapture config #1302

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Changes from 5 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
25 changes: 23 additions & 2 deletions src/__tests__/autocapture.test.ts
Original file line number Diff line number Diff line change
@@ -1104,12 +1104,33 @@ describe('Autocapture system', () => {
url_allowlist: ['https://posthog.com/test/*'],
}

window!.location = new URL('https://posthog.com/test/captured') as unknown as Location
window!.location = new URL('https://posthog.com/test/matching') as unknown as Location

expect(shouldCaptureDomEvent(button, e, autocapture_config)).toBe(true)

window!.location = new URL('https://posthog.com/docs/not-captured') as unknown as Location
window!.location = new URL('https://posthog.com/docs/not-matching') as unknown as Location
expect(shouldCaptureDomEvent(button, e, autocapture_config)).toBe(false)
})

it('not capture urls which match the url regex ignorelist', () => {
const main_el = document.createElement('some-element')
const button = document.createElement('a')
button.innerHTML = 'bla'
main_el.appendChild(button)
const e = makeMouseEvent({
target: main_el,
composedPath: () => [button, main_el],
})
const autocapture_config = {
url_ignorelist: ['https://posthog.com/test/*'],
}

window!.location = new URL('https://posthog.com/test/matching') as unknown as Location

expect(shouldCaptureDomEvent(button, e, autocapture_config)).toBe(false)

window!.location = new URL('https://posthog.com/docs/not-matching') as unknown as Location
expect(shouldCaptureDomEvent(button, e, autocapture_config)).toBe(true)
})

it('an empty url regex allowlist does not match any url', () => {
17 changes: 14 additions & 3 deletions src/autocapture-utils.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,11 @@ export function splitClassString(s: string): string[] {
return s ? trim(s).split(/\s+/) : []
}

function checkForURLMatches(urlsList: (string | RegExp)[]): boolean {
const url = window?.location.href
return !!(url && urlsList && urlsList.some((regex) => url.match(regex)))
}

/*
* Get the className of an element, accounting for edge cases where element.className is an object
*
@@ -201,9 +206,15 @@ export function shouldCaptureDomEvent(
}

if (autocaptureConfig?.url_allowlist) {
const url = window.location.href
const allowlist = autocaptureConfig.url_allowlist
if (allowlist && !allowlist.some((regex) => url.match(regex))) {
// if the current URL is not in the allow list, don't capture
if (!checkForURLMatches(autocaptureConfig.url_allowlist)) {
return false
}
}

if (autocaptureConfig?.url_ignorelist) {
// if the current URL is in the ignore list, don't capture
if (checkForURLMatches(autocaptureConfig.url_ignorelist)) {
return false
}
}
1 change: 1 addition & 0 deletions src/autocapture.ts
Original file line number Diff line number Diff line change
@@ -53,6 +53,7 @@ export class Autocapture {
const config = isObject(this.instance.config.autocapture) ? this.instance.config.autocapture : {}
// precompile the regex
config.url_allowlist = config.url_allowlist?.map((url) => new RegExp(url))
config.url_ignorelist = config.url_ignorelist?.map((url) => new RegExp(url))
return config
}

8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -25,9 +25,17 @@ export interface AutocaptureConfig {
/**
* List of URLs to allow autocapture on, can be strings to match
* or regexes e.g. ['https://example.com', 'test.com/.*']
* this is useful when you want to autocapture on specific pages only
*/
url_allowlist?: (string | RegExp)[]

/**
* List of URLs to not allow autocapture on, can be strings to match
* or regexes e.g. ['https://example.com', 'test.com/.*']
* this is useful when you want to autocapture on most pages but not some specific ones
*/
url_ignorelist?: (string | RegExp)[]

/**
* List of DOM events to allow autocapture on e.g. ['click', 'change', 'submit']
*/