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

chore: Move blocked UAs to own file #905

Merged
merged 1 commit into from
Nov 20, 2023
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
10 changes: 2 additions & 8 deletions src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,10 @@
* currently not supported in the browser lib).
*/

import {
_copyAndTruncateStrings,
_isBlockedUA,
DEFAULT_BLOCKED_UA_STRS,
loadScript,
isCrossDomainCookie,
_base64Encode,
} from '../utils'
import { _copyAndTruncateStrings, loadScript, isCrossDomainCookie, _base64Encode } from '../utils'
import { _info } from '../utils/event-utils'
import { document } from '../utils/globals'
import { _isBlockedUA, DEFAULT_BLOCKED_UA_STRS } from '../utils/blocked-uas'

function userAgentFor(botString: string) {
const randOne = (Math.random() + 1).toString(36).substring(7)
Expand Down
2 changes: 1 addition & 1 deletion src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
_each,
_eachArray,
_extend,
_isBlockedUA,
_register_event,
_safewrap_class,
isCrossDomainCookie,
Expand Down Expand Up @@ -58,6 +57,7 @@ import { _info } from './utils/event-utils'
import { logger } from './utils/logger'
import { document, userAgent } from './utils/globals'
import { SessionPropsManager } from './session-props'
import { _isBlockedUA } from './utils/blocked-uas'

/*
SIMPLE STYLE GUIDE:
Expand Down
60 changes: 60 additions & 0 deletions src/utils/blocked-uas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export const DEFAULT_BLOCKED_UA_STRS = [
'ahrefsbot',
'applebot',
'baiduspider',
'bingbot',
'bingpreview',
'bot.htm',
'bot.php',
'crawler',
'duckduckbot',
'facebookexternal',
'facebookcatalog',
'gptbot',
'hubspot',
'linkedinbot',
'mj12bot',
'petalbot',
'pinterest',
'prerender',
'rogerbot',
'screaming frog',
'semrushbot',
'sitebulb',
'twitterbot',
'yahoo! slurp',
'yandexbot',

// a whole bunch of goog-specific crawlers
// https://developers.google.com/search/docs/advanced/crawling/overview-google-crawlers
'adsbot-google',
'apis-google',
'duplexweb-google',
'feedfetcher-google',
'google favicon',
'google web preview',
'google-read-aloud',
'googlebot',
'googleweblight',
'mediapartners-google',
'storebot-google',
]

// _.isBlockedUA()
// This is to 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
}
const uaLower = ua.toLowerCase()
return DEFAULT_BLOCKED_UA_STRS.concat(customBlockedUserAgents || []).some((blockedUA) => {
const blockedUaLower = blockedUA.toLowerCase()
if (uaLower.includes) {
return uaLower.includes(blockedUaLower)
} else {
// IE 11 :/
return uaLower.indexOf(blockedUaLower) !== -1
}
})
}
61 changes: 0 additions & 61 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,67 +363,6 @@ export const _utf8Encode = function (string: string): string {
return utftext
}

export const DEFAULT_BLOCKED_UA_STRS = [
'ahrefsbot',
'applebot',
'baiduspider',
'bingbot',
'bingpreview',
'bot.htm',
'bot.php',
'crawler',
'duckduckbot',
'facebookexternal',
'facebookcatalog',
'gptbot',
'hubspot',
'linkedinbot',
'mj12bot',
'petalbot',
'pinterest',
'prerender',
'rogerbot',
'screaming frog',
'semrushbot',
'sitebulb',
'twitterbot',
'yahoo! slurp',
'yandexbot',

// a whole bunch of goog-specific crawlers
// https://developers.google.com/search/docs/advanced/crawling/overview-google-crawlers
'adsbot-google',
'apis-google',
'duplexweb-google',
'feedfetcher-google',
'google favicon',
'google web preview',
'google-read-aloud',
'googlebot',
'googleweblight',
'mediapartners-google',
'storebot-google',
]

// _.isBlockedUA()
// This is to 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
}
const uaLower = ua.toLowerCase()
return DEFAULT_BLOCKED_UA_STRS.concat(customBlockedUserAgents || []).some((blockedUA) => {
const blockedUaLower = blockedUA.toLowerCase()
if (uaLower.includes) {
return uaLower.includes(blockedUaLower)
} else {
// IE 11 :/
return uaLower.indexOf(blockedUaLower) !== -1
}
})
}

export const _register_event = (function () {
// written by Dean Edwards, 2005
// with input from Tino Zijdel - [email protected]
Expand Down
Loading