forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubject.ts
More file actions
87 lines (76 loc) · 2.27 KB
/
Copy pathsubject.ts
File metadata and controls
87 lines (76 loc) · 2.27 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { CorePluginConfiguration, PayloadBuilder, TrackerCore } from '@snowplow/tracker-core';
import { ScreenSize, SubjectConfiguration } from './types';
export function newSubject(core: TrackerCore, configuration?: SubjectConfiguration) {
let domainUserId: string | undefined;
let networkUserId: string | undefined;
const addSubjectToPayload = (payload: PayloadBuilder) => {
payload.add('duid', domainUserId);
payload.add('nuid', networkUserId);
};
const setScreenResolution = (screenSize: ScreenSize) =>
core.setScreenResolution(String(screenSize[0]), String(screenSize[1]));
const setNetworkUserId = (userId: string | undefined) => {
networkUserId = userId;
};
const setDomainUserId = (userId: string | undefined) => {
domainUserId = userId;
};
const setColorDepth = (colorDepth: number) => {
core.setColorDepth(String(colorDepth));
};
const setScreenViewport = (screenSize: ScreenSize) => {
core.setViewport(String(screenSize[0]), String(screenSize[1]));
};
const setSubjectData = (data: SubjectConfiguration) => {
setNetworkUserId(data.networkUserId);
setDomainUserId(data.domainUserId);
if (data.userId) {
core.setUserId(data.userId);
}
if (data.useragent) {
core.setUseragent(data.useragent);
}
if (data.ipAddress) {
core.setIpAddress(data.ipAddress);
}
if (data.timezone) {
core.setTimezone(data.timezone);
}
if (data.language) {
core.setLang(data.language);
}
if (data.screenResolution) {
setScreenResolution(data.screenResolution);
}
if (data.colorDepth) {
setColorDepth(data.colorDepth);
}
if (data.screenViewport) {
setScreenViewport(data.screenViewport);
}
};
if (configuration) {
setSubjectData(configuration);
}
const subjectPlugin: CorePluginConfiguration = {
plugin: {
beforeTrack: addSubjectToPayload,
},
};
return {
subjectPlugin,
properties: {
setUserId: core.setUserId,
setIpAddress: core.setIpAddress,
setUseragent: core.setUseragent,
setTimezone: core.setTimezone,
setLanguage: core.setLang,
setScreenResolution,
setNetworkUserId,
setDomainUserId,
setSubjectData,
setColorDepth,
setScreenViewport,
},
};
}