UNPKG

1.51 kBJavaScriptView Raw
1import AvAnalyticsPlugin from './plugin';
2
3const requiredFields = [
4 'tradingPartnerId',
5 'customerId',
6 'category',
7 'applicationId',
8];
9
10export default class AvDmaAnalytics extends AvAnalyticsPlugin {
11 constructor(AvLogMessages, enabled) {
12 super(enabled);
13 this.AvLogMessages = AvLogMessages;
14 }
15
16 trackEvent(properties) {
17 if (!properties) return {};
18
19 const data = {};
20
21 if (properties.ApplicationId) {
22 properties.applicationId = properties.ApplicationId;
23 delete properties.ApplicationId;
24 }
25
26 if (properties.Category) {
27 properties.category = properties.Category;
28 delete properties.Category;
29 }
30
31 if (properties.tradingPartnerId || properties.TradingPartnerId) {
32 properties.tradingPartnerId =
33 properties.tradingPartnerId || properties.TradingPartnerId;
34 delete properties.TradingPartnerId;
35 }
36
37 if (properties.customerId || properties.CustomerId) {
38 properties.customerId = properties.customerId || properties.CustomerId;
39 delete properties.CustomerId;
40 } else {
41 properties.customerId = 'NA';
42 }
43
44 Object.keys(properties).forEach(key => {
45 const isRequiredField = requiredFields.filter(field => key === field)
46 .length;
47 if (!isRequiredField) {
48 data[key] = properties[key];
49
50 delete properties[key];
51 }
52 });
53
54 return this.AvLogMessages.send([{ ...properties, data }]);
55 }
56
57 trackPageView(url) {
58 return this.trackEvent({ event: 'page', url });
59 }
60}