choreograph-create-pixel 0.1.3 โ†’ 0.1.5

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 CHANGED
@@ -192,10 +192,18 @@ Returns the bare page URL without query parameters or location hash. This is rec
192
192
  | `allowedParameters` | Array | Explicitly allow query parameters in the resulting URL. | `[]` |
193
193
  | `allowHash` | Boolean | Wether or not to include the location hash (`#foo`) of the URL. | `false` |
194
194
 
195
- ### `getUrlPathSegments()`
195
+ ### `getAllPathSegments()`
196
196
 
197
197
  Retrieves all path segments from the URL as an array. E.g. _http://www.example.com/foo/bar_ returns `["foo", "bar"]`.
198
198
 
199
- ### `getUrlQueryParameters()`
199
+ ### `getPathSegment(index)`
200
+
201
+ Retrieves a specific segment from the URL. E.g. `getPathSegment(0)` with URL _http://www.example.com/foo/bar_ returns `"foo"`.
202
+
203
+ ### `getAllQueryParameters()`
200
204
 
201
205
  Retrieves all query string parameters from the URL as an object. E.g. _http://www.example.com/?foo=bar_ returns `{ foo: "bar" }`.
206
+
207
+ ### `getQueryParameter(key)`
208
+
209
+ Retrieves a specific query parameter from the URL. E.g. `getQueryParameter('foo')` with URL _http://www.example.com/?foo=bar_ returns `"bar"`.
@@ -1,4 +1,4 @@
1
- /*! choreograph-create-pixel v0.1.3 2022/09/16 */
1
+ /*! choreograph-create-pixel v0.1.5 2022/09/19 */
2
2
  'use strict';
3
3
 
4
4
  class ChoreographCreatePixel {
@@ -11,6 +11,7 @@ class ChoreographCreatePixel {
11
11
  icons: { scrape: "๐Ÿ”", view: "๐Ÿ‘€", basket: "๐Ÿ›’", purchase: "๐Ÿงพ" },
12
12
  levels: { error: "โ›”๏ธ", warn: "โš ๏ธ", success: "โœ…", info: "โ„น๏ธ" },
13
13
  titleCss: "font:700 80% HelveticaNeue;margin:12px 0 6px",
14
+ iconCss: "font:HelveticaNeue",
14
15
  messageCss: "font:120% HelveticaNeue;margin-bottom:12px",
15
16
  eventTypes: {
16
17
  view: "product-viewed",
@@ -58,14 +59,18 @@ class ChoreographCreatePixel {
58
59
  return url;
59
60
  }
60
61
 
61
- static getUrlPathSegments() {
62
+ static getAllPathSegments() {
62
63
  return location.pathname
63
64
  .split("/")
64
65
  .filter((segment) => segment)
65
66
  .map((segment) => decodeURI(segment));
66
67
  }
67
68
 
68
- static getUrlQueryParameters() {
69
+ static getPathSegment(index) {
70
+ return this.getAllPathSegments(index);
71
+ }
72
+
73
+ static getAllQueryParameters() {
69
74
  return location.search
70
75
  .replace(/^\?/, "")
71
76
  .split("&")
@@ -81,6 +86,10 @@ class ChoreographCreatePixel {
81
86
  );
82
87
  }
83
88
 
89
+ static getQueryParameter(key) {
90
+ return this.getAllQueryParameters()[key];
91
+ }
92
+
84
93
  log(message, { level = "info", data = null }) {
85
94
  if (!this.config.debug || this.logs.includes(message)) return;
86
95
 
@@ -89,7 +98,7 @@ class ChoreographCreatePixel {
89
98
  this.settings.icons[this.config.what]
90
99
  }\n${this.settings.levels[level]} %c${message}`,
91
100
  this.settings.titleCss,
92
- "",
101
+ this.settings.iconCss,
93
102
  this.settings.colors[level]
94
103
  ? `${this.settings.messageCss};color:${this.settings.colors[level]}`
95
104
  : this.settings.messageCss,
@@ -1,4 +1,4 @@
1
- /*! choreograph-create-pixel v0.1.3 2022/09/16 */
1
+ /*! choreograph-create-pixel v0.1.5 2022/09/19 */
2
2
  class ChoreographCreatePixel {
3
3
  constructor(userConfig) {
4
4
  this.previouslyScrapedJsonString = null;
@@ -9,6 +9,7 @@ class ChoreographCreatePixel {
9
9
  icons: { scrape: "๐Ÿ”", view: "๐Ÿ‘€", basket: "๐Ÿ›’", purchase: "๐Ÿงพ" },
10
10
  levels: { error: "โ›”๏ธ", warn: "โš ๏ธ", success: "โœ…", info: "โ„น๏ธ" },
11
11
  titleCss: "font:700 80% HelveticaNeue;margin:12px 0 6px",
12
+ iconCss: "font:HelveticaNeue",
12
13
  messageCss: "font:120% HelveticaNeue;margin-bottom:12px",
13
14
  eventTypes: {
14
15
  view: "product-viewed",
@@ -56,14 +57,18 @@ class ChoreographCreatePixel {
56
57
  return url;
57
58
  }
58
59
 
59
- static getUrlPathSegments() {
60
+ static getAllPathSegments() {
60
61
  return location.pathname
61
62
  .split("/")
62
63
  .filter((segment) => segment)
63
64
  .map((segment) => decodeURI(segment));
64
65
  }
65
66
 
66
- static getUrlQueryParameters() {
67
+ static getPathSegment(index) {
68
+ return this.getAllPathSegments(index);
69
+ }
70
+
71
+ static getAllQueryParameters() {
67
72
  return location.search
68
73
  .replace(/^\?/, "")
69
74
  .split("&")
@@ -79,6 +84,10 @@ class ChoreographCreatePixel {
79
84
  );
80
85
  }
81
86
 
87
+ static getQueryParameter(key) {
88
+ return this.getAllQueryParameters()[key];
89
+ }
90
+
82
91
  log(message, { level = "info", data = null }) {
83
92
  if (!this.config.debug || this.logs.includes(message)) return;
84
93
 
@@ -87,7 +96,7 @@ class ChoreographCreatePixel {
87
96
  this.settings.icons[this.config.what]
88
97
  }\n${this.settings.levels[level]} %c${message}`,
89
98
  this.settings.titleCss,
90
- "",
99
+ this.settings.iconCss,
91
100
  this.settings.colors[level]
92
101
  ? `${this.settings.messageCss};color:${this.settings.colors[level]}`
93
102
  : this.settings.messageCss,
@@ -1,2 +1,2 @@
1
- /*! choreograph-create-pixel v0.1.3 2022/09/16 */
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}return 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",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 i,s,c;return i=e,c=[{key:"getUrl",value:function(e){var t="".concat(location.protocol,"//").concat(location.host).concat(location.pathname),n=new URLSearchParams(location.search);if(e){var o=!1;e.allowedParameters&&e.allowedParameters.length&&e.allowedParameters.forEach((function(e){var r=o?"&":"?",i=encodeURI(e),s=n.get(e);null!=s&&(t+="".concat(r).concat(i,"=").concat(encodeURI(s)),o=!0)})),e.allowHash&&(t+=location.hash)}return t}},{key:"getUrlPathSegments",value:function(){return location.pathname.split("/").filter((function(e){return e})).map((function(e){return decodeURI(e)}))}},{key:"getUrlQueryParameters",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]||"")))}),{})}}],(s=[{key:"log",value:function(e,t){var n,o=t.level,r=void 0===o?"info":o,i=t.data,s=void 0===i?null:i;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[r]," %c").concat(e),this.settings.titleCss,"",this.settings.colors[r]?"".concat(this.settings.messageCss,";color:").concat(this.settings.colors[r]):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={},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.log(e.message,{level:"error",data:{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("This required field's value is empty",{level:"error",data:{which:r({},t,o[t])}}),i=!0))}));var s=JSON.stringify(o);if(!i&&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 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){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=r}}])&&o(i.prototype,s),c&&o(i,c),Object.defineProperty(i,"prototype",{writable:!1}),e}()}();
1
+ /*! choreograph-create-pixel v0.1.5 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}return 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,a;return r=e,a=[{key:"getUrl",value:function(e){var t="".concat(location.protocol,"//").concat(location.host).concat(location.pathname),n=new URLSearchParams(location.search);if(e){var o=!1;e.allowedParameters&&e.allowedParameters.length&&e.allowedParameters.forEach((function(e){var i=o?"&":"?",r=encodeURI(e),s=n.get(e);null!=s&&(t+="".concat(i).concat(r,"=").concat(encodeURI(s)),o=!0)})),e.allowHash&&(t+=location.hash)}return t}},{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 a=["%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&&a.push(s),(n=console).info.apply(n,a),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.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),a&&o(r,a),Object.defineProperty(r,"prototype",{writable:!1}),e}()}();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "choreograph-create-pixel",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
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",