feat(analytics): filter bot traffic client-side via SDK filter option
This commit is contained in:
1 parent
05a4ae298b
commit
69814bcc68
1 file changed
+18
-7
@@ -9,12 +9,23 @@ const SCRIPT_ID = 'openpanel-sdk';
|
|||||||
|
|
||||||
const SRC = OPENPANEL_SCRIPT_URL || 'https://openpanel.dev/op1.js';
|
const SRC = OPENPANEL_SCRIPT_URL || 'https://openpanel.dev/op1.js';
|
||||||
|
|
||||||
const OPTIONS = {
|
const BOT_FILTER = `function () {
|
||||||
clientId: OPENPANEL_CLIENT_ID,
|
try {
|
||||||
apiUrl: OPENPANEL_API_URL,
|
const ua = navigator.userAgent;
|
||||||
trackScreenViews: true,
|
|
||||||
trackOutgoingLinks: true
|
return !/bot|crawl|spider|headless|lighthouse|prerender|slurp/i.test(ua) && !navigator.webdriver;
|
||||||
};
|
} catch {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}`;
|
||||||
|
|
||||||
|
const INIT_OPTIONS = [
|
||||||
|
`clientId: ${JSON.stringify(OPENPANEL_CLIENT_ID)}`,
|
||||||
|
`apiUrl: ${JSON.stringify(OPENPANEL_API_URL)}`,
|
||||||
|
'trackScreenViews: true',
|
||||||
|
'trackOutgoingLinks: true',
|
||||||
|
`filter: ${BOT_FILTER}`
|
||||||
|
].join(', ');
|
||||||
|
|
||||||
const Analytics = () => {
|
const Analytics = () => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -52,7 +63,7 @@ const Analytics = () => {
|
|||||||
return (
|
return (
|
||||||
<script
|
<script
|
||||||
dangerouslySetInnerHTML={{
|
dangerouslySetInnerHTML={{
|
||||||
__html: `${getInitSnippet()}window.op('init', ${JSON.stringify(OPTIONS)});`
|
__html: `${getInitSnippet()}window.op('init', {${INIT_OPTIONS}});`
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in new issue
Block a user