choreograph-create-pixel 0.1.6 โ 0.1.8
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 +8 -14
- package/dist/bundle.cjs.js +21 -23
- package/dist/bundle.esm.js +21 -23
- package/dist/bundle.iife.js +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -82,14 +82,10 @@ new Pixel({
|
|
|
82
82
|
// Which data should be scraped?
|
|
83
83
|
which: {
|
|
84
84
|
// Data layer example
|
|
85
|
-
sku:
|
|
86
|
-
return window.dataLayer[0].product.sku;
|
|
87
|
-
},
|
|
85
|
+
sku: () => window.dataLayer[0].product.sku,
|
|
88
86
|
|
|
89
87
|
// DOM example
|
|
90
|
-
name:
|
|
91
|
-
return document.querySelector("#product-name").innerText;
|
|
92
|
-
},
|
|
88
|
+
name: () => document.querySelector("#product-name").innerText,
|
|
93
89
|
|
|
94
90
|
// Helper example
|
|
95
91
|
url: Pixel.getUrl,
|
|
@@ -103,9 +99,7 @@ new Pixel({
|
|
|
103
99
|
where: /example\.com\/products\/\d/,
|
|
104
100
|
|
|
105
101
|
which: {
|
|
106
|
-
sku:
|
|
107
|
-
return window.dataLayer[0].product.sku;
|
|
108
|
-
},
|
|
102
|
+
sku: () => window.dataLayer[0].product.sku,
|
|
109
103
|
},
|
|
110
104
|
});
|
|
111
105
|
```
|
|
@@ -183,14 +177,14 @@ Enable the console debugger by adding `?pixel_debug` or `#pixel_debug` to the pa
|
|
|
183
177
|
|
|
184
178
|
## Static methods (helpers)
|
|
185
179
|
|
|
186
|
-
### `getUrl({
|
|
180
|
+
### `getUrl({ allowedQueryParameters, allowHash })`
|
|
187
181
|
|
|
188
182
|
Returns the bare page URL without query parameters or location hash. This is recommended to prevent scraping unwanted UTM tagging.
|
|
189
183
|
|
|
190
|
-
| Property
|
|
191
|
-
|
|
|
192
|
-
| `
|
|
193
|
-
| `allowHash`
|
|
184
|
+
| Property | Type | Description | Default |
|
|
185
|
+
| ------------------------ | ------- | --------------------------------------------------------------- | ------- |
|
|
186
|
+
| `allowedQueryParameters` | Array | Explicitly allow query parameters in the resulting URL. | `[]` |
|
|
187
|
+
| `allowHash` | Boolean | Wether or not to include the location hash (`#foo`) of the URL. | `false` |
|
|
194
188
|
|
|
195
189
|
### `getAllPathSegments()`
|
|
196
190
|
|
package/dist/bundle.cjs.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! choreograph-create-pixel v0.1.
|
|
1
|
+
/*! choreograph-create-pixel v0.1.8 2022/09/19 */
|
|
2
2
|
'use strict';
|
|
3
3
|
|
|
4
4
|
class ChoreographCreatePixel {
|
|
@@ -31,31 +31,27 @@ class ChoreographCreatePixel {
|
|
|
31
31
|
if (this.validateConfig()) this.cycle();
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
static getUrl(
|
|
34
|
+
static getUrl({ allowedQueryParameters = [], allowHash = false } = {}) {
|
|
35
35
|
let url = `${location.protocol}//${location.host}${location.pathname}`;
|
|
36
36
|
const parameters = new URLSearchParams(location.search);
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
allowedQueryParameters.reduce((paramAdded, parameter) => {
|
|
39
|
+
const separator = paramAdded ? "&" : "?";
|
|
40
|
+
const key = encodeURI(parameter);
|
|
41
|
+
const value = parameters.get(parameter);
|
|
40
42
|
|
|
41
|
-
if (
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const value = parameters.get(parameter);
|
|
43
|
+
if (value != null) {
|
|
44
|
+
url += `${separator}${key}${
|
|
45
|
+
value === "" ? "" : `=${encodeURI(value)}`
|
|
46
|
+
}`;
|
|
46
47
|
|
|
47
|
-
|
|
48
|
-
url += `${separator}${key}=${encodeURI(value)}`;
|
|
49
|
-
paramAdded = true;
|
|
50
|
-
}
|
|
51
|
-
});
|
|
48
|
+
return true;
|
|
52
49
|
}
|
|
53
50
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
}
|
|
51
|
+
return paramAdded;
|
|
52
|
+
}, false);
|
|
58
53
|
|
|
54
|
+
if (allowHash) url += location.hash;
|
|
59
55
|
return url;
|
|
60
56
|
}
|
|
61
57
|
|
|
@@ -192,12 +188,14 @@ class ChoreographCreatePixel {
|
|
|
192
188
|
try {
|
|
193
189
|
data[fieldName] = data[fieldName](element);
|
|
194
190
|
} catch (error) {
|
|
195
|
-
this.
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
191
|
+
if (!this.config.optionalFields.includes(fieldName)) {
|
|
192
|
+
this.log(error.message, {
|
|
193
|
+
level: "error",
|
|
194
|
+
data: { which: { [fieldName]: this.config.which[fieldName] } },
|
|
195
|
+
});
|
|
199
196
|
|
|
200
|
-
|
|
197
|
+
hasErrors = true;
|
|
198
|
+
}
|
|
201
199
|
}
|
|
202
200
|
}
|
|
203
201
|
|
package/dist/bundle.esm.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
/*! choreograph-create-pixel v0.1.
|
|
1
|
+
/*! choreograph-create-pixel v0.1.8 2022/09/19 */
|
|
2
2
|
class ChoreographCreatePixel {
|
|
3
3
|
constructor(userConfig) {
|
|
4
4
|
this.previouslyScrapedJsonString = null;
|
|
@@ -29,31 +29,27 @@ class ChoreographCreatePixel {
|
|
|
29
29
|
if (this.validateConfig()) this.cycle();
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
static getUrl(
|
|
32
|
+
static getUrl({ allowedQueryParameters = [], allowHash = false } = {}) {
|
|
33
33
|
let url = `${location.protocol}//${location.host}${location.pathname}`;
|
|
34
34
|
const parameters = new URLSearchParams(location.search);
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
allowedQueryParameters.reduce((paramAdded, parameter) => {
|
|
37
|
+
const separator = paramAdded ? "&" : "?";
|
|
38
|
+
const key = encodeURI(parameter);
|
|
39
|
+
const value = parameters.get(parameter);
|
|
38
40
|
|
|
39
|
-
if (
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const value = parameters.get(parameter);
|
|
41
|
+
if (value != null) {
|
|
42
|
+
url += `${separator}${key}${
|
|
43
|
+
value === "" ? "" : `=${encodeURI(value)}`
|
|
44
|
+
}`;
|
|
44
45
|
|
|
45
|
-
|
|
46
|
-
url += `${separator}${key}=${encodeURI(value)}`;
|
|
47
|
-
paramAdded = true;
|
|
48
|
-
}
|
|
49
|
-
});
|
|
46
|
+
return true;
|
|
50
47
|
}
|
|
51
48
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
}
|
|
49
|
+
return paramAdded;
|
|
50
|
+
}, false);
|
|
56
51
|
|
|
52
|
+
if (allowHash) url += location.hash;
|
|
57
53
|
return url;
|
|
58
54
|
}
|
|
59
55
|
|
|
@@ -190,12 +186,14 @@ class ChoreographCreatePixel {
|
|
|
190
186
|
try {
|
|
191
187
|
data[fieldName] = data[fieldName](element);
|
|
192
188
|
} catch (error) {
|
|
193
|
-
this.
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
189
|
+
if (!this.config.optionalFields.includes(fieldName)) {
|
|
190
|
+
this.log(error.message, {
|
|
191
|
+
level: "error",
|
|
192
|
+
data: { which: { [fieldName]: this.config.which[fieldName] } },
|
|
193
|
+
});
|
|
197
194
|
|
|
198
|
-
|
|
195
|
+
hasErrors = true;
|
|
196
|
+
}
|
|
199
197
|
}
|
|
200
198
|
}
|
|
201
199
|
|
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){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}
|
|
1
|
+
/*! choreograph-create-pixel v0.1.8 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)||(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}();
|
package/package.json
CHANGED