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
41 lines (37 loc) · 1.5 KB
/
Copy pathindex.ts
File metadata and controls
41 lines (37 loc) · 1.5 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
33
34
35
36
37
38
39
40
41
import { BrowserPlugin, BrowserTracker } from '@snowplow/browser-tracker-core';
import { integrations, AvailablePlugins } from './integrations';
import { createEventSpecificationContext } from './utils';
const _trackers: Record<string, BrowserTracker> = {};
type EventSpecificationsPluginOptions = {
[k in AvailablePlugins]?: Record<string, string>;
};
const defaultPluginOptions = {};
export function EventSpecificationsPlugin(
pluginOptions: EventSpecificationsPluginOptions = defaultPluginOptions
): BrowserPlugin {
const options = { ...defaultPluginOptions, ...pluginOptions };
const enabledIntegrations = Object.keys(integrations).filter(
(integration) => options[integration as AvailablePlugins]
);
let trackerId: string;
return {
activateBrowserPlugin: (tracker) => {
trackerId = tracker.id;
_trackers[trackerId] = tracker;
},
beforeTrack(payloadBuilder) {
enabledIntegrations.forEach((integrationKey) => {
const matchedEvent = integrations[integrationKey as AvailablePlugins].detectMatchingEvent(payloadBuilder);
if (!matchedEvent) {
return;
}
const matchingPluginEventMap = options[integrationKey as AvailablePlugins];
const matchingEventSpecificationId = matchingPluginEventMap && matchingPluginEventMap[matchedEvent];
if (!matchingEventSpecificationId) {
return;
}
payloadBuilder.addContextEntity(createEventSpecificationContext(matchingEventSpecificationId));
});
},
};
}