-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.js
More file actions
128 lines (104 loc) · 4.02 KB
/
template.js
File metadata and controls
128 lines (104 loc) · 4.02 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
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
const dataLayer = require('copyFromDataLayer');
const event = dataLayer('event', 2);
const getType = require('getType');
const queryPermission = require('queryPermission');
const makeNumber = require('makeNumber');
const makeString = require('makeString');
const Math = require('Math');
// 1. Determine Mapping Result
let mappingResult;
if (data.selectionType === 'standardMapping') {
const standardMapping = {
'add_payment_info': data.add_payment_info,
'add_shipping_info': data.add_shipping_info,
'add_to_cart': data.add_to_cart,
'add_to_wishlist': data.add_to_wishlist,
'begin_checkout': data.begin_checkout,
'purchase': data.purchase,
'refund': data.refund,
'remove_from_cart': data.remove_from_cart,
'select_item': data.select_item,
'select_promotion': data.select_promotion,
'view_cart': data.view_cart,
'view_item': data.view_item,
'view_item_list': data.view_item_list,
'view_promotion': data.view_promotion
};
mappingResult = standardMapping[event];
} else if (data.selectionType === 'customMapping' && data.selectionCustom) {
data.selectionCustom.forEach((customArray) => {
if (customArray.customParameter === customArray.customParameterVariable) {
mappingResult = customArray.customParameter;
}
});
}
// 2. Validate Permissions and Extract Data
const keyPath = 'ecommerce.items';
if (!queryPermission('read_data_layer', keyPath) || !mappingResult) {
return;
}
const ecom = data.inputSelectionVariable ? data.inputSelectionVariable : dataLayer('ecommerce', 1);
if (!ecom || !ecom.items || getType(ecom.items) !== 'array') {
return;
}
let itemsArray = ecom.items;
// --------------------------------------------------------
// Filtering Logic (Target Specific Items)
// --------------------------------------------------------
if (data.enableFilter && data.filterKey && data.filterValue) {
const targetValue = data.normalizeFilter ? makeString(data.filterValue).toLowerCase().trim() : makeString(data.filterValue);
itemsArray = itemsArray.filter((item) => {
let itemValue = item[data.filterKey];
// If the key is missing from the item:
// - Drop it if we are INCLUDING specific matches.
// - Keep it if we are EXCLUDING specific matches.
if (itemValue === undefined || itemValue === null) {
return data.filterMode === 'exclude';
}
itemValue = makeString(itemValue);
const finalItemValue = data.normalizeFilter ? itemValue.toLowerCase().trim() : itemValue;
const isMatch = finalItemValue === targetValue;
// Return the opposite if the user selected 'exclude'
return data.filterMode === 'exclude' ? !isMatch : isMatch;
});
}
// Exit if array is empty after filtering
if (!itemsArray.length) {
return;
}
// --------------------------------------------------------
// 3. Process Items (String Map vs Metric Sum)
if (data.itemTypeSelection === 'string') {
const paramDelimiter = data.paramDelimiter;
const itemString = data.itemStandardString ? data.itemStandardString : data.itemCustomString;
const values = itemsArray
.map(obj => obj[itemString])
.filter(v => v != null && v !== '');
if (!values.length) return;
if (data.itemUniqueString) {
const unique = [];
for (let i = 0; i < values.length; i++) {
if (unique.indexOf(values[i]) === -1) {
unique.push(values[i]);
}
}
return unique.join(paramDelimiter);
}
return values.join(paramDelimiter);
} else {
const itemMetric = data.itemStandardMetric ? data.itemStandardMetric : data.itemCustomMetric;
if (data.multiplyQuantity) {
const rawTotal = itemsArray.reduce((total, item) => {
const value = makeNumber(item[itemMetric]) || 0;
const qty = makeNumber(item.quantity) || 1;
return total + (value * qty);
}, 0);
return Math.round(rawTotal * 100) / 100;
} else {
const rawTotal = itemsArray
.map(obj => obj[itemMetric])
.filter(val => val != null)
.reduce((total, val) => total + (makeNumber(val) || 0), 0);
return Math.round(rawTotal * 100) / 100;
}
}