choreograph-create-pixel 0.1.10 โ 0.1.12
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/README.md +15 -6
- package/dist/bundle.cjs.js +41 -57
- package/dist/bundle.esm.js +41 -57
- package/dist/bundle.iife.js +2 -2
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -7,8 +7,8 @@ This library lets you apply best practises to [Choreograph Create](https://www.l
|
|
|
7
7
|
- [x] Supports **scrape**, **view**, **basket**, and **purchase** pixels
|
|
8
8
|
- [x] Supports dynamic page changes
|
|
9
9
|
- [x] Continuous URL validation
|
|
10
|
-
- [x]
|
|
11
|
-
- [x] Prevents scraping browser-translated
|
|
10
|
+
- [x] Optionally trigger by user interaction
|
|
11
|
+
- [x] Prevents scraping browser-translated pages
|
|
12
12
|
- [x] Console debugger
|
|
13
13
|
|
|
14
14
|
## Quickstart
|
|
@@ -121,6 +121,7 @@ Besides **scrape** and **view** pixels, this library also supports **basket** an
|
|
|
121
121
|
// (Optional) When should the pixel be enabled?
|
|
122
122
|
when: {
|
|
123
123
|
listener: "click",
|
|
124
|
+
|
|
124
125
|
elements: function () {
|
|
125
126
|
return document.querySelectorAll("button.basket[data-sku]");
|
|
126
127
|
},
|
|
@@ -141,13 +142,21 @@ Besides **scrape** and **view** pixels, this library also supports **basket** an
|
|
|
141
142
|
{
|
|
142
143
|
who: 0,
|
|
143
144
|
what: "purchase",
|
|
144
|
-
where: /example\.com\/
|
|
145
|
+
where: /example\.com\/cart/,
|
|
146
|
+
|
|
147
|
+
when: {
|
|
148
|
+
listener: "click",
|
|
149
|
+
|
|
150
|
+
elements: function () {
|
|
151
|
+
return document.querySelectorAll("button.checkout");
|
|
152
|
+
},
|
|
153
|
+
},
|
|
145
154
|
|
|
146
155
|
which: {
|
|
147
|
-
// sku
|
|
156
|
+
// which.sku supports returning an array of strings
|
|
148
157
|
sku: function () {
|
|
149
|
-
return [].map
|
|
150
|
-
return
|
|
158
|
+
return window.dataLayer[0].cart.map(function (product) {
|
|
159
|
+
return product.sku;
|
|
151
160
|
});
|
|
152
161
|
},
|
|
153
162
|
},
|
package/dist/bundle.cjs.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! choreograph-create-pixel v0.1.
|
|
1
|
+
/*! choreograph-create-pixel v0.1.12 2022/09/20 */
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
class ChoreographCreatePixel {
|
|
@@ -10,7 +10,7 @@ class ChoreographCreatePixel {
|
|
|
10
10
|
colors: { error: "#f44336", warn: "#ffa726", success: "#66bb6a" },
|
|
11
11
|
icons: { scrape: "๐", view: "๐", basket: "๐", purchase: "๐งพ" },
|
|
12
12
|
titleCss: "font:700 80% HelveticaNeue;margin:12px 0 8px",
|
|
13
|
-
messageCss: "font:120% HelveticaNeue;margin-bottom:12px",
|
|
13
|
+
messageCss: "font:500 120% HelveticaNeue;margin-bottom:12px",
|
|
14
14
|
eventTypes: {
|
|
15
15
|
view: "product-viewed",
|
|
16
16
|
basket: "product-basketed",
|
|
@@ -84,13 +84,13 @@ class ChoreographCreatePixel {
|
|
|
84
84
|
return this.getAllQueryParameters()[key];
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
log(
|
|
87
|
+
log(level, message, data = null) {
|
|
88
88
|
if (!this.config.debug || this.logs.includes(message)) return;
|
|
89
89
|
|
|
90
90
|
const args = [
|
|
91
91
|
`%c${
|
|
92
92
|
this.settings.icons[this.config.what]
|
|
93
|
-
}
|
|
93
|
+
} ${this.config.what.toUpperCase()} PIXEL DEBUG%c\n%c${message}`,
|
|
94
94
|
this.settings.titleCss,
|
|
95
95
|
"",
|
|
96
96
|
this.settings.colors[level]
|
|
@@ -105,43 +105,34 @@ class ChoreographCreatePixel {
|
|
|
105
105
|
|
|
106
106
|
validateConfig() {
|
|
107
107
|
if (typeof this.config.who !== "number")
|
|
108
|
-
this.log("Please use a number", {
|
|
109
|
-
level: "error",
|
|
110
|
-
data: { who: this.config.who },
|
|
111
|
-
});
|
|
108
|
+
this.log("error", "Please use a number", { who: this.config.who });
|
|
112
109
|
else if (
|
|
113
110
|
!["scrape", "view", "basket", "purchase"].includes(this.config.what)
|
|
114
111
|
)
|
|
115
|
-
this.log("Please use scrape, view, basket, or purchase", {
|
|
116
|
-
|
|
117
|
-
data: { what: this.config.what },
|
|
112
|
+
this.log("error", "Please use scrape, view, basket, or purchase", {
|
|
113
|
+
what: this.config.what,
|
|
118
114
|
});
|
|
119
115
|
else if (!(this.config.where instanceof RegExp))
|
|
120
|
-
this.log("Please use a regular expression", {
|
|
121
|
-
|
|
122
|
-
data: { where: this.config.where },
|
|
116
|
+
this.log("error", "Please use a regular expression", {
|
|
117
|
+
where: this.config.where,
|
|
123
118
|
});
|
|
124
119
|
else if (typeof this.config.which !== "object" || !this.config.which.sku)
|
|
125
|
-
this.log("Please provide an SKU", {
|
|
126
|
-
level: "error",
|
|
127
|
-
data: { which: this.config.which },
|
|
128
|
-
});
|
|
120
|
+
this.log("error", "Please provide an SKU", { which: this.config.which });
|
|
129
121
|
else return true;
|
|
130
122
|
}
|
|
131
123
|
|
|
132
124
|
cycle() {
|
|
133
125
|
if (!this.config.where.test(location.href)) {
|
|
134
|
-
this.log(`Pattern does not match ${location.href}`, {
|
|
135
|
-
|
|
136
|
-
data: { where: this.config.where },
|
|
126
|
+
this.log("warn", `Pattern does not match "${location.href}"`, {
|
|
127
|
+
where: this.config.where,
|
|
137
128
|
});
|
|
138
129
|
} else if (
|
|
139
130
|
this.config.what === "scrape" &&
|
|
140
131
|
document.querySelector('html[class*="translated-"]')
|
|
141
132
|
) {
|
|
142
133
|
this.log(
|
|
143
|
-
"
|
|
144
|
-
|
|
134
|
+
"warn",
|
|
135
|
+
"This page has been translated by the browser, and will be excluded"
|
|
145
136
|
);
|
|
146
137
|
} else if (this.config.when) {
|
|
147
138
|
try {
|
|
@@ -163,9 +154,8 @@ class ChoreographCreatePixel {
|
|
|
163
154
|
}
|
|
164
155
|
});
|
|
165
156
|
} catch (error) {
|
|
166
|
-
this.log(error.message, {
|
|
167
|
-
|
|
168
|
-
data: { when: { elements: this.config.when.elements } },
|
|
157
|
+
this.log("error", error.message, {
|
|
158
|
+
when: { elements: this.config.when.elements },
|
|
169
159
|
});
|
|
170
160
|
}
|
|
171
161
|
} else {
|
|
@@ -187,9 +177,8 @@ class ChoreographCreatePixel {
|
|
|
187
177
|
data[fieldName] = data[fieldName](element);
|
|
188
178
|
} catch (error) {
|
|
189
179
|
if (!this.config.optionalFields.includes(fieldName)) {
|
|
190
|
-
this.log(error.message, {
|
|
191
|
-
|
|
192
|
-
data: { which: { [fieldName]: this.config.which[fieldName] } },
|
|
180
|
+
this.log("error", error.message, {
|
|
181
|
+
which: { [fieldName]: this.config.which[fieldName] },
|
|
193
182
|
});
|
|
194
183
|
|
|
195
184
|
hasErrors = true;
|
|
@@ -202,9 +191,8 @@ class ChoreographCreatePixel {
|
|
|
202
191
|
|
|
203
192
|
if (data[fieldName] == null || data[fieldName] === "")
|
|
204
193
|
if (!this.config.optionalFields.includes(fieldName)) {
|
|
205
|
-
this.log("This required field's value is empty", {
|
|
206
|
-
|
|
207
|
-
data: { which: { [fieldName]: data[fieldName] } },
|
|
194
|
+
this.log("error", "This required field's value is empty", {
|
|
195
|
+
which: { [fieldName]: data[fieldName] },
|
|
208
196
|
});
|
|
209
197
|
|
|
210
198
|
hasErrors = true;
|
|
@@ -221,9 +209,8 @@ class ChoreographCreatePixel {
|
|
|
221
209
|
else this.send(newData);
|
|
222
210
|
});
|
|
223
211
|
} catch (error) {
|
|
224
|
-
this.log(error.message, {
|
|
225
|
-
|
|
226
|
-
data: { beforeSend: this.config.beforeSend },
|
|
212
|
+
this.log("error", error.message, {
|
|
213
|
+
beforeSend: this.config.beforeSend,
|
|
227
214
|
});
|
|
228
215
|
}
|
|
229
216
|
|
|
@@ -267,56 +254,53 @@ class ChoreographCreatePixel {
|
|
|
267
254
|
}
|
|
268
255
|
: null
|
|
269
256
|
)
|
|
270
|
-
.then((response) =>
|
|
271
|
-
if (!this.config.debug) return;
|
|
272
|
-
|
|
257
|
+
.then((response) =>
|
|
273
258
|
response
|
|
274
259
|
.json()
|
|
275
260
|
.then((result) => {
|
|
276
261
|
if (response.ok) {
|
|
277
|
-
this.log("Successful, with warnings. Details:",
|
|
278
|
-
level: "warn",
|
|
279
|
-
data: result,
|
|
280
|
-
});
|
|
262
|
+
this.log("warn", "Successful, with warnings. Details:", result);
|
|
281
263
|
|
|
282
264
|
try {
|
|
283
265
|
this.config.afterSend(data);
|
|
284
266
|
} catch (error) {
|
|
285
|
-
this.log(error.message, {
|
|
286
|
-
|
|
287
|
-
data: { afterSend: this.config.afterSend },
|
|
267
|
+
this.log("error", error.message, {
|
|
268
|
+
afterSend: this.config.afterSend,
|
|
288
269
|
});
|
|
289
270
|
}
|
|
290
271
|
} else
|
|
291
272
|
this.log(
|
|
273
|
+
"error",
|
|
292
274
|
`Failed: ${response.status} (${response.statusText}). Details:`,
|
|
293
|
-
|
|
275
|
+
result
|
|
294
276
|
);
|
|
277
|
+
|
|
278
|
+
this.logs.length = 0;
|
|
295
279
|
})
|
|
296
280
|
.catch(() => {
|
|
297
281
|
if (response.ok) {
|
|
298
|
-
this.log("
|
|
282
|
+
this.log("success", "Successful!", data);
|
|
299
283
|
|
|
300
284
|
try {
|
|
301
285
|
this.config.afterSend(data);
|
|
302
286
|
} catch (error) {
|
|
303
|
-
this.log(error.message, {
|
|
304
|
-
|
|
305
|
-
data: { afterSend: this.config.afterSend },
|
|
287
|
+
this.log("error", error.message, {
|
|
288
|
+
afterSend: this.config.afterSend,
|
|
306
289
|
});
|
|
307
290
|
}
|
|
308
291
|
} else
|
|
309
292
|
this.log(
|
|
293
|
+
"error",
|
|
310
294
|
`Failed: ${response.status} (${response.statusText}). Details:`,
|
|
311
|
-
|
|
295
|
+
response
|
|
312
296
|
);
|
|
313
|
-
|
|
314
|
-
|
|
297
|
+
|
|
298
|
+
this.logs.length = 0;
|
|
299
|
+
})
|
|
300
|
+
)
|
|
315
301
|
.catch((error) => {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
level: "error",
|
|
319
|
-
});
|
|
302
|
+
this.log("error", `Failed: ${error.message}`);
|
|
303
|
+
this.logs.length = 0;
|
|
320
304
|
});
|
|
321
305
|
}
|
|
322
306
|
}
|
package/dist/bundle.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! choreograph-create-pixel v0.1.
|
|
1
|
+
/*! choreograph-create-pixel v0.1.12 2022/09/20 */
|
|
2
2
|
class ChoreographCreatePixel {
|
|
3
3
|
constructor(userConfig) {
|
|
4
4
|
this.previouslyScrapedJsonString = null;
|
|
@@ -8,7 +8,7 @@ class ChoreographCreatePixel {
|
|
|
8
8
|
colors: { error: "#f44336", warn: "#ffa726", success: "#66bb6a" },
|
|
9
9
|
icons: { scrape: "๐", view: "๐", basket: "๐", purchase: "๐งพ" },
|
|
10
10
|
titleCss: "font:700 80% HelveticaNeue;margin:12px 0 8px",
|
|
11
|
-
messageCss: "font:120% HelveticaNeue;margin-bottom:12px",
|
|
11
|
+
messageCss: "font:500 120% HelveticaNeue;margin-bottom:12px",
|
|
12
12
|
eventTypes: {
|
|
13
13
|
view: "product-viewed",
|
|
14
14
|
basket: "product-basketed",
|
|
@@ -82,13 +82,13 @@ class ChoreographCreatePixel {
|
|
|
82
82
|
return this.getAllQueryParameters()[key];
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
log(
|
|
85
|
+
log(level, message, data = null) {
|
|
86
86
|
if (!this.config.debug || this.logs.includes(message)) return;
|
|
87
87
|
|
|
88
88
|
const args = [
|
|
89
89
|
`%c${
|
|
90
90
|
this.settings.icons[this.config.what]
|
|
91
|
-
}
|
|
91
|
+
} ${this.config.what.toUpperCase()} PIXEL DEBUG%c\n%c${message}`,
|
|
92
92
|
this.settings.titleCss,
|
|
93
93
|
"",
|
|
94
94
|
this.settings.colors[level]
|
|
@@ -103,43 +103,34 @@ class ChoreographCreatePixel {
|
|
|
103
103
|
|
|
104
104
|
validateConfig() {
|
|
105
105
|
if (typeof this.config.who !== "number")
|
|
106
|
-
this.log("Please use a number", {
|
|
107
|
-
level: "error",
|
|
108
|
-
data: { who: this.config.who },
|
|
109
|
-
});
|
|
106
|
+
this.log("error", "Please use a number", { who: this.config.who });
|
|
110
107
|
else if (
|
|
111
108
|
!["scrape", "view", "basket", "purchase"].includes(this.config.what)
|
|
112
109
|
)
|
|
113
|
-
this.log("Please use scrape, view, basket, or purchase", {
|
|
114
|
-
|
|
115
|
-
data: { what: this.config.what },
|
|
110
|
+
this.log("error", "Please use scrape, view, basket, or purchase", {
|
|
111
|
+
what: this.config.what,
|
|
116
112
|
});
|
|
117
113
|
else if (!(this.config.where instanceof RegExp))
|
|
118
|
-
this.log("Please use a regular expression", {
|
|
119
|
-
|
|
120
|
-
data: { where: this.config.where },
|
|
114
|
+
this.log("error", "Please use a regular expression", {
|
|
115
|
+
where: this.config.where,
|
|
121
116
|
});
|
|
122
117
|
else if (typeof this.config.which !== "object" || !this.config.which.sku)
|
|
123
|
-
this.log("Please provide an SKU", {
|
|
124
|
-
level: "error",
|
|
125
|
-
data: { which: this.config.which },
|
|
126
|
-
});
|
|
118
|
+
this.log("error", "Please provide an SKU", { which: this.config.which });
|
|
127
119
|
else return true;
|
|
128
120
|
}
|
|
129
121
|
|
|
130
122
|
cycle() {
|
|
131
123
|
if (!this.config.where.test(location.href)) {
|
|
132
|
-
this.log(`Pattern does not match ${location.href}`, {
|
|
133
|
-
|
|
134
|
-
data: { where: this.config.where },
|
|
124
|
+
this.log("warn", `Pattern does not match "${location.href}"`, {
|
|
125
|
+
where: this.config.where,
|
|
135
126
|
});
|
|
136
127
|
} else if (
|
|
137
128
|
this.config.what === "scrape" &&
|
|
138
129
|
document.querySelector('html[class*="translated-"]')
|
|
139
130
|
) {
|
|
140
131
|
this.log(
|
|
141
|
-
"
|
|
142
|
-
|
|
132
|
+
"warn",
|
|
133
|
+
"This page has been translated by the browser, and will be excluded"
|
|
143
134
|
);
|
|
144
135
|
} else if (this.config.when) {
|
|
145
136
|
try {
|
|
@@ -161,9 +152,8 @@ class ChoreographCreatePixel {
|
|
|
161
152
|
}
|
|
162
153
|
});
|
|
163
154
|
} catch (error) {
|
|
164
|
-
this.log(error.message, {
|
|
165
|
-
|
|
166
|
-
data: { when: { elements: this.config.when.elements } },
|
|
155
|
+
this.log("error", error.message, {
|
|
156
|
+
when: { elements: this.config.when.elements },
|
|
167
157
|
});
|
|
168
158
|
}
|
|
169
159
|
} else {
|
|
@@ -185,9 +175,8 @@ class ChoreographCreatePixel {
|
|
|
185
175
|
data[fieldName] = data[fieldName](element);
|
|
186
176
|
} catch (error) {
|
|
187
177
|
if (!this.config.optionalFields.includes(fieldName)) {
|
|
188
|
-
this.log(error.message, {
|
|
189
|
-
|
|
190
|
-
data: { which: { [fieldName]: this.config.which[fieldName] } },
|
|
178
|
+
this.log("error", error.message, {
|
|
179
|
+
which: { [fieldName]: this.config.which[fieldName] },
|
|
191
180
|
});
|
|
192
181
|
|
|
193
182
|
hasErrors = true;
|
|
@@ -200,9 +189,8 @@ class ChoreographCreatePixel {
|
|
|
200
189
|
|
|
201
190
|
if (data[fieldName] == null || data[fieldName] === "")
|
|
202
191
|
if (!this.config.optionalFields.includes(fieldName)) {
|
|
203
|
-
this.log("This required field's value is empty", {
|
|
204
|
-
|
|
205
|
-
data: { which: { [fieldName]: data[fieldName] } },
|
|
192
|
+
this.log("error", "This required field's value is empty", {
|
|
193
|
+
which: { [fieldName]: data[fieldName] },
|
|
206
194
|
});
|
|
207
195
|
|
|
208
196
|
hasErrors = true;
|
|
@@ -219,9 +207,8 @@ class ChoreographCreatePixel {
|
|
|
219
207
|
else this.send(newData);
|
|
220
208
|
});
|
|
221
209
|
} catch (error) {
|
|
222
|
-
this.log(error.message, {
|
|
223
|
-
|
|
224
|
-
data: { beforeSend: this.config.beforeSend },
|
|
210
|
+
this.log("error", error.message, {
|
|
211
|
+
beforeSend: this.config.beforeSend,
|
|
225
212
|
});
|
|
226
213
|
}
|
|
227
214
|
|
|
@@ -265,56 +252,53 @@ class ChoreographCreatePixel {
|
|
|
265
252
|
}
|
|
266
253
|
: null
|
|
267
254
|
)
|
|
268
|
-
.then((response) =>
|
|
269
|
-
if (!this.config.debug) return;
|
|
270
|
-
|
|
255
|
+
.then((response) =>
|
|
271
256
|
response
|
|
272
257
|
.json()
|
|
273
258
|
.then((result) => {
|
|
274
259
|
if (response.ok) {
|
|
275
|
-
this.log("Successful, with warnings. Details:",
|
|
276
|
-
level: "warn",
|
|
277
|
-
data: result,
|
|
278
|
-
});
|
|
260
|
+
this.log("warn", "Successful, with warnings. Details:", result);
|
|
279
261
|
|
|
280
262
|
try {
|
|
281
263
|
this.config.afterSend(data);
|
|
282
264
|
} catch (error) {
|
|
283
|
-
this.log(error.message, {
|
|
284
|
-
|
|
285
|
-
data: { afterSend: this.config.afterSend },
|
|
265
|
+
this.log("error", error.message, {
|
|
266
|
+
afterSend: this.config.afterSend,
|
|
286
267
|
});
|
|
287
268
|
}
|
|
288
269
|
} else
|
|
289
270
|
this.log(
|
|
271
|
+
"error",
|
|
290
272
|
`Failed: ${response.status} (${response.statusText}). Details:`,
|
|
291
|
-
|
|
273
|
+
result
|
|
292
274
|
);
|
|
275
|
+
|
|
276
|
+
this.logs.length = 0;
|
|
293
277
|
})
|
|
294
278
|
.catch(() => {
|
|
295
279
|
if (response.ok) {
|
|
296
|
-
this.log("
|
|
280
|
+
this.log("success", "Successful!", data);
|
|
297
281
|
|
|
298
282
|
try {
|
|
299
283
|
this.config.afterSend(data);
|
|
300
284
|
} catch (error) {
|
|
301
|
-
this.log(error.message, {
|
|
302
|
-
|
|
303
|
-
data: { afterSend: this.config.afterSend },
|
|
285
|
+
this.log("error", error.message, {
|
|
286
|
+
afterSend: this.config.afterSend,
|
|
304
287
|
});
|
|
305
288
|
}
|
|
306
289
|
} else
|
|
307
290
|
this.log(
|
|
291
|
+
"error",
|
|
308
292
|
`Failed: ${response.status} (${response.statusText}). Details:`,
|
|
309
|
-
|
|
293
|
+
response
|
|
310
294
|
);
|
|
311
|
-
|
|
312
|
-
|
|
295
|
+
|
|
296
|
+
this.logs.length = 0;
|
|
297
|
+
})
|
|
298
|
+
)
|
|
313
299
|
.catch((error) => {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
level: "error",
|
|
317
|
-
});
|
|
300
|
+
this.log("error", `Failed: ${error.message}`);
|
|
301
|
+
this.logs.length = 0;
|
|
318
302
|
});
|
|
319
303
|
}
|
|
320
304
|
}
|
package/dist/bundle.iife.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
/*! choreograph-create-pixel v0.1.
|
|
2
|
-
var ChoreographCreatePixel=function(){"use strict";function e(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,o)}return n}function t(t){for(var n=1;n<arguments.length;n++){var o=null!=arguments[n]?arguments[n]:{};n%2?e(Object(o),!0).forEach((function(e){
|
|
1
|
+
/*! choreograph-create-pixel v0.1.12 2022/09/20 */
|
|
2
|
+
var ChoreographCreatePixel=function(){"use strict";function e(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,o)}return n}function t(t){for(var n=1;n<arguments.length;n++){var o=null!=arguments[n]?arguments[n]:{};n%2?e(Object(o),!0).forEach((function(e){r(t,e,o[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(o)):e(Object(o)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(o,e))}))}return t}function n(e){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},n(e)}function o(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var i=function(){function e(n){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.previouslyScrapedJsonString=null,this.logs=[],this.settings={colors:{error:"#f44336",warn:"#ffa726",success:"#66bb6a"},icons:{scrape:"๐",view:"๐",basket:"๐",purchase:"๐งพ"},titleCss:"font:700 80% HelveticaNeue;margin:12px 0 8px",messageCss:"font:500 120% HelveticaNeue;margin-bottom:12px",eventTypes:{view:"product-viewed",basket:"product-basketed",purchase:"product-purchased"}},this.config=t({debug:/(pixel|lemonpi)_debug/i.test(location.href),beforeSend:function(e,t){return t(e)},afterSend:function(){},optionalFields:[]},n),this.validateConfig()&&this.cycle()}var i,s,c;return i=e,s=[{key:"log",value:function(e,t){var n,o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:null;if(this.config.debug&&!this.logs.includes(t)){var r=["%c".concat(this.settings.icons[this.config.what]," ").concat(this.config.what.toUpperCase()," PIXEL DEBUG%c\n%c").concat(t),this.settings.titleCss,"",this.settings.colors[e]?"".concat(this.settings.messageCss,";color:").concat(this.settings.colors[e]):this.settings.messageCss];o&&r.push(o),(n=console).info.apply(n,r),this.logs.push(t)}}},{key:"validateConfig",value:function(){if("number"!=typeof this.config.who)this.log("error","Please use a number",{who:this.config.who});else if(["scrape","view","basket","purchase"].includes(this.config.what))if(this.config.where instanceof RegExp){if("object"===n(this.config.which)&&this.config.which.sku)return!0;this.log("error","Please provide an SKU",{which:this.config.which})}else this.log("error","Please use a regular expression",{where:this.config.where});else this.log("error","Please use scrape, view, basket, or purchase",{what:this.config.what})}},{key:"cycle",value:function(){var e=this;if(this.config.where.test(location.href))if("scrape"===this.config.what&&document.querySelector('html[class*="translated-"]'))this.log("warn","This page has been translated by the browser, and will be excluded");else if(this.config.when)try{var t=this.config.when.elements();t.forEach||(t=[t]),t.forEach((function(t){t.hasAttribute("choreograph-".concat(e.config.when.listener))||(t.addEventListener(e.config.when.listener,(function(){return e.scrape(t)})),t.setAttribute("choreograph-".concat(e.config.when.listener),""))}))}catch(e){this.log("error",e.message,{when:{elements:this.config.when.elements}})}else this.scrape();else this.log("warn",'Pattern does not match "'.concat(location.href,'"'),{where:this.config.where});setTimeout((function(){return e.cycle()}),750)}},{key:"scrape",value:function(e){var n=this,o={},i=!1;Object.keys(this.config.which).forEach((function(t){if(o[t]=n.config.which[t],"function"==typeof o[t])try{o[t]=o[t](e)}catch(e){n.config.optionalFields.includes(t)?delete o[t]:(n.log("error",e.message,{which:r({},t,n.config.which[t])}),i=!0)}"string"==typeof o[t]&&(o[t]=o[t].replace(/\s+/g," ").trim()),null!=o[t]&&""!==o[t]||(n.config.optionalFields.includes(t)?o[t]=null:(n.log("error","This required field's value is empty",{which:r({},t,o[t])}),i=!0))}));var s=JSON.stringify(o);if(!i&&this.previouslyScrapedJsonString!==s){try{this.config.beforeSend(o,(function(e){"scrape"!==n.config.what&&Array.isArray(e.sku)?e.sku.forEach((function(o){return n.send(t(t({},e),{},{sku:o}))})):n.send(e)}))}catch(e){this.log("error",e.message,{beforeSend:this.config.beforeSend})}this.previouslyScrapedJsonString=s,this.logs.length=0}}},{key:"send",value:function(e){var n=this,o=t({},e);delete o.sku;var r="scrape"===this.config.what?"https://d.lemonpi.io/scrapes".concat(this.config.debug?"?validate=true":""):"https://d.lemonpi.io/a/".concat(this.config.who,"/product/event?e=").concat(encodeURIComponent(JSON.stringify({"event-type":this.settings.eventTypes[this.config.what],sku:e.sku})));"scrape"===this.config.what||this.config.debug?fetch(r,"scrape"===this.config.what?{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({"advertiser-id":this.config.who,sku:e.sku,fields:o})}:null).then((function(t){return t.json().then((function(o){if(t.ok){n.log("warn","Successful, with warnings. Details:",o);try{n.config.afterSend(e)}catch(e){n.log("error",e.message,{afterSend:n.config.afterSend})}}else n.log("error","Failed: ".concat(t.status," (").concat(t.statusText,"). Details:"),o);n.logs.length=0})).catch((function(){if(t.ok){n.log("success","Successful!",e);try{n.config.afterSend(e)}catch(e){n.log("error",e.message,{afterSend:n.config.afterSend})}}else n.log("error","Failed: ".concat(t.status," (").concat(t.statusText,"). Details:"),t);n.logs.length=0}))})).catch((function(e){n.log("error","Failed: ".concat(e.message)),n.logs.length=0})):(new Image).src=r}}],c=[{key:"getUrl",value:function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.allowedQueryParameters,n=void 0===t?[]:t,o=e.allowHash,r=void 0!==o&&o,i="".concat(location.protocol,"//").concat(location.host).concat(location.pathname),s=new URLSearchParams(location.search);return n.reduce((function(e,t){var n=e?"&":"?",o=encodeURI(t),r=s.get(t);return null!=r?(i+="".concat(n).concat(o).concat(""===r?"":"=".concat(encodeURI(r))),!0):e}),!1),r&&(i+=location.hash),i}},{key:"getAllPathSegments",value:function(){return location.pathname.split("/").filter((function(e){return e})).map((function(e){return decodeURI(e)}))}},{key:"getPathSegment",value:function(e){return this.getAllPathSegments()[e]}},{key:"getAllQueryParameters",value:function(){return location.search.replace(/^\?/,"").split("&").filter((function(e){return e})).reduce((function(e,n){return t(t({},e),{},r({},decodeURI(n.split("=")[0]),decodeURI(n.split("=")[1]||"")))}),{})}},{key:"getQueryParameter",value:function(e){return this.getAllQueryParameters()[e]}}],s&&o(i.prototype,s),c&&o(i,c),Object.defineProperty(i,"prototype",{writable:!1}),e}();return i}();
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "choreograph-create-pixel",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"author": "Rick Stevens <rick.stevens@choreograph.com> (https://www.lemonpi.io/)",
|
|
5
|
-
"repository": "
|
|
5
|
+
"repository": "gitlab:GreenhouseGroup/lemonpi/solutions/choreograph-create-pixel",
|
|
6
6
|
"license": "ISC",
|
|
7
7
|
"main": "dist/bundle.cjs.js",
|
|
8
8
|
"module": "dist/bundle.esm.js",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"format": "prettier --ignore-path .gitignore --check . '!**/*.{js,jsx,vue}'",
|
|
17
17
|
"build": "rollup -c",
|
|
18
18
|
"dev": "rollup -cw",
|
|
19
|
-
"prepare": "npm run
|
|
19
|
+
"prepare": "npm run build"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@babel/core": "^7.19.1",
|