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 opt_out_useragent_filter and $browser_type #949

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
39 changes: 39 additions & 0 deletions src/__tests__/posthog-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { autocapture } from '../autocapture'
import { truth } from './helpers/truth'
import { _info } from '../utils/event-utils'
import { document, window } from '../utils/globals'
import * as globals from '../utils/globals'

jest.mock('../gdpr-utils', () => ({
...jest.requireActual('../gdpr-utils'),
Expand Down Expand Up @@ -127,6 +128,44 @@ describe('posthog core', () => {
expect(console.error).toHaveBeenCalledWith('[PostHog.js]', 'No event name provided to posthog.capture')
})

it('respects opt_out_useragent_filter (default: false)', () => {
const originalUseragent = globals.userAgent
// eslint-disable-next-line no-import-assign
globals['userAgent'] =
'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Chrome/W.X.Y.Z Safari/537.36'

const hook = jest.fn()
given.lib._addCaptureHook(hook)
given.subject()
expect(hook).not.toHaveBeenCalledWith('$event')

// eslint-disable-next-line no-import-assign
globals['userAgent'] = originalUseragent
})

it('respects opt_out_useragent_filter', () => {
const originalUseragent = globals.userAgent

given('config', () => ({
opt_out_useragent_filter: true,
property_blacklist: [],
_onCapture: jest.fn(),
}))

// eslint-disable-next-line no-import-assign
globals['userAgent'] =
'Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html) Chrome/W.X.Y.Z Safari/537.36'

const hook = jest.fn()
given.lib._addCaptureHook(hook)
const event = given.subject()
expect(hook).toHaveBeenCalledWith('$event')
expect(event.properties['$browser_type']).toEqual('bot')

// eslint-disable-next-line no-import-assign
globals['userAgent'] = originalUseragent
})

it('truncates long properties', () => {
given('config', () => ({
properties_string_max_length: 1000,
Expand Down
16 changes: 15 additions & 1 deletion src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const defaultConfig = (): PostHogConfig => ({
ip: true,
opt_out_capturing_by_default: false,
opt_out_persistence_by_default: false,
opt_out_useragent_filter: false,
opt_out_capturing_persistence_type: 'localStorage',
opt_out_capturing_cookie_prefix: null,
opt_in_site_apps: false,
Expand Down Expand Up @@ -866,7 +867,11 @@ export class PostHog {
return
}

if (userAgent && _isBlockedUA(userAgent, this.config.custom_blocked_useragents)) {
if (
userAgent &&
!this.config.opt_out_useragent_filter &&
_isBlockedUA(userAgent, this.config.custom_blocked_useragents)
) {
return
}

Expand Down Expand Up @@ -989,6 +994,12 @@ export class PostHog {
properties['$duration'] = parseFloat((duration_in_ms / 1000).toFixed(3))
}

if (userAgent && this.config.opt_out_useragent_filter) {
properties['$browser_type'] = _isBlockedUA(userAgent, this.config.custom_blocked_useragents)
? 'bot'
: 'browser'
}

// note: extend writes to the first object, so lets make sure we
// don't write to the persistence properties object and info
// properties object by passing in a new object
Expand Down Expand Up @@ -1631,6 +1642,9 @@ export class PostHog {
* // opt users out of browser data storage by this PostHog instance by default
* opt_out_persistence_by_default: false
*
* // opt out of user agent filtering such as googlebot or other bots
* opt_out_useragent_filter: false
*
* // persistence mechanism used by opt-in/opt-out methods - cookie
* // or localStorage - falls back to cookie if localStorage is unavailable
* opt_out_capturing_persistence_type: 'localStorage'
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ export interface PostHogConfig {
ip: boolean
opt_out_capturing_by_default: boolean
opt_out_persistence_by_default: boolean
/** Opt out of user agent filtering such as googlebot or other bots. Defaults to `false` */
opt_out_useragent_filter: boolean
opt_out_capturing_persistence_type: 'localStorage' | 'cookie'
opt_out_capturing_cookie_prefix: string | null
opt_in_site_apps: boolean
Expand Down
6 changes: 3 additions & 3 deletions src/utils/blocked-uas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export const DEFAULT_BLOCKED_UA_STRS = [
'storebot-google',
]

// _.isBlockedUA()
// This is to block various web spiders from executing our JS and
// sending false capturing data
/**
* Block various web spiders from executing our JS and sending false capturing data
*/
export const _isBlockedUA = function (ua: string, customBlockedUserAgents: string[]): boolean {
if (!ua) {
return false
Expand Down
Loading