posthog-js
Version:
Posthog-js allows you to automatically capture usage and send events to PostHog.
41 lines • 1.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isLikelyWebViewBot = isLikelyWebViewBot;
/**
* Opt-in heuristic that flags user agents whose Chrome token appears without
* the usual AppleWebKit / Safari tokens that real Chrome always emits. Legit
* mobile WebViews and custom WebView apps CAN omit these, so this is a
* client-side-only signal — intended for callers who want to filter or tag
* events in `before_send`, not a universal blocklist entry.
*
* This is deliberately a standalone utility (not wired into the default
* blocklist) so default SDK behavior is unchanged. Server-side rules remain
* the primary bot-detection surface — see posthog/posthog#109617 and #109620.
*
* @example
* ```ts
* import { isLikelyWebViewBot } from 'posthog-js/customizations'
*
* posthog.init('PROJECT_TOKEN', {
* before_send: (event) => {
* if (event && isLikelyWebViewBot(navigator.userAgent)) {
* event.properties.$suspected_webview_bot = true
* }
* return event
* },
* })
* ```
*/
function isLikelyWebViewBot(ua) {
if (!ua) {
return false;
}
var uaLower = ua.toLowerCase();
var hasChrome = uaLower.indexOf('chrome/') !== -1;
var hasWebKit = uaLower.indexOf('applewebkit/') !== -1;
var hasSafari = uaLower.indexOf('safari/') !== -1;
// A Chrome token without BOTH WebKit and Safari tokens is unusual for a
// real browser; most legitimate WebViews still carry the full triple.
return hasChrome && (!hasWebKit || !hasSafari);
}
//# sourceMappingURL=bot-detection.js.map