-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreporter.js
195 lines (195 loc) · 7.45 KB
/
reporter.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
function CombineEventData(payload, target, data) {
let combination = Object.assign(payload(target), data ?? {});
return combination;
}
const eventMap = new Map([
['Page Load Started', {
payload: 'page',
data: {
page: (tgt, data) => {
const subsections = (new URL(location.href)).pathname.split('/').slice(2);
const pageNameData = [data['siteName'], data['pageCategory']].concat(subsections);
return Object.assign({
"analyticsTitle": "",
"blogAuthor": "",
"contentID": "",
"contentType": "",
"dataObject": "",
"destinationURL": "",
"errorType": "",
"gated": "",
"pageCategory": "",
pageName: data ? pageNameData.filter(v => (typeof v !== 'undefined' && v !== null && v !== '')).join('|') : '',
"siteName": "",
"pageTitle": "",
"pageType": "",
"pageSubType": "",
"pageStatus": "published",
previousPage: ((r) => {
if (r) {
let a = document.createElement("a");
a.href = r;
return a.href;
}
else {
return '';
}
})(document.referrer),
queryParameters: ((url) => Object.fromEntries(url.searchParams))(new URL(window.location.href)),
siteExperience: ((w) => (w > 992) ? 'desktop' : ((w > 768) ? 'tablet' : 'mobile'))(window.innerWidth),
"siteLanguage": "",
"subsection": subsections[0] || "",
"subsection2": subsections[1] || "",
"subsection3": subsections[2] || ""
}, data);
}
}
}],
['Page Load Completed', {}],
['User Signed In', {
payload: 'user',
data: {
user: (tgt, data) => {
return Object.assign({
"custKey": ""
}, data);
}
}
}],
['User Detected', { payload: 'user', data: {
user: (tgt, data) => {
return Object.assign({
"custKey": "",
"ebsAccountNumber": "",
"accountIDType": "",
"userID": "",
"lastLoginDate": "",
"loggedIn": "false",
"socialAccountsLinked": "",
"subscriptionFrequency": "",
"subscriptionLevel": "",
"hashedEmail": ""
}, data);
}
}
}],
['Onsite Search Performed', {
payload: 'onsiteSearch',
data: {
onsiteSearch: (tgt, data) => {
const kw = { searchType: '', searchTerm: '', searchMethod: '' };
if (data['keyword']) {
data['keyword'] = Object.assign(kw, data['keyword'] ?? {});
}
else {
data = Object.assign({
"keyword": kw
}, data);
}
return data;
}
}
}],
['Onsite Search Failed', {
payload: 'onsiteSearch',
data: {
onsiteSearch: (tgt, data) => {
return Object.assign({
"keyword": {
"searchType": "",
"searchTerm": ""
}
}, data);
}
}
}],
['Content Listing Displayed', {
payload: 'listingDisplayed',
data: {
listingDisplayed: (tgt, data) => {
return Object.assign({
"displayCount": "",
"listingDriver": "",
"filterList": "",
"resultsCount": ""
}, data);
}
}
}],
['Content Listing Item Clicked', {
payload: 'listingClicked',
data: {
listingClicked: (tgt, data) => {
return Object.assign({
"displayPosition": "",
"linkType": "",
"contentTitle": ""
}, data);
}
}
}],
['Form Viewed', { payload: 'form', data: { form: (tgt, data) => { return {}; } } }],
['Form Submission Succeeded', { payload: 'form', data: { form: (tgt, data) => { return {}; } } }],
['Form Submission Failed', { payload: 'form', data: { form: (tgt, data) => { return {}; } } }],
['Error Message Presented', {
payload: 'error',
data: {
error: (tgt, data) => {
return Object.assign({
errorCode: '',
errorType: ''
}, data);
}
}
}],
['Download Started', {
payload: 'linkInfo',
data: {
linkInfo: (tgt, data) => {
return Object.assign({
"fileName": "",
"fileType": "",
"product": []
}, data);
}
}
}],
['default', {
payload: 'default',
data: {
default: (tgt, data) => {
return Object.assign({}, data);
}
}
}]
]);
export class ReporterEvent extends Event {
constructor(name, data, emitName = 'cpx-report') {
super(emitName, { bubbles: true, composed: true });
this.toJSON = () => Object.assign({ event: this.name }, this.data);
this.name = name;
this.obj = eventMap.has(name) ? eventMap.get(name) : eventMap.get('default');
if (this.obj && this.obj.payload && this.obj.data) {
const objEntries = new Map([[
this.obj.payload,
this.obj.data[this.obj.payload](this.currentTarget, data ?? {})
]]);
this.data = Object.fromEntries(objEntries);
}
}
}
if (import.meta.url) {
const reporter = document.querySelector(`script[src*='${(new URL(import.meta.url)).pathname}']:not([reported])`);
if (reporter instanceof HTMLElement) {
if (reporter.getAttribute('reported') == null) {
const txt = reporter.textContent;
if (txt !== null && txt.length > 0) {
const data = JSON.parse(reporter.textContent ?? '');
const emitName = reporter.getAttribute('data-emit') ?? 'cpx-report';
globalThis.dispatchEvent(new ReporterEvent(reporter.dataset.event, data, emitName));
reporter.setAttribute('reported', '');
}
}
}
}
globalThis['ReporterEvent'] = ReporterEvent;