choreograph-create-pixel 0.1.9 โ†’ 0.1.11

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.
@@ -1,4 +1,4 @@
1
- /*! choreograph-create-pixel v0.1.9 2022/09/19 */
1
+ /*! choreograph-create-pixel v0.1.11 2022/09/20 */
2
2
  'use strict';
3
3
 
4
4
  class ChoreographCreatePixel {
@@ -9,10 +9,8 @@ class ChoreographCreatePixel {
9
9
  this.settings = {
10
10
  colors: { error: "#f44336", warn: "#ffa726", success: "#66bb6a" },
11
11
  icons: { scrape: "๐Ÿ”", view: "๐Ÿ‘€", basket: "๐Ÿ›’", purchase: "๐Ÿงพ" },
12
- levels: { error: "โ›”๏ธ", warn: "โš ๏ธ", success: "โœ…", info: "โ„น๏ธ" },
13
- titleCss: "font:700 80% HelveticaNeue;margin:12px 0 6px",
14
- iconCss: "font:HelveticaNeue",
15
- messageCss: "font:120% HelveticaNeue;margin-bottom:12px",
12
+ titleCss: "font:700 80% HelveticaNeue;margin:12px 0 8px",
13
+ messageCss: "font:500 120% HelveticaNeue;margin-bottom:12px",
16
14
  eventTypes: {
17
15
  view: "product-viewed",
18
16
  basket: "product-basketed",
@@ -86,15 +84,15 @@ class ChoreographCreatePixel {
86
84
  return this.getAllQueryParameters()[key];
87
85
  }
88
86
 
89
- log(message, { level = "info", data = null }) {
87
+ log(level, message, data = null) {
90
88
  if (!this.config.debug || this.logs.includes(message)) return;
91
89
 
92
90
  const args = [
93
- `%cCHOREOGRAPH ${this.config.what.toUpperCase()} PIXEL%c ${
91
+ `%c${
94
92
  this.settings.icons[this.config.what]
95
- }\n${this.settings.levels[level]} %c${message}`,
93
+ } ${this.config.what.toUpperCase()} PIXEL DEBUG%c\n%c${message}`,
96
94
  this.settings.titleCss,
97
- this.settings.iconCss,
95
+ "",
98
96
  this.settings.colors[level]
99
97
  ? `${this.settings.messageCss};color:${this.settings.colors[level]}`
100
98
  : this.settings.messageCss,
@@ -107,43 +105,34 @@ class ChoreographCreatePixel {
107
105
 
108
106
  validateConfig() {
109
107
  if (typeof this.config.who !== "number")
110
- this.log("Please use a number", {
111
- level: "error",
112
- data: { who: this.config.who },
113
- });
108
+ this.log("error", "Please use a number", { who: this.config.who });
114
109
  else if (
115
110
  !["scrape", "view", "basket", "purchase"].includes(this.config.what)
116
111
  )
117
- this.log("Please use scrape, view, basket, or purchase", {
118
- level: "error",
119
- data: { what: this.config.what },
112
+ this.log("error", "Please use scrape, view, basket, or purchase", {
113
+ what: this.config.what,
120
114
  });
121
115
  else if (!(this.config.where instanceof RegExp))
122
- this.log("Please use a regular expression", {
123
- level: "error",
124
- data: { where: this.config.where },
116
+ this.log("error", "Please use a regular expression", {
117
+ where: this.config.where,
125
118
  });
126
119
  else if (typeof this.config.which !== "object" || !this.config.which.sku)
127
- this.log("Please provide an SKU", {
128
- level: "error",
129
- data: { which: this.config.which },
130
- });
120
+ this.log("error", "Please provide an SKU", { which: this.config.which });
131
121
  else return true;
132
122
  }
133
123
 
134
124
  cycle() {
135
125
  if (!this.config.where.test(location.href)) {
136
- this.log(`Pattern does not match ${location.href}`, {
137
- level: "warn",
138
- data: { where: this.config.where },
126
+ this.log("warn", `Pattern does not match "${location.href}"`, {
127
+ where: this.config.where,
139
128
  });
140
129
  } else if (
141
130
  this.config.what === "scrape" &&
142
131
  document.querySelector('html[class*="translated-"]')
143
132
  ) {
144
133
  this.log(
145
- "This page has been translated by the browser, and will be excluded",
146
- { level: "warn" }
134
+ "warn",
135
+ "This page has been translated by the browser, and will be excluded"
147
136
  );
148
137
  } else if (this.config.when) {
149
138
  try {
@@ -165,9 +154,8 @@ class ChoreographCreatePixel {
165
154
  }
166
155
  });
167
156
  } catch (error) {
168
- this.log(error.message, {
169
- level: "error",
170
- data: { when: { elements: this.config.when.elements } },
157
+ this.log("error", error.message, {
158
+ when: { elements: this.config.when.elements },
171
159
  });
172
160
  }
173
161
  } else {
@@ -189,9 +177,8 @@ class ChoreographCreatePixel {
189
177
  data[fieldName] = data[fieldName](element);
190
178
  } catch (error) {
191
179
  if (!this.config.optionalFields.includes(fieldName)) {
192
- this.log(error.message, {
193
- level: "error",
194
- data: { which: { [fieldName]: this.config.which[fieldName] } },
180
+ this.log("error", error.message, {
181
+ which: { [fieldName]: this.config.which[fieldName] },
195
182
  });
196
183
 
197
184
  hasErrors = true;
@@ -204,9 +191,8 @@ class ChoreographCreatePixel {
204
191
 
205
192
  if (data[fieldName] == null || data[fieldName] === "")
206
193
  if (!this.config.optionalFields.includes(fieldName)) {
207
- this.log("This required field's value is empty", {
208
- level: "error",
209
- data: { which: { [fieldName]: data[fieldName] } },
194
+ this.log("error", "This required field's value is empty", {
195
+ which: { [fieldName]: data[fieldName] },
210
196
  });
211
197
 
212
198
  hasErrors = true;
@@ -216,8 +202,6 @@ class ChoreographCreatePixel {
216
202
  const jsonString = JSON.stringify(data);
217
203
 
218
204
  if (!hasErrors && this.previouslyScrapedJsonString !== jsonString) {
219
- this.log("Scraped data:", { data });
220
-
221
205
  try {
222
206
  this.config.beforeSend(data, (newData) => {
223
207
  if (this.config.what !== "scrape" && Array.isArray(newData.sku))
@@ -225,9 +209,8 @@ class ChoreographCreatePixel {
225
209
  else this.send(newData);
226
210
  });
227
211
  } catch (error) {
228
- this.log(error.message, {
229
- level: "error",
230
- data: { beforeSend: this.config.beforeSend },
212
+ this.log("error", error.message, {
213
+ beforeSend: this.config.beforeSend,
231
214
  });
232
215
  }
233
216
 
@@ -271,56 +254,53 @@ class ChoreographCreatePixel {
271
254
  }
272
255
  : null
273
256
  )
274
- .then((response) => {
275
- if (!this.config.debug) return;
276
-
257
+ .then((response) =>
277
258
  response
278
259
  .json()
279
260
  .then((result) => {
280
261
  if (response.ok) {
281
- this.log("Successful, with warnings. Details:", {
282
- level: "warn",
283
- data: result,
284
- });
262
+ this.log("warn", "Successful, with warnings. Details:", result);
285
263
 
286
264
  try {
287
265
  this.config.afterSend(data);
288
266
  } catch (error) {
289
- this.log(error.message, {
290
- level: "error",
291
- data: { afterSend: this.config.afterSend },
267
+ this.log("error", error.message, {
268
+ afterSend: this.config.afterSend,
292
269
  });
293
270
  }
294
271
  } else
295
272
  this.log(
273
+ "error",
296
274
  `Failed: ${response.status} (${response.statusText}). Details:`,
297
- { level: "error", data: result }
275
+ result
298
276
  );
277
+
278
+ this.logs.length = 0;
299
279
  })
300
280
  .catch(() => {
301
281
  if (response.ok) {
302
- this.log("Successful!", { level: "success", data });
282
+ this.log("success", "Successful!", data);
303
283
 
304
284
  try {
305
285
  this.config.afterSend(data);
306
286
  } catch (error) {
307
- this.log(error.message, {
308
- level: "error",
309
- data: { afterSend: this.config.afterSend },
287
+ this.log("error", error.message, {
288
+ afterSend: this.config.afterSend,
310
289
  });
311
290
  }
312
291
  } else
313
292
  this.log(
293
+ "error",
314
294
  `Failed: ${response.status} (${response.statusText}). Details:`,
315
- { level: "error", data: response }
295
+ response
316
296
  );
317
- });
318
- })
297
+
298
+ this.logs.length = 0;
299
+ })
300
+ )
319
301
  .catch((error) => {
320
- if (this.config.debug)
321
- this.log(`Failed: ${error.message}`, {
322
- level: "error",
323
- });
302
+ this.log("error", `Failed: ${error.message}`);
303
+ this.logs.length = 0;
324
304
  });
325
305
  }
326
306
  }
@@ -1,4 +1,4 @@
1
- /*! choreograph-create-pixel v0.1.9 2022/09/19 */
1
+ /*! choreograph-create-pixel v0.1.11 2022/09/20 */
2
2
  class ChoreographCreatePixel {
3
3
  constructor(userConfig) {
4
4
  this.previouslyScrapedJsonString = null;
@@ -7,10 +7,8 @@ class ChoreographCreatePixel {
7
7
  this.settings = {
8
8
  colors: { error: "#f44336", warn: "#ffa726", success: "#66bb6a" },
9
9
  icons: { scrape: "๐Ÿ”", view: "๐Ÿ‘€", basket: "๐Ÿ›’", purchase: "๐Ÿงพ" },
10
- levels: { error: "โ›”๏ธ", warn: "โš ๏ธ", success: "โœ…", info: "โ„น๏ธ" },
11
- titleCss: "font:700 80% HelveticaNeue;margin:12px 0 6px",
12
- iconCss: "font:HelveticaNeue",
13
- messageCss: "font:120% HelveticaNeue;margin-bottom:12px",
10
+ titleCss: "font:700 80% HelveticaNeue;margin:12px 0 8px",
11
+ messageCss: "font:500 120% HelveticaNeue;margin-bottom:12px",
14
12
  eventTypes: {
15
13
  view: "product-viewed",
16
14
  basket: "product-basketed",
@@ -84,15 +82,15 @@ class ChoreographCreatePixel {
84
82
  return this.getAllQueryParameters()[key];
85
83
  }
86
84
 
87
- log(message, { level = "info", data = null }) {
85
+ log(level, message, data = null) {
88
86
  if (!this.config.debug || this.logs.includes(message)) return;
89
87
 
90
88
  const args = [
91
- `%cCHOREOGRAPH ${this.config.what.toUpperCase()} PIXEL%c ${
89
+ `%c${
92
90
  this.settings.icons[this.config.what]
93
- }\n${this.settings.levels[level]} %c${message}`,
91
+ } ${this.config.what.toUpperCase()} PIXEL DEBUG%c\n%c${message}`,
94
92
  this.settings.titleCss,
95
- this.settings.iconCss,
93
+ "",
96
94
  this.settings.colors[level]
97
95
  ? `${this.settings.messageCss};color:${this.settings.colors[level]}`
98
96
  : this.settings.messageCss,
@@ -105,43 +103,34 @@ class ChoreographCreatePixel {
105
103
 
106
104
  validateConfig() {
107
105
  if (typeof this.config.who !== "number")
108
- this.log("Please use a number", {
109
- level: "error",
110
- data: { who: this.config.who },
111
- });
106
+ this.log("error", "Please use a number", { who: this.config.who });
112
107
  else if (
113
108
  !["scrape", "view", "basket", "purchase"].includes(this.config.what)
114
109
  )
115
- this.log("Please use scrape, view, basket, or purchase", {
116
- level: "error",
117
- data: { what: this.config.what },
110
+ this.log("error", "Please use scrape, view, basket, or purchase", {
111
+ what: this.config.what,
118
112
  });
119
113
  else if (!(this.config.where instanceof RegExp))
120
- this.log("Please use a regular expression", {
121
- level: "error",
122
- data: { where: this.config.where },
114
+ this.log("error", "Please use a regular expression", {
115
+ where: this.config.where,
123
116
  });
124
117
  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
- });
118
+ this.log("error", "Please provide an SKU", { which: this.config.which });
129
119
  else return true;
130
120
  }
131
121
 
132
122
  cycle() {
133
123
  if (!this.config.where.test(location.href)) {
134
- this.log(`Pattern does not match ${location.href}`, {
135
- level: "warn",
136
- data: { where: this.config.where },
124
+ this.log("warn", `Pattern does not match "${location.href}"`, {
125
+ where: this.config.where,
137
126
  });
138
127
  } else if (
139
128
  this.config.what === "scrape" &&
140
129
  document.querySelector('html[class*="translated-"]')
141
130
  ) {
142
131
  this.log(
143
- "This page has been translated by the browser, and will be excluded",
144
- { level: "warn" }
132
+ "warn",
133
+ "This page has been translated by the browser, and will be excluded"
145
134
  );
146
135
  } else if (this.config.when) {
147
136
  try {
@@ -163,9 +152,8 @@ class ChoreographCreatePixel {
163
152
  }
164
153
  });
165
154
  } catch (error) {
166
- this.log(error.message, {
167
- level: "error",
168
- data: { when: { elements: this.config.when.elements } },
155
+ this.log("error", error.message, {
156
+ when: { elements: this.config.when.elements },
169
157
  });
170
158
  }
171
159
  } else {
@@ -187,9 +175,8 @@ class ChoreographCreatePixel {
187
175
  data[fieldName] = data[fieldName](element);
188
176
  } catch (error) {
189
177
  if (!this.config.optionalFields.includes(fieldName)) {
190
- this.log(error.message, {
191
- level: "error",
192
- data: { which: { [fieldName]: this.config.which[fieldName] } },
178
+ this.log("error", error.message, {
179
+ which: { [fieldName]: this.config.which[fieldName] },
193
180
  });
194
181
 
195
182
  hasErrors = true;
@@ -202,9 +189,8 @@ class ChoreographCreatePixel {
202
189
 
203
190
  if (data[fieldName] == null || data[fieldName] === "")
204
191
  if (!this.config.optionalFields.includes(fieldName)) {
205
- this.log("This required field's value is empty", {
206
- level: "error",
207
- data: { which: { [fieldName]: data[fieldName] } },
192
+ this.log("error", "This required field's value is empty", {
193
+ which: { [fieldName]: data[fieldName] },
208
194
  });
209
195
 
210
196
  hasErrors = true;
@@ -214,8 +200,6 @@ class ChoreographCreatePixel {
214
200
  const jsonString = JSON.stringify(data);
215
201
 
216
202
  if (!hasErrors && this.previouslyScrapedJsonString !== jsonString) {
217
- this.log("Scraped data:", { data });
218
-
219
203
  try {
220
204
  this.config.beforeSend(data, (newData) => {
221
205
  if (this.config.what !== "scrape" && Array.isArray(newData.sku))
@@ -223,9 +207,8 @@ class ChoreographCreatePixel {
223
207
  else this.send(newData);
224
208
  });
225
209
  } catch (error) {
226
- this.log(error.message, {
227
- level: "error",
228
- data: { beforeSend: this.config.beforeSend },
210
+ this.log("error", error.message, {
211
+ beforeSend: this.config.beforeSend,
229
212
  });
230
213
  }
231
214
 
@@ -269,56 +252,53 @@ class ChoreographCreatePixel {
269
252
  }
270
253
  : null
271
254
  )
272
- .then((response) => {
273
- if (!this.config.debug) return;
274
-
255
+ .then((response) =>
275
256
  response
276
257
  .json()
277
258
  .then((result) => {
278
259
  if (response.ok) {
279
- this.log("Successful, with warnings. Details:", {
280
- level: "warn",
281
- data: result,
282
- });
260
+ this.log("warn", "Successful, with warnings. Details:", result);
283
261
 
284
262
  try {
285
263
  this.config.afterSend(data);
286
264
  } catch (error) {
287
- this.log(error.message, {
288
- level: "error",
289
- data: { afterSend: this.config.afterSend },
265
+ this.log("error", error.message, {
266
+ afterSend: this.config.afterSend,
290
267
  });
291
268
  }
292
269
  } else
293
270
  this.log(
271
+ "error",
294
272
  `Failed: ${response.status} (${response.statusText}). Details:`,
295
- { level: "error", data: result }
273
+ result
296
274
  );
275
+
276
+ this.logs.length = 0;
297
277
  })
298
278
  .catch(() => {
299
279
  if (response.ok) {
300
- this.log("Successful!", { level: "success", data });
280
+ this.log("success", "Successful!", data);
301
281
 
302
282
  try {
303
283
  this.config.afterSend(data);
304
284
  } catch (error) {
305
- this.log(error.message, {
306
- level: "error",
307
- data: { afterSend: this.config.afterSend },
285
+ this.log("error", error.message, {
286
+ afterSend: this.config.afterSend,
308
287
  });
309
288
  }
310
289
  } else
311
290
  this.log(
291
+ "error",
312
292
  `Failed: ${response.status} (${response.statusText}). Details:`,
313
- { level: "error", data: response }
293
+ response
314
294
  );
315
- });
316
- })
295
+
296
+ this.logs.length = 0;
297
+ })
298
+ )
317
299
  .catch((error) => {
318
- if (this.config.debug)
319
- this.log(`Failed: ${error.message}`, {
320
- level: "error",
321
- });
300
+ this.log("error", `Failed: ${error.message}`);
301
+ this.logs.length = 0;
322
302
  });
323
303
  }
324
304
  }
@@ -1,2 +1,2 @@
1
- /*! choreograph-create-pixel v0.1.9 2022/09/19 */
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){i(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 i(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var r=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:"๐Ÿงพ"},levels:{error:"โ›”๏ธ",warn:"โš ๏ธ",success:"โœ…",info:"โ„น๏ธ"},titleCss:"font:700 80% HelveticaNeue;margin:12px 0 6px",iconCss:"font:HelveticaNeue",messageCss:"font: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 r,s,c;return r=e,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,i=void 0!==o&&o,r="".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),i=s.get(t);return null!=i?(r+="".concat(n).concat(o).concat(""===i?"":"=".concat(encodeURI(i))),!0):e}),!1),i&&(r+=location.hash),r}},{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),{},i({},decodeURI(n.split("=")[0]),decodeURI(n.split("=")[1]||"")))}),{})}},{key:"getQueryParameter",value:function(e){return this.getAllQueryParameters()[e]}}],(s=[{key:"log",value:function(e,t){var n,o=t.level,i=void 0===o?"info":o,r=t.data,s=void 0===r?null:r;if(this.config.debug&&!this.logs.includes(e)){var c=["%cCHOREOGRAPH ".concat(this.config.what.toUpperCase()," PIXEL%c ").concat(this.settings.icons[this.config.what],"\n").concat(this.settings.levels[i]," %c").concat(e),this.settings.titleCss,this.settings.iconCss,this.settings.colors[i]?"".concat(this.settings.messageCss,";color:").concat(this.settings.colors[i]):this.settings.messageCss];s&&c.push(s),(n=console).info.apply(n,c),this.logs.push(e)}}},{key:"validateConfig",value:function(){if("number"!=typeof this.config.who)this.log("Please use a number",{level:"error",data:{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("Please provide an SKU",{level:"error",data:{which:this.config.which}})}else this.log("Please use a regular expression",{level:"error",data:{where:this.config.where}});else this.log("Please use scrape, view, basket, or purchase",{level:"error",data:{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("This page has been translated by the browser, and will be excluded",{level:"warn"});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(e.message,{level:"error",data:{when:{elements:this.config.when.elements}}})}else this.scrape();else this.log("Pattern does not match ".concat(location.href),{level:"warn",data:{where:this.config.where}});setTimeout((function(){return e.cycle()}),750)}},{key:"scrape",value:function(e){var n=this,o={},r=!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(e.message,{level:"error",data:{which:i({},t,n.config.which[t])}}),r=!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("This required field's value is empty",{level:"error",data:{which:i({},t,o[t])}}),r=!0))}));var s=JSON.stringify(o);if(!r&&this.previouslyScrapedJsonString!==s){this.log("Scraped data:",{data:o});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(e.message,{level:"error",data:{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 i="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(i,"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){n.config.debug&&t.json().then((function(o){if(t.ok){n.log("Successful, with warnings. Details:",{level:"warn",data:o});try{n.config.afterSend(e)}catch(e){n.log(e.message,{level:"error",data:{afterSend:n.config.afterSend}})}}else n.log("Failed: ".concat(t.status," (").concat(t.statusText,"). Details:"),{level:"error",data:o})})).catch((function(){if(t.ok){n.log("Successful!",{level:"success",data:e});try{n.config.afterSend(e)}catch(e){n.log(e.message,{level:"error",data:{afterSend:n.config.afterSend}})}}else n.log("Failed: ".concat(t.status," (").concat(t.statusText,"). Details:"),{level:"error",data:t})}))})).catch((function(e){n.config.debug&&n.log("Failed: ".concat(e.message),{level:"error"})})):(new Image).src=i}}])&&o(r.prototype,s),c&&o(r,c),Object.defineProperty(r,"prototype",{writable:!1}),e}();return r}();
1
+ /*! choreograph-create-pixel v0.1.11 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,6 +1,6 @@
1
1
  {
2
2
  "name": "choreograph-create-pixel",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "author": "Rick Stevens <rick.stevens@choreograph.com> (https://www.lemonpi.io/)",
5
5
  "repository": "github:rick-stevens/choreograph-create-pixel",
6
6
  "license": "ISC",