@availity/analytics-core 3.1.6 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.yml +2 -0
- package/CHANGELOG.md +30 -3
- package/dist/index.d.ts +80 -0
- package/dist/index.js +289 -0
- package/dist/index.mjs +260 -0
- package/jest.config.js +16 -0
- package/package.json +16 -13
- package/project.json +23 -0
- package/{types → src}/analytics.d.ts +14 -2
- package/{types → src}/dma.d.ts +0 -0
- package/{types → src}/index.d.ts +0 -0
- package/{types → src}/plugin.d.ts +0 -0
- package/{types → src}/splunk.d.ts +0 -0
- package/src/tests/analytics.test.ts +187 -0
- package/src/tests/dma.test.ts +31 -0
- package/src/tests/plugin.test.ts +23 -0
- package/src/tests/splunk.test.ts +68 -0
- package/src/tests/util.test.ts +52 -0
- package/src/util.d.ts +14 -0
- package/tsconfig.json +5 -0
- package/tsconfig.spec.json +10 -0
- package/LICENSE +0 -21
- package/lib/analytics.js +0 -291
- package/lib/dma.js +0 -99
- package/lib/index.js +0 -39
- package/lib/plugin.js +0 -36
- package/lib/splunk.js +0 -52
- package/lib/util.js +0 -79
package/.eslintrc.yml
ADDED
package/CHANGELOG.md
CHANGED
|
@@ -1,7 +1,34 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver).
|
|
4
|
+
|
|
5
|
+
# [4.0.0](https://github.com/Availity/sdk-js/compare/@availity/analytics-core@3.1.8...@availity/analytics-core@4.0.0) (2022-04-28)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
* build!: switch from lerna to nx and compile to esm with tsup ([c586085](https://github.com/Availity/sdk-js/commit/c5860856ca96b743a0653d335ea00f0889132f7f))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### BREAKING CHANGES
|
|
12
|
+
|
|
13
|
+
* Drop Internet Explorer support
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
## [3.1.8](https://github.com/availity/sdk-js/compare/@availity/analytics-core@3.1.6...@availity/analytics-core@3.1.8) (2022-02-22)
|
|
18
|
+
|
|
19
|
+
**Note:** Version bump only for package @availity/analytics-core
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
## [3.1.7](https://github.com/availity/sdk-js/compare/@availity/analytics-core@3.1.6...@availity/analytics-core@3.1.7) (2022-01-19)
|
|
26
|
+
|
|
27
|
+
**Note:** Version bump only for package @availity/analytics-core
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
2
31
|
|
|
3
|
-
All notable changes to this project will be documented in this file.
|
|
4
|
-
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
32
|
|
|
6
33
|
## [3.1.6](https://github.com/availity/sdk-js/compare/@availity/analytics-core@3.1.5...@availity/analytics-core@3.1.6) (2021-12-21)
|
|
7
34
|
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
+
declare class AvAnalytics {
|
|
4
|
+
plugins: any[];
|
|
5
|
+
|
|
6
|
+
attributePrefix: string;
|
|
7
|
+
|
|
8
|
+
recursive: boolean;
|
|
9
|
+
|
|
10
|
+
pageTracking: boolean;
|
|
11
|
+
|
|
12
|
+
isPageTracking: boolean;
|
|
13
|
+
|
|
14
|
+
hasInit: boolean;
|
|
15
|
+
|
|
16
|
+
constructor(
|
|
17
|
+
plugins: any | any[],
|
|
18
|
+
promise?: PromiseConstructor,
|
|
19
|
+
pageTracking?: boolean,
|
|
20
|
+
autoTrack?: boolean,
|
|
21
|
+
options?: Record<string, any>
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
startAutoTrack(): void;
|
|
25
|
+
|
|
26
|
+
stopAutoTrack(): void;
|
|
27
|
+
|
|
28
|
+
handleEvent(event: any): void;
|
|
29
|
+
|
|
30
|
+
invalidEvent(event: any): boolean;
|
|
31
|
+
|
|
32
|
+
getAnalyticAttrs(elem: any): any;
|
|
33
|
+
|
|
34
|
+
startPageTracking(): void;
|
|
35
|
+
|
|
36
|
+
stopPageTracking(): void;
|
|
37
|
+
|
|
38
|
+
init(): void;
|
|
39
|
+
|
|
40
|
+
setPageTracking(value?: any): void;
|
|
41
|
+
|
|
42
|
+
trackEvent(properties: any): Promise<any[]>;
|
|
43
|
+
|
|
44
|
+
trackPageView(url?: string): Promise<any[]>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
48
|
+
declare class AvAnalyticsPlugin {
|
|
49
|
+
constructor(enabled?: boolean);
|
|
50
|
+
|
|
51
|
+
trackEvent(): any;
|
|
52
|
+
|
|
53
|
+
trackPageView(): any;
|
|
54
|
+
|
|
55
|
+
isEnabled(): boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
declare class AvSplunkAnalytics extends AvAnalyticsPlugin {
|
|
62
|
+
constructor(AvLogMessages: any, enabled?: boolean);
|
|
63
|
+
|
|
64
|
+
trackEvent(properties: any): any;
|
|
65
|
+
|
|
66
|
+
trackPageView(url: string): any;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
declare class AvDmaAnalytics extends AvAnalyticsPlugin {
|
|
73
|
+
constructor(AvLogMessages: any, enabled?: boolean);
|
|
74
|
+
|
|
75
|
+
trackEvent(properties: any): any;
|
|
76
|
+
|
|
77
|
+
trackPageView(url: string): any;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export { AvAnalytics, AvAnalyticsPlugin, AvDmaAnalytics, AvSplunkAnalytics };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
+
var __spreadValues = (a, b) => {
|
|
11
|
+
for (var prop in b || (b = {}))
|
|
12
|
+
if (__hasOwnProp.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
if (__getOwnPropSymbols)
|
|
15
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
+
if (__propIsEnum.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
}
|
|
19
|
+
return a;
|
|
20
|
+
};
|
|
21
|
+
var __export = (target, all) => {
|
|
22
|
+
for (var name in all)
|
|
23
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
24
|
+
};
|
|
25
|
+
var __copyProps = (to, from, except, desc) => {
|
|
26
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
27
|
+
for (let key of __getOwnPropNames(from))
|
|
28
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
29
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
30
|
+
}
|
|
31
|
+
return to;
|
|
32
|
+
};
|
|
33
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
34
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
35
|
+
|
|
36
|
+
// src/index.js
|
|
37
|
+
var src_exports = {};
|
|
38
|
+
__export(src_exports, {
|
|
39
|
+
AvAnalytics: () => AvAnalytics,
|
|
40
|
+
AvAnalyticsPlugin: () => AvAnalyticsPlugin,
|
|
41
|
+
AvDmaAnalytics: () => AvDmaAnalytics,
|
|
42
|
+
AvSplunkAnalytics: () => AvSplunkAnalytics
|
|
43
|
+
});
|
|
44
|
+
module.exports = __toCommonJS(src_exports);
|
|
45
|
+
|
|
46
|
+
// src/util.js
|
|
47
|
+
var isLeftClickEvent = (event) => event.button === 0;
|
|
48
|
+
var isModifiedEvent = (event) => !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
|
|
49
|
+
var trackMap = {
|
|
50
|
+
select: ["focus", "blur"],
|
|
51
|
+
textarea: ["focus", "blur"],
|
|
52
|
+
input: ["focus", "blur"],
|
|
53
|
+
default: ["click"]
|
|
54
|
+
};
|
|
55
|
+
var isValidEventTypeOnTarget = (event) => (trackMap[event.target.nodeName.toLowerCase()] || trackMap.default).indexOf(event.type) > -1;
|
|
56
|
+
var isPluginEnabled = (plugin) => typeof plugin.isEnabled === "function" ? plugin.isEnabled() : plugin.isEnabled;
|
|
57
|
+
var camelCase = (str) => str.replace(/-([\da-z])/gi, (match, char) => char.toUpperCase());
|
|
58
|
+
var getComposedPath = (node) => {
|
|
59
|
+
let parent;
|
|
60
|
+
if (node.parentNode) {
|
|
61
|
+
parent = node.parentNode;
|
|
62
|
+
} else if (node.host) {
|
|
63
|
+
parent = node.host;
|
|
64
|
+
} else if (node.defaultView) {
|
|
65
|
+
parent = node.defaultView;
|
|
66
|
+
}
|
|
67
|
+
if (parent !== void 0) {
|
|
68
|
+
return [node, ...getComposedPath(parent)];
|
|
69
|
+
}
|
|
70
|
+
return [node];
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
// src/analytics.js
|
|
74
|
+
var AvAnalytics = class {
|
|
75
|
+
constructor(plugins, promise = Promise, pageTracking, autoTrack = true, options = {}) {
|
|
76
|
+
if (!plugins || !promise) {
|
|
77
|
+
throw new Error("[plugins] and [promise] must be defined");
|
|
78
|
+
}
|
|
79
|
+
this.plugins = Array.isArray(plugins) ? plugins : [plugins];
|
|
80
|
+
this.pageTracking = !!pageTracking;
|
|
81
|
+
if (options.eventModifiers) {
|
|
82
|
+
this.eventModifiers = Array.isArray(options.eventModifiers) ? options.eventModifiers : [options.eventModifiers];
|
|
83
|
+
} else {
|
|
84
|
+
this.eventModifiers = ["action"];
|
|
85
|
+
}
|
|
86
|
+
this.Promise = promise;
|
|
87
|
+
this.recursive = !!options.recursive;
|
|
88
|
+
this.attributePrefix = options.attributePrefix || "data-analytics";
|
|
89
|
+
this.isPageTracking = false;
|
|
90
|
+
this.hasInit = false;
|
|
91
|
+
if (autoTrack) {
|
|
92
|
+
this.startAutoTrack();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
startAutoTrack = () => {
|
|
96
|
+
document.body.addEventListener("click", this.handleEvent, true);
|
|
97
|
+
document.body.addEventListener("focus", this.handleEvent, true);
|
|
98
|
+
document.body.addEventListener("blur", this.handleEvent, true);
|
|
99
|
+
};
|
|
100
|
+
stopAutoTrack = () => {
|
|
101
|
+
document.body.removeEventListener("click", this.handleEvent, true);
|
|
102
|
+
document.body.removeEventListener("focus", this.handleEvent, true);
|
|
103
|
+
document.body.removeEventListener("blur", this.handleEvent, true);
|
|
104
|
+
};
|
|
105
|
+
handleEvent = (event) => {
|
|
106
|
+
if (this.invalidEvent(event)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const target = event.target || event.srcElement;
|
|
110
|
+
const path = getComposedPath(event.target);
|
|
111
|
+
let analyticAttrs = {};
|
|
112
|
+
if (this.recursive) {
|
|
113
|
+
for (const pth of path.reverse()) {
|
|
114
|
+
const attrs = this.getAnalyticAttrs(pth);
|
|
115
|
+
analyticAttrs = __spreadValues(__spreadValues({}, analyticAttrs), attrs);
|
|
116
|
+
if (Object.keys(attrs).length > 0) {
|
|
117
|
+
analyticAttrs.elemId = pth.getAttribute("id") || pth.getAttribute("name") || void 0;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
analyticAttrs = this.getAnalyticAttrs(target);
|
|
122
|
+
}
|
|
123
|
+
const actions = analyticAttrs ? this.eventModifiers.filter((mod) => analyticAttrs[mod] === event.type) : [];
|
|
124
|
+
if (Object.keys(analyticAttrs).length === 0 || this.recursive && actions.length === 0 || actions.length === 0) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
analyticAttrs.action = analyticAttrs.action || event.type;
|
|
128
|
+
analyticAttrs.event = event.type;
|
|
129
|
+
analyticAttrs.elemId = analyticAttrs.elemId || target.getAttribute("id") || target.getAttribute("name") || void 0;
|
|
130
|
+
if (analyticAttrs.elemId === void 0) {
|
|
131
|
+
delete analyticAttrs.elemId;
|
|
132
|
+
}
|
|
133
|
+
for (const key of actions) {
|
|
134
|
+
if (key !== "action" && key !== "event") {
|
|
135
|
+
delete analyticAttrs[key];
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
this.trackEvent(analyticAttrs);
|
|
139
|
+
};
|
|
140
|
+
invalidEvent = (event) => isModifiedEvent(event) || event.type === "click" && !isLeftClickEvent(event) || !isValidEventTypeOnTarget(event);
|
|
141
|
+
getAnalyticAttrs = (elem) => {
|
|
142
|
+
if (!elem.attributes) {
|
|
143
|
+
return {};
|
|
144
|
+
}
|
|
145
|
+
const attrs = elem.attributes;
|
|
146
|
+
const analyticAttrs = {};
|
|
147
|
+
if (elem.nodeType === 1) {
|
|
148
|
+
for (let i = attrs.length - 1; i >= 0; i--) {
|
|
149
|
+
const { name } = attrs[i];
|
|
150
|
+
if (name.indexOf(`${this.attributePrefix}-`) === 0) {
|
|
151
|
+
const camelName = camelCase(name.slice(this.attributePrefix.length + 1));
|
|
152
|
+
analyticAttrs[camelName] = elem.getAttribute(name);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return analyticAttrs;
|
|
157
|
+
};
|
|
158
|
+
startPageTracking = () => {
|
|
159
|
+
if (!this.pageListener) {
|
|
160
|
+
this.pageListener = this.trackPageView;
|
|
161
|
+
window.addEventListener("hashchange", this.pageListener, false);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
stopPageTracking = () => {
|
|
165
|
+
if (this.pageListener) {
|
|
166
|
+
window.removeEventListener("hashchange", this.pageListener, false);
|
|
167
|
+
delete this.pageListener;
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
init = () => {
|
|
171
|
+
this.setPageTracking();
|
|
172
|
+
for (const plugin of this.plugins) {
|
|
173
|
+
if (isPluginEnabled(plugin) && typeof plugin.init === "function") {
|
|
174
|
+
plugin.init();
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
setPageTracking = (value) => {
|
|
179
|
+
if (value != void 0) {
|
|
180
|
+
this.pageTracking = !!value;
|
|
181
|
+
}
|
|
182
|
+
const canPageTrack = typeof this.startPageTracking === "function" && typeof this.stopPageTracking === "function";
|
|
183
|
+
if (canPageTrack && this.pageTracking !== this.isPageTracking) {
|
|
184
|
+
if (this.pageTracking) {
|
|
185
|
+
this.startPageTracking();
|
|
186
|
+
} else {
|
|
187
|
+
this.stopPageTracking();
|
|
188
|
+
}
|
|
189
|
+
this.isPageTracking = this.pageTracking;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
trackEvent = (properties) => {
|
|
193
|
+
const promises = [];
|
|
194
|
+
properties.url = properties.url || window.location.href || "N/A";
|
|
195
|
+
for (const plugin of this.plugins) {
|
|
196
|
+
const props = __spreadValues({}, properties);
|
|
197
|
+
if (isPluginEnabled(plugin) && typeof plugin.trackEvent === "function") {
|
|
198
|
+
promises.push(plugin.trackEvent(props));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return this.Promise.all(promises);
|
|
202
|
+
};
|
|
203
|
+
trackPageView = (url) => {
|
|
204
|
+
if (typeof url === "object") {
|
|
205
|
+
url = url.newURL;
|
|
206
|
+
}
|
|
207
|
+
url = url || window.location.href;
|
|
208
|
+
const promises = [];
|
|
209
|
+
for (const plugin of this.plugins) {
|
|
210
|
+
if (isPluginEnabled(plugin) && typeof plugin.trackPageView === "function") {
|
|
211
|
+
promises.push(plugin.trackPageView(url));
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return this.Promise.all(promises);
|
|
215
|
+
};
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
// src/plugin.js
|
|
219
|
+
var AvAnalyticsPlugin = class {
|
|
220
|
+
constructor(enabled = true) {
|
|
221
|
+
this.enabled = !!enabled;
|
|
222
|
+
}
|
|
223
|
+
trackEvent() {
|
|
224
|
+
}
|
|
225
|
+
trackPageView() {
|
|
226
|
+
}
|
|
227
|
+
isEnabled() {
|
|
228
|
+
return this.enabled;
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
// src/splunk.js
|
|
233
|
+
var AvSplunkAnalytics = class extends AvAnalyticsPlugin {
|
|
234
|
+
constructor(AvLogMessages, enabled) {
|
|
235
|
+
super(enabled);
|
|
236
|
+
this.AvLogMessages = AvLogMessages;
|
|
237
|
+
}
|
|
238
|
+
trackEvent(properties) {
|
|
239
|
+
properties.level = properties.level || "info";
|
|
240
|
+
return this.AvLogMessages[properties.level](properties);
|
|
241
|
+
}
|
|
242
|
+
trackPageView(url) {
|
|
243
|
+
return this.trackEvent({ event: "page", url });
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// src/dma.js
|
|
248
|
+
var yup = __toESM(require("yup"));
|
|
249
|
+
var schema = yup.object().shape({
|
|
250
|
+
level: yup.string().optional(),
|
|
251
|
+
applicationId: yup.string().optional(),
|
|
252
|
+
payerSpaceId: yup.string().optional(),
|
|
253
|
+
label: yup.string().optional(),
|
|
254
|
+
appName: yup.string().optional(),
|
|
255
|
+
category: yup.string().optional(),
|
|
256
|
+
section: yup.string().optional(),
|
|
257
|
+
url: yup.string().optional(),
|
|
258
|
+
value: yup.string().optional(),
|
|
259
|
+
raw: yup.string().optional(),
|
|
260
|
+
feed: yup.string().optional(),
|
|
261
|
+
feedback: yup.string().optional(),
|
|
262
|
+
feedbackName: yup.string().optional(),
|
|
263
|
+
additionalFeedback: yup.string().optional(),
|
|
264
|
+
smile: yup.string().optional(),
|
|
265
|
+
surveyId: yup.string().optional()
|
|
266
|
+
}).noUnknown(true);
|
|
267
|
+
var AvDmaAnalytics = class extends AvAnalyticsPlugin {
|
|
268
|
+
constructor(AvLogMessages, enabled) {
|
|
269
|
+
super(enabled);
|
|
270
|
+
this.AvLogMessages = AvLogMessages;
|
|
271
|
+
}
|
|
272
|
+
trackEvent(properties) {
|
|
273
|
+
properties.level = properties.level || "info";
|
|
274
|
+
schema.validateSync(properties, {
|
|
275
|
+
strict: true
|
|
276
|
+
});
|
|
277
|
+
return this.AvLogMessages[properties.level](properties);
|
|
278
|
+
}
|
|
279
|
+
trackPageView(url) {
|
|
280
|
+
return this.trackEvent({ event: "page", url });
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
284
|
+
0 && (module.exports = {
|
|
285
|
+
AvAnalytics,
|
|
286
|
+
AvAnalyticsPlugin,
|
|
287
|
+
AvDmaAnalytics,
|
|
288
|
+
AvSplunkAnalytics
|
|
289
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
3
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
4
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __spreadValues = (a, b) => {
|
|
7
|
+
for (var prop in b || (b = {}))
|
|
8
|
+
if (__hasOwnProp.call(b, prop))
|
|
9
|
+
__defNormalProp(a, prop, b[prop]);
|
|
10
|
+
if (__getOwnPropSymbols)
|
|
11
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
12
|
+
if (__propIsEnum.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
}
|
|
15
|
+
return a;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// src/util.js
|
|
19
|
+
var isLeftClickEvent = (event) => event.button === 0;
|
|
20
|
+
var isModifiedEvent = (event) => !!(event.metaKey || event.altKey || event.ctrlKey || event.shiftKey);
|
|
21
|
+
var trackMap = {
|
|
22
|
+
select: ["focus", "blur"],
|
|
23
|
+
textarea: ["focus", "blur"],
|
|
24
|
+
input: ["focus", "blur"],
|
|
25
|
+
default: ["click"]
|
|
26
|
+
};
|
|
27
|
+
var isValidEventTypeOnTarget = (event) => (trackMap[event.target.nodeName.toLowerCase()] || trackMap.default).indexOf(event.type) > -1;
|
|
28
|
+
var isPluginEnabled = (plugin) => typeof plugin.isEnabled === "function" ? plugin.isEnabled() : plugin.isEnabled;
|
|
29
|
+
var camelCase = (str) => str.replace(/-([\da-z])/gi, (match, char) => char.toUpperCase());
|
|
30
|
+
var getComposedPath = (node) => {
|
|
31
|
+
let parent;
|
|
32
|
+
if (node.parentNode) {
|
|
33
|
+
parent = node.parentNode;
|
|
34
|
+
} else if (node.host) {
|
|
35
|
+
parent = node.host;
|
|
36
|
+
} else if (node.defaultView) {
|
|
37
|
+
parent = node.defaultView;
|
|
38
|
+
}
|
|
39
|
+
if (parent !== void 0) {
|
|
40
|
+
return [node, ...getComposedPath(parent)];
|
|
41
|
+
}
|
|
42
|
+
return [node];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/analytics.js
|
|
46
|
+
var AvAnalytics = class {
|
|
47
|
+
constructor(plugins, promise = Promise, pageTracking, autoTrack = true, options = {}) {
|
|
48
|
+
if (!plugins || !promise) {
|
|
49
|
+
throw new Error("[plugins] and [promise] must be defined");
|
|
50
|
+
}
|
|
51
|
+
this.plugins = Array.isArray(plugins) ? plugins : [plugins];
|
|
52
|
+
this.pageTracking = !!pageTracking;
|
|
53
|
+
if (options.eventModifiers) {
|
|
54
|
+
this.eventModifiers = Array.isArray(options.eventModifiers) ? options.eventModifiers : [options.eventModifiers];
|
|
55
|
+
} else {
|
|
56
|
+
this.eventModifiers = ["action"];
|
|
57
|
+
}
|
|
58
|
+
this.Promise = promise;
|
|
59
|
+
this.recursive = !!options.recursive;
|
|
60
|
+
this.attributePrefix = options.attributePrefix || "data-analytics";
|
|
61
|
+
this.isPageTracking = false;
|
|
62
|
+
this.hasInit = false;
|
|
63
|
+
if (autoTrack) {
|
|
64
|
+
this.startAutoTrack();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
startAutoTrack = () => {
|
|
68
|
+
document.body.addEventListener("click", this.handleEvent, true);
|
|
69
|
+
document.body.addEventListener("focus", this.handleEvent, true);
|
|
70
|
+
document.body.addEventListener("blur", this.handleEvent, true);
|
|
71
|
+
};
|
|
72
|
+
stopAutoTrack = () => {
|
|
73
|
+
document.body.removeEventListener("click", this.handleEvent, true);
|
|
74
|
+
document.body.removeEventListener("focus", this.handleEvent, true);
|
|
75
|
+
document.body.removeEventListener("blur", this.handleEvent, true);
|
|
76
|
+
};
|
|
77
|
+
handleEvent = (event) => {
|
|
78
|
+
if (this.invalidEvent(event)) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
const target = event.target || event.srcElement;
|
|
82
|
+
const path = getComposedPath(event.target);
|
|
83
|
+
let analyticAttrs = {};
|
|
84
|
+
if (this.recursive) {
|
|
85
|
+
for (const pth of path.reverse()) {
|
|
86
|
+
const attrs = this.getAnalyticAttrs(pth);
|
|
87
|
+
analyticAttrs = __spreadValues(__spreadValues({}, analyticAttrs), attrs);
|
|
88
|
+
if (Object.keys(attrs).length > 0) {
|
|
89
|
+
analyticAttrs.elemId = pth.getAttribute("id") || pth.getAttribute("name") || void 0;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
analyticAttrs = this.getAnalyticAttrs(target);
|
|
94
|
+
}
|
|
95
|
+
const actions = analyticAttrs ? this.eventModifiers.filter((mod) => analyticAttrs[mod] === event.type) : [];
|
|
96
|
+
if (Object.keys(analyticAttrs).length === 0 || this.recursive && actions.length === 0 || actions.length === 0) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
analyticAttrs.action = analyticAttrs.action || event.type;
|
|
100
|
+
analyticAttrs.event = event.type;
|
|
101
|
+
analyticAttrs.elemId = analyticAttrs.elemId || target.getAttribute("id") || target.getAttribute("name") || void 0;
|
|
102
|
+
if (analyticAttrs.elemId === void 0) {
|
|
103
|
+
delete analyticAttrs.elemId;
|
|
104
|
+
}
|
|
105
|
+
for (const key of actions) {
|
|
106
|
+
if (key !== "action" && key !== "event") {
|
|
107
|
+
delete analyticAttrs[key];
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
this.trackEvent(analyticAttrs);
|
|
111
|
+
};
|
|
112
|
+
invalidEvent = (event) => isModifiedEvent(event) || event.type === "click" && !isLeftClickEvent(event) || !isValidEventTypeOnTarget(event);
|
|
113
|
+
getAnalyticAttrs = (elem) => {
|
|
114
|
+
if (!elem.attributes) {
|
|
115
|
+
return {};
|
|
116
|
+
}
|
|
117
|
+
const attrs = elem.attributes;
|
|
118
|
+
const analyticAttrs = {};
|
|
119
|
+
if (elem.nodeType === 1) {
|
|
120
|
+
for (let i = attrs.length - 1; i >= 0; i--) {
|
|
121
|
+
const { name } = attrs[i];
|
|
122
|
+
if (name.indexOf(`${this.attributePrefix}-`) === 0) {
|
|
123
|
+
const camelName = camelCase(name.slice(this.attributePrefix.length + 1));
|
|
124
|
+
analyticAttrs[camelName] = elem.getAttribute(name);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return analyticAttrs;
|
|
129
|
+
};
|
|
130
|
+
startPageTracking = () => {
|
|
131
|
+
if (!this.pageListener) {
|
|
132
|
+
this.pageListener = this.trackPageView;
|
|
133
|
+
window.addEventListener("hashchange", this.pageListener, false);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
stopPageTracking = () => {
|
|
137
|
+
if (this.pageListener) {
|
|
138
|
+
window.removeEventListener("hashchange", this.pageListener, false);
|
|
139
|
+
delete this.pageListener;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
init = () => {
|
|
143
|
+
this.setPageTracking();
|
|
144
|
+
for (const plugin of this.plugins) {
|
|
145
|
+
if (isPluginEnabled(plugin) && typeof plugin.init === "function") {
|
|
146
|
+
plugin.init();
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
setPageTracking = (value) => {
|
|
151
|
+
if (value != void 0) {
|
|
152
|
+
this.pageTracking = !!value;
|
|
153
|
+
}
|
|
154
|
+
const canPageTrack = typeof this.startPageTracking === "function" && typeof this.stopPageTracking === "function";
|
|
155
|
+
if (canPageTrack && this.pageTracking !== this.isPageTracking) {
|
|
156
|
+
if (this.pageTracking) {
|
|
157
|
+
this.startPageTracking();
|
|
158
|
+
} else {
|
|
159
|
+
this.stopPageTracking();
|
|
160
|
+
}
|
|
161
|
+
this.isPageTracking = this.pageTracking;
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
trackEvent = (properties) => {
|
|
165
|
+
const promises = [];
|
|
166
|
+
properties.url = properties.url || window.location.href || "N/A";
|
|
167
|
+
for (const plugin of this.plugins) {
|
|
168
|
+
const props = __spreadValues({}, properties);
|
|
169
|
+
if (isPluginEnabled(plugin) && typeof plugin.trackEvent === "function") {
|
|
170
|
+
promises.push(plugin.trackEvent(props));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return this.Promise.all(promises);
|
|
174
|
+
};
|
|
175
|
+
trackPageView = (url) => {
|
|
176
|
+
if (typeof url === "object") {
|
|
177
|
+
url = url.newURL;
|
|
178
|
+
}
|
|
179
|
+
url = url || window.location.href;
|
|
180
|
+
const promises = [];
|
|
181
|
+
for (const plugin of this.plugins) {
|
|
182
|
+
if (isPluginEnabled(plugin) && typeof plugin.trackPageView === "function") {
|
|
183
|
+
promises.push(plugin.trackPageView(url));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return this.Promise.all(promises);
|
|
187
|
+
};
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// src/plugin.js
|
|
191
|
+
var AvAnalyticsPlugin = class {
|
|
192
|
+
constructor(enabled = true) {
|
|
193
|
+
this.enabled = !!enabled;
|
|
194
|
+
}
|
|
195
|
+
trackEvent() {
|
|
196
|
+
}
|
|
197
|
+
trackPageView() {
|
|
198
|
+
}
|
|
199
|
+
isEnabled() {
|
|
200
|
+
return this.enabled;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
// src/splunk.js
|
|
205
|
+
var AvSplunkAnalytics = class extends AvAnalyticsPlugin {
|
|
206
|
+
constructor(AvLogMessages, enabled) {
|
|
207
|
+
super(enabled);
|
|
208
|
+
this.AvLogMessages = AvLogMessages;
|
|
209
|
+
}
|
|
210
|
+
trackEvent(properties) {
|
|
211
|
+
properties.level = properties.level || "info";
|
|
212
|
+
return this.AvLogMessages[properties.level](properties);
|
|
213
|
+
}
|
|
214
|
+
trackPageView(url) {
|
|
215
|
+
return this.trackEvent({ event: "page", url });
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
// src/dma.js
|
|
220
|
+
import * as yup from "yup";
|
|
221
|
+
var schema = yup.object().shape({
|
|
222
|
+
level: yup.string().optional(),
|
|
223
|
+
applicationId: yup.string().optional(),
|
|
224
|
+
payerSpaceId: yup.string().optional(),
|
|
225
|
+
label: yup.string().optional(),
|
|
226
|
+
appName: yup.string().optional(),
|
|
227
|
+
category: yup.string().optional(),
|
|
228
|
+
section: yup.string().optional(),
|
|
229
|
+
url: yup.string().optional(),
|
|
230
|
+
value: yup.string().optional(),
|
|
231
|
+
raw: yup.string().optional(),
|
|
232
|
+
feed: yup.string().optional(),
|
|
233
|
+
feedback: yup.string().optional(),
|
|
234
|
+
feedbackName: yup.string().optional(),
|
|
235
|
+
additionalFeedback: yup.string().optional(),
|
|
236
|
+
smile: yup.string().optional(),
|
|
237
|
+
surveyId: yup.string().optional()
|
|
238
|
+
}).noUnknown(true);
|
|
239
|
+
var AvDmaAnalytics = class extends AvAnalyticsPlugin {
|
|
240
|
+
constructor(AvLogMessages, enabled) {
|
|
241
|
+
super(enabled);
|
|
242
|
+
this.AvLogMessages = AvLogMessages;
|
|
243
|
+
}
|
|
244
|
+
trackEvent(properties) {
|
|
245
|
+
properties.level = properties.level || "info";
|
|
246
|
+
schema.validateSync(properties, {
|
|
247
|
+
strict: true
|
|
248
|
+
});
|
|
249
|
+
return this.AvLogMessages[properties.level](properties);
|
|
250
|
+
}
|
|
251
|
+
trackPageView(url) {
|
|
252
|
+
return this.trackEvent({ event: "page", url });
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
export {
|
|
256
|
+
AvAnalytics,
|
|
257
|
+
AvAnalyticsPlugin,
|
|
258
|
+
AvDmaAnalytics,
|
|
259
|
+
AvSplunkAnalytics
|
|
260
|
+
};
|
package/jest.config.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const { pathsToModuleNameMapper } = require('ts-jest/utils');
|
|
2
|
+
|
|
3
|
+
const { compilerOptions } = require('../../tsconfig.json');
|
|
4
|
+
|
|
5
|
+
module.exports = {
|
|
6
|
+
displayName: 'analytics-core',
|
|
7
|
+
preset: '../../jest.preset.js',
|
|
8
|
+
globals: {
|
|
9
|
+
'ts-jest': {
|
|
10
|
+
tsconfig: '<rootDir>/tsconfig.spec.json',
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
coverageDirectory: '../../coverage/analytics-core',
|
|
14
|
+
coverageReporters: ['json'],
|
|
15
|
+
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/../../' }),
|
|
16
|
+
};
|