Skip to content

Commit

Permalink
Move blocked UAs to own file (#905)
Browse files Browse the repository at this point in the history
  • Loading branch information
robbie-c authored Nov 20, 2023
1 parent 4ee20ea commit 8823f9e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 70 deletions.
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

0 comments on commit 8823f9e

Please sign in to comment.