-
Notifications
You must be signed in to change notification settings - Fork 35
/
user-agent-enrichment-plugin.ts
43 lines (34 loc) · 1.52 KB
/
user-agent-enrichment-plugin.ts
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
33
34
35
36
37
38
39
40
41
42
43
import { BrowserConfig, EnrichmentPlugin, Event } from '@amplitude/analytics-types';
import { CreateUserAgentEnrichmentPlugin, Options } from './typings/user-agent-enrichment-plugin';
import UAParser from '@amplitude/ua-parser-js';
export const userAgentEnrichmentPlugin: CreateUserAgentEnrichmentPlugin = function (options: Options = {}) {
const { osName = true, osVersion = true, deviceManufacturer = true, deviceModel = true } = options;
let uaResult: UAParser.IResult;
const plugin: EnrichmentPlugin = {
name: '@amplitude/plugin-user-agent-enrichment-browser',
type: 'enrichment',
setup: async function (config: BrowserConfig) {
let userAgent: string | undefined;
/* istanbul ignore else */
if (typeof navigator !== 'undefined') {
userAgent = navigator.userAgent;
}
uaResult = new UAParser(userAgent).getResult();
config.loggerProvider.log('Installing @amplitude/plugin-user-agent-enrichment-browser.');
},
execute: async (event: Event) => {
const uaOsName = uaResult.browser.name;
const UaOsVersion = uaResult.browser.version;
const UaDeviceModel = uaResult.device.model || uaResult.os.name;
const UaDeviceVendor = uaResult.device.vendor;
return {
...event,
...(osName && { os_name: uaOsName }),
...(osVersion && { os_version: UaOsVersion }),
...(deviceManufacturer && { device_manufacturer: UaDeviceVendor }),
...(deviceModel && { device_model: UaDeviceModel }),
};
},
};
return plugin;
};