forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
32 lines (29 loc) · 1.04 KB
/
Copy pathindex.ts
File metadata and controls
32 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { BrowserPlugin } from '@snowplow/browser-tracker-core';
import { LOG } from '@snowplow/tracker-core';
import { load } from '@fingerprintjs/botd';
import { CLIENT_SIDE_BOT_DETECTION_SCHEMA } from './schemata';
import { BotDetectionContextData } from './types';
export { BotDetectionContextData, BotKind } from './types';
let contextData: BotDetectionContextData | undefined;
let detectionStarted = false;
export function BotDetectionPlugin(): BrowserPlugin {
return {
activateBrowserPlugin: () => {
if (!detectionStarted) {
detectionStarted = true;
load()
.then((detector) => detector.detect())
.then((result) => {
contextData = result.bot ? { bot: true, kind: result.botKind } : { bot: false, kind: null };
})
.catch((err) => LOG.error('BotDetectionPlugin: BotD load/detect failed', err));
}
},
contexts: () => {
if (contextData) {
return [{ schema: CLIENT_SIDE_BOT_DETECTION_SCHEMA, data: contextData }];
}
return [];
},
};
}