@shopgate/tracking-core 7.30.0-alpha.7 → 7.30.0-alpha.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/helpers/helper.js CHANGED
@@ -1,75 +1,286 @@
1
- function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}import DataRequest from'@shopgate/pwa-core/classes/DataRequest';import{logger}from'@shopgate/pwa-core/helpers';import*as _SGAction from'@shopgate/pwa-core/commands/unifiedTracking';export{_SGAction as SGAction};/**
1
+ import "core-js/modules/es.string.replace.js";
2
+ import DataRequest from '@shopgate/pwa-core/classes/DataRequest';
3
+ import { logger } from '@shopgate/pwa-core/helpers';
4
+ import * as _SGAction from '@shopgate/pwa-core/commands/unifiedTracking';
5
+ export { _SGAction as SGAction };
6
+ /**
2
7
  * Decodes a hexadecimal encoded binary string
3
8
  * @param {string} str The string that shall be decoded
4
9
  * @see http://locutus.io/php/strings/hex2bin/
5
10
  * @returns {string|boolean} Hexadecimal representation of data. FALSE if decoding failed.
6
- */export var hex2bin=function hex2bin(str){var s="".concat(str);var ret=[];var i=0;var l;for(l=s.length;i<l;i+=2){var c=parseInt(s.substr(i,1),16);var k=parseInt(s.substr(i+1,1),16);// eslint-disable-next-line no-restricted-globals
7
- if(isNaN(c)||isNaN(k)){return false;}// eslint-disable-next-line no-bitwise
8
- ret.push(c<<4|k);}// eslint-disable-next-line prefer-spread
9
- return String.fromCharCode.apply(String,ret);};/**
11
+ */
12
+ export const hex2bin = str => {
13
+ const s = `${str}`;
14
+ const ret = [];
15
+ let i = 0;
16
+ let l;
17
+ for (l = s.length; i < l; i += 2) {
18
+ const c = parseInt(s.substr(i, 1), 16);
19
+ const k = parseInt(s.substr(i + 1, 1), 16);
20
+
21
+ // eslint-disable-next-line no-restricted-globals
22
+ if (isNaN(c) || isNaN(k)) {
23
+ return false;
24
+ }
25
+
26
+ // eslint-disable-next-line no-bitwise
27
+ ret.push(c << 4 | k);
28
+ }
29
+
30
+ // eslint-disable-next-line prefer-spread
31
+ return String.fromCharCode.apply(String, ret);
32
+ };
33
+
34
+ /**
10
35
  * Sends a DataRequest
11
36
  * @param {string} url Url for the request
12
- */export function sendDataRequest(url){new DataRequest(url).dispatch().then(function(result){return logger.info(url,result);})["catch"](function(err){return err&&logger.error(err);});}/**
37
+ */
38
+ export function sendDataRequest(url) {
39
+ new DataRequest(url).dispatch().then(result => logger.info(url, result)).catch(err => err && logger.error(err));
40
+ }
41
+
42
+ /**
13
43
  * Object representation of URI(RFC2396) string
14
44
  * Made for general purpose. Feel free to extend it for your needs.
15
- */export var SGLink=/*#__PURE__*/function(){/**
45
+ */
46
+ export class SGLink {
47
+ /**
16
48
  * Constructor
17
49
  * @param {string} url Url to creating SGLink from
18
- */function SGLink(url){var _this=this;_classCallCheck(this,SGLink);// Complete url string
19
- _defineProperty(this,"url",'');// Any scheme we support (ex. https|shopgate-{$number}|sgapi)
20
- _defineProperty(this,"scheme",'');_defineProperty(this,"authority",'');_defineProperty(this,"path",'');_defineProperty(this,"splittedPath",[]);_defineProperty(this,"query",'');// Endpoint - safe for shopgate (ex. no endpoint is converted to "index")
21
- _defineProperty(this,"action",'');_defineProperty(this,"params",{});_defineProperty(this,"isDeepLink",false);/**
22
- * Converts the object to relative url
23
- * @param {boolean} [leadingSlash=true] Tells if the url shall start with a leading slash
24
- * @return {string}
25
- */_defineProperty(this,"toRelativeString",function(){var leadingSlash=arguments.length>0&&arguments[0]!==undefined?arguments[0]:true;var outputUrl='';if(leadingSlash&&_this.path[0]!=='/'){outputUrl='/';}if(_this.path){outputUrl+=SGLink.encodeURISafe(_this.path);}if(_this.query){// .query is always encoded
26
- outputUrl+="?".concat(_this.query);}return outputUrl;});this.url=url;this.parseUrl(url);}/**
50
+ */
51
+ constructor(url) {
52
+ // Complete url string
53
+ this.url = '';
54
+ // Any scheme we support (ex. https|shopgate-{$number}|sgapi)
55
+ this.scheme = '';
56
+ this.authority = '';
57
+ this.path = '';
58
+ this.splittedPath = [];
59
+ this.query = '';
60
+ // Endpoint - safe for shopgate (ex. no endpoint is converted to "index")
61
+ this.action = '';
62
+ this.params = {};
63
+ this.isDeepLink = false;
64
+ /**
65
+ * Converts the object to relative url
66
+ * @param {boolean} [leadingSlash=true] Tells if the url shall start with a leading slash
67
+ * @return {string}
68
+ */
69
+ this.toRelativeString = (leadingSlash = true) => {
70
+ let outputUrl = '';
71
+ if (leadingSlash && this.path[0] !== '/') {
72
+ outputUrl = '/';
73
+ }
74
+ if (this.path) {
75
+ outputUrl += SGLink.encodeURISafe(this.path);
76
+ }
77
+ if (this.query) {
78
+ // .query is always encoded
79
+ outputUrl += `?${this.query}`;
80
+ }
81
+ return outputUrl;
82
+ };
83
+ this.url = url;
84
+ this.parseUrl(url);
85
+ }
86
+
87
+ /**
27
88
  * Encode the url with encodeURIComponent and
28
89
  * takes care of double encoding
29
90
  *
30
91
  * @param {string} string - string to be encoded
31
92
  * @return {string}
32
- */return _createClass(SGLink,[{key:"parseUrl",value:/**
93
+ */
94
+ static encodeURIComponentSafe(string) {
95
+ const decoded = decodeURIComponent(string);
96
+ if (decoded !== string) {
97
+ return string;
98
+ }
99
+ return encodeURIComponent(string);
100
+ }
101
+
102
+ /**
103
+ * Encode the url and takes care of double encoding
104
+ * @param {string} string String to be encoded
105
+ * @returns {string} encoded string
106
+ */
107
+ static encodeURISafe(string) {
108
+ const decoded = decodeURI(string);
109
+ if (decoded !== string) {
110
+ return string;
111
+ }
112
+ return encodeURI(string);
113
+ }
114
+
115
+ /**
33
116
  * Parses url and extracts path, query, action, splittedPath, ...
34
117
  *
35
118
  * @param {string} incomingUrl The url that shall be parsed.
36
- */function parseUrl(incomingUrl){var urlToSanitize=incomingUrl;if(!urlToSanitize){urlToSanitize='';}var commonSchemas=['http','https','tel','mailto'];// Based on the regex in RFC2396 Appendix B.
37
- var parser=/^(?:([^:/?#]+):)?(?:\/\/([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/;var result=urlToSanitize.match(parser);if(!this.isDeepLink&&result[1]&&commonSchemas.indexOf(result[1])===-1){this.isDeepLink=true;var scheme="".concat(result[1],"://");/**
119
+ */
120
+ parseUrl(incomingUrl) {
121
+ let urlToSanitize = incomingUrl;
122
+ if (!urlToSanitize) {
123
+ urlToSanitize = '';
124
+ }
125
+ const commonSchemas = ['http', 'https', 'tel', 'mailto'];
126
+
127
+ // Based on the regex in RFC2396 Appendix B.
128
+ const parser = /^(?:([^:/?#]+):)?(?:\/\/([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?/;
129
+ const result = urlToSanitize.match(parser);
130
+ if (!this.isDeepLink && result[1] && commonSchemas.indexOf(result[1]) === -1) {
131
+ this.isDeepLink = true;
132
+ const scheme = `${result[1]}://`;
133
+
134
+ /**
38
135
  * Add slash so that we can parse the attributes properly
39
136
  * (for shopgate-standalone://cart we would get e.g. "authority: cart" which is wrong)
40
- */urlToSanitize=urlToSanitize.replace(scheme,"".concat(scheme,"/"));this.parseUrl(urlToSanitize);return;}this.scheme=result[1]||'';this.authority=result[2]||'';this.path=result[3]||'';this.query=result[4]||'';this.action='';// Endpoint - safe for shopgate (ex. no endpoint is converted to "index")
41
- if(this.query){var queryParts=this.query.split('&');var queryPartsLength=queryParts.length;// Clearing params
42
- this.setParams({});for(var i=0;i<queryPartsLength;i+=1){var queryPair=queryParts[i].split('=');this.setParam(queryPair[0],queryPair[1]);}}if(this.path){var pathSplitted=this.path.replace('/php/shopgate','').split('/');this.action=pathSplitted[0]||'';this.splittedPath=pathSplitted;if(pathSplitted[0]==='/'||pathSplitted[0]===''){this.action=pathSplitted[1]||'';this.splittedPath.shift();}}}/**
137
+ */
138
+ urlToSanitize = urlToSanitize.replace(scheme, `${scheme}/`);
139
+ this.parseUrl(urlToSanitize);
140
+ return;
141
+ }
142
+ this.scheme = result[1] || '';
143
+ this.authority = result[2] || '';
144
+ this.path = result[3] || '';
145
+ this.query = result[4] || '';
146
+ this.action = ''; // Endpoint - safe for shopgate (ex. no endpoint is converted to "index")
147
+
148
+ if (this.query) {
149
+ const queryParts = this.query.split('&');
150
+ const queryPartsLength = queryParts.length;
151
+
152
+ // Clearing params
153
+ this.setParams({});
154
+ for (let i = 0; i < queryPartsLength; i += 1) {
155
+ const queryPair = queryParts[i].split('=');
156
+ this.setParam(queryPair[0], queryPair[1]);
157
+ }
158
+ }
159
+ if (this.path) {
160
+ const pathSplitted = this.path.replace('/php/shopgate', '').split('/');
161
+ this.action = pathSplitted[0] || '';
162
+ this.splittedPath = pathSplitted;
163
+ if (pathSplitted[0] === '/' || pathSplitted[0] === '') {
164
+ this.action = pathSplitted[1] || '';
165
+ this.splittedPath.shift();
166
+ }
167
+ }
168
+ }
169
+
170
+ /**
43
171
  * Gets a param
44
172
  * @param {string} name name of param
45
173
  * @return {string|undefined}
46
- */},{key:"getParam",value:function getParam(name){if(!(name in this.params)){return undefined;}return this.params[name];}/**
174
+ */
175
+ getParam(name) {
176
+ if (!(name in this.params)) {
177
+ return undefined;
178
+ }
179
+ return this.params[name];
180
+ }
181
+
182
+ /**
47
183
  * Sets param.
48
184
  * @param {string} name Name of param
49
185
  * @param {string} [value] Value of param - if empty, the parameter will be deleted from the query
50
- */},{key:"setParam",value:function setParam(name,value){if(typeof value==='undefined'){this.deleteParam(name);}else{this.params[name]=value;this.setParams();}}/**
186
+ */
187
+ setParam(name, value) {
188
+ if (typeof value === 'undefined') {
189
+ this.deleteParam(name);
190
+ } else {
191
+ this.params[name] = value;
192
+ this.setParams();
193
+ }
194
+ }
195
+
196
+ /**
51
197
  * Sets params array
52
198
  *
53
199
  * @param {Object} [newParams] New Params to be set
54
- */},{key:"setParams",value:function setParams(newParams){var _this2=this;var newQueryArr=[];if(typeof newParams!=='undefined'){this.params=newParams;}Object.keys(this.params).forEach(function(keyName){newQueryArr.push("".concat(keyName,"=").concat(SGLink.encodeURIComponentSafe(_this2.params[keyName])));});this.query=newQueryArr.join('&');}/**
200
+ */
201
+ setParams(newParams) {
202
+ const newQueryArr = [];
203
+ if (typeof newParams !== 'undefined') {
204
+ this.params = newParams;
205
+ }
206
+ Object.keys(this.params).forEach(keyName => {
207
+ newQueryArr.push(`${keyName}=${SGLink.encodeURIComponentSafe(this.params[keyName])}`);
208
+ });
209
+ this.query = newQueryArr.join('&');
210
+ }
211
+
212
+ /**
55
213
  * Safely deletes the param.
56
214
  *
57
215
  * @param {string} name name of param
58
216
  * @returns {boolean}
59
- */},{key:"deleteParam",value:function deleteParam(name){if(!(name in this.params)){return false;}delete this.params[name];this.setParams(this.params);return true;}/**
217
+ */
218
+ deleteParam(name) {
219
+ if (!(name in this.params)) {
220
+ return false;
221
+ }
222
+ delete this.params[name];
223
+ this.setParams(this.params);
224
+ return true;
225
+ }
226
+
227
+ /**
60
228
  * Converts the object to string
61
229
  *
62
230
  * @return {string}
63
- */},{key:"toString",value:function toString(){var outputUrl='';var notNavigatorSchema=['mailto','tel'].indexOf(this.scheme)>-1;if(this.scheme){outputUrl+=this.scheme;// The sgapi-links don't need further scheme parsing since the scheme is 'sgapi:'
64
- if(this.scheme!=='sgapi'){if(notNavigatorSchema){if(this.scheme.indexOf(':')===-1){outputUrl+=':';}}else{if(this.scheme.indexOf(':/')===-1){// If the scheme already contains :/ we don't want to add it again
65
- outputUrl+=':/';this.scheme=outputUrl;}if(!this.isDeepLink){outputUrl+='/';}}}else if(this.scheme.indexOf(':')===-1){outputUrl+=':';}}if(this.authority){outputUrl+=this.authority;}outputUrl+=this.toRelativeString(false);return outputUrl;}},{key:"setUtmParams",value:/**
231
+ */
232
+ toString() {
233
+ let outputUrl = '';
234
+ const notNavigatorSchema = ['mailto', 'tel'].indexOf(this.scheme) > -1;
235
+ if (this.scheme) {
236
+ outputUrl += this.scheme;
237
+
238
+ // The sgapi-links don't need further scheme parsing since the scheme is 'sgapi:'
239
+ if (this.scheme !== 'sgapi') {
240
+ if (notNavigatorSchema) {
241
+ if (this.scheme.indexOf(':') === -1) {
242
+ outputUrl += ':';
243
+ }
244
+ } else {
245
+ if (this.scheme.indexOf(':/') === -1) {
246
+ // If the scheme already contains :/ we don't want to add it again
247
+ outputUrl += ':/';
248
+ this.scheme = outputUrl;
249
+ }
250
+ if (!this.isDeepLink) {
251
+ outputUrl += '/';
252
+ }
253
+ }
254
+ } else if (this.scheme.indexOf(':') === -1) {
255
+ outputUrl += ':';
256
+ }
257
+ }
258
+ if (this.authority) {
259
+ outputUrl += this.authority;
260
+ }
261
+ outputUrl += this.toRelativeString(false);
262
+ return outputUrl;
263
+ }
264
+ /**
66
265
  * Sets utm param from event.
67
266
  * @param {Object} data event data
68
267
  * @param {Object} raw event raw data
69
- */function setUtmParams(data,raw){// Add fake params, only if it didn't come from branch.io
70
- if(raw.type!=='branchio'){this.setParam('utm_source','shopgate');this.setParam('utm_medium',raw.type);}if(raw.type==='push_message'){var campaigns=['cart_reminder','inactive_app_user'];var notificationId=raw.notificationId||'not-provided';var campaignName=this.getParam('utm_campaign');if(campaigns.indexOf(campaignName)!==-1){// Set utm_content to distinguish the cart reminders from "normal" push messages
71
- this.setParam('utm_content',campaignName);}this.setParam('utm_campaign',"push-".concat(notificationId));}}}],[{key:"encodeURIComponentSafe",value:function encodeURIComponentSafe(string){var decoded=decodeURIComponent(string);if(decoded!==string){return string;}return encodeURIComponent(string);}/**
72
- * Encode the url and takes care of double encoding
73
- * @param {string} string String to be encoded
74
- * @returns {string} encoded string
75
- */},{key:"encodeURISafe",value:function encodeURISafe(string){var decoded=decodeURI(string);if(decoded!==string){return string;}return encodeURI(string);}}]);}();
268
+ */
269
+ setUtmParams(data, raw) {
270
+ // Add fake params, only if it didn't come from branch.io
271
+ if (raw.type !== 'branchio') {
272
+ this.setParam('utm_source', 'shopgate');
273
+ this.setParam('utm_medium', raw.type);
274
+ }
275
+ if (raw.type === 'push_message') {
276
+ const campaigns = ['cart_reminder', 'inactive_app_user'];
277
+ const notificationId = raw.notificationId || 'not-provided';
278
+ const campaignName = this.getParam('utm_campaign');
279
+ if (campaigns.indexOf(campaignName) !== -1) {
280
+ // Set utm_content to distinguish the cart reminders from "normal" push messages
281
+ this.setParam('utm_content', campaignName);
282
+ }
283
+ this.setParam('utm_campaign', `push-${notificationId}`);
284
+ }
285
+ }
286
+ }
package/helpers/optOut.js CHANGED
@@ -1,44 +1,140 @@
1
- var disableStr='sg-tracking-disabled';/**
1
+ const disableStr = 'sg-tracking-disabled';
2
+
3
+ /**
2
4
  * Sets opt out state to localStorage
3
5
  * @param {boolean} optOutState true - user opted out of tracking, false - user did not opted out
4
6
  * @private
5
7
  * @returns {boolean|null} Info about the success
6
- */function setLocalStorage(optOutState){if(!(localStorage&&localStorage.setItem)){return null;}if(typeof optOutState!=='boolean'){console.warn('setCookie for outOut invalid param optOutState. Param must be boolean');return null;}try{localStorage.setItem(disableStr,optOutState);}catch(e){return null;}return optOutState;}/**
8
+ */
9
+ function setLocalStorage(optOutState) {
10
+ if (!(localStorage && localStorage.setItem)) {
11
+ return null;
12
+ }
13
+ if (typeof optOutState !== 'boolean') {
14
+ console.warn('setCookie for outOut invalid param optOutState. Param must be boolean');
15
+ return null;
16
+ }
17
+ try {
18
+ localStorage.setItem(disableStr, optOutState);
19
+ } catch (e) {
20
+ return null;
21
+ }
22
+ return optOutState;
23
+ }
24
+
25
+ /**
7
26
  * Gets optOut state from localStorage
8
27
  * @private
9
28
  * @returns {boolean|null} Opt out state in the localstorage
10
- */function getLocalStorage(){if(!(localStorage&&localStorage.getItem)){return null;}var state=localStorage.getItem(disableStr);if(state==='false'){state=false;}else if(state==='true'){state=true;}return state;}/**
29
+ */
30
+ function getLocalStorage() {
31
+ if (!(localStorage && localStorage.getItem)) {
32
+ return null;
33
+ }
34
+ let state = localStorage.getItem(disableStr);
35
+ if (state === 'false') {
36
+ state = false;
37
+ } else if (state === 'true') {
38
+ state = true;
39
+ }
40
+ return state;
41
+ }
42
+
43
+ /**
11
44
  * Sets opt out cookie
12
45
  * @param {boolean} optOutState true - user opted out of tracking, false - user did not opted out
13
46
  * @private
14
47
  * @returns {boolean} Info about the success
15
- */function setCookie(optOutState){switch(optOutState){case true:document.cookie="".concat(disableStr,"=true; expires=Thu, 18 Jan 2038 03:13:59 UTC; path=/");window[disableStr]=true;break;case false:document.cookie="".concat(disableStr,"=false; expires=Thu, 01 Jan 1970 00:00:01 UTC; path=/");window[disableStr]=false;break;default:console.warn('setCookie for outOut invalid param optOutState. Param must be boolean');return false;}return true;}/**
48
+ */
49
+ function setCookie(optOutState) {
50
+ switch (optOutState) {
51
+ case true:
52
+ document.cookie = `${disableStr}=true; expires=Thu, 18 Jan 2038 03:13:59 UTC; path=/`;
53
+ window[disableStr] = true;
54
+ break;
55
+ case false:
56
+ document.cookie = `${disableStr}=false; expires=Thu, 01 Jan 1970 00:00:01 UTC; path=/`;
57
+ window[disableStr] = false;
58
+ break;
59
+ default:
60
+ console.warn('setCookie for outOut invalid param optOutState. Param must be boolean');
61
+ return false;
62
+ }
63
+ return true;
64
+ }
65
+
66
+ /**
16
67
  * Set global + storages
17
68
  * @param {boolean} optOutParam If false -> revert the opt out (enable tracking)
18
69
  * @private
19
70
  * @returns {boolean} optOut State which was set
20
- */function setOptOut(optOutParam){window[disableStr]=optOutParam;setCookie(optOutParam);setLocalStorage(optOutParam);return optOutParam;}/**
71
+ */
72
+ function setOptOut(optOutParam) {
73
+ window[disableStr] = optOutParam;
74
+ setCookie(optOutParam);
75
+ setLocalStorage(optOutParam);
76
+ return optOutParam;
77
+ }
78
+
79
+ /**
21
80
  * Global helper for the opt out mechanism for all tracking tools
22
81
  * Sets information to container and inform whoever should
23
82
  * be informed (GA)
24
83
  *
25
84
  * @param {boolean} [optOutParam = true] If false -> revert the opt out (enable tracking)
26
85
  * @returns {boolean} - state which was set
27
- */function optOut(optOutParam){var out=optOutParam;if(typeof optOutParam==='undefined'){out=true;}setOptOut(out);return out;}/**
86
+ */
87
+ function optOut(optOutParam) {
88
+ let out = optOutParam;
89
+ if (typeof optOutParam === 'undefined') {
90
+ out = true;
91
+ }
92
+ setOptOut(out);
93
+ return out;
94
+ }
95
+
96
+ /**
28
97
  * Gets optout state from cookie
29
98
  * @private
30
99
  * @returns {boolean|null} OptOut state from the cookie
31
- */function getCookie(){if(document.cookie.indexOf("".concat(disableStr,"=true"))>-1){return true;}if(document.cookie.indexOf("".concat(disableStr,"=false"))>-1){return false;}return null;}/**
100
+ */
101
+ function getCookie() {
102
+ if (document.cookie.indexOf(`${disableStr}=true`) > -1) {
103
+ return true;
104
+ }
105
+ if (document.cookie.indexOf(`${disableStr}=false`) > -1) {
106
+ return false;
107
+ }
108
+ return null;
109
+ }
110
+
111
+ /**
32
112
  * Check if the opt-out state is set
33
113
  *
34
114
  * Cookie is privileged.
35
115
  *
36
116
  * @returns {boolean} Information if the user opt out
37
- */function isOptOut(){// Check cookie first
38
- var optOutState=getCookie();// No cookie info, check localStorage
39
- if(optOutState===null){optOutState=getLocalStorage();}// No localStorage, we get default value
40
- if(optOutState===null||typeof optOutState==='undefined'){optOutState=false;}// Set global for the environment
41
- window[disableStr]=optOutState;return optOutState;}/**
117
+ */
118
+ function isOptOut() {
119
+ // Check cookie first
120
+ let optOutState = getCookie();
121
+
122
+ // No cookie info, check localStorage
123
+ if (optOutState === null) {
124
+ optOutState = getLocalStorage();
125
+ }
126
+
127
+ // No localStorage, we get default value
128
+ if (optOutState === null || typeof optOutState === 'undefined') {
129
+ optOutState = false;
130
+ }
131
+
132
+ // Set global for the environment
133
+ window[disableStr] = optOutState;
134
+ return optOutState;
135
+ }
136
+
137
+ /**
42
138
  * Inits cookie and synchronizes localStorage
43
139
  * with information stored in cookie (if needed)
44
140
  *
@@ -46,4 +142,9 @@ window[disableStr]=optOutState;return optOutState;}/**
46
142
  * not be removed, since localStorage may be purged
47
143
  * from time to time (depends on multiple factors
48
144
  * like memory usage and etc.).
49
- */function init(){setOptOut(isOptOut());}init();export{isOptOut,optOut};
145
+ */
146
+ function init() {
147
+ setOptOut(isOptOut());
148
+ }
149
+ init();
150
+ export { isOptOut, optOut };
@@ -1,27 +1,123 @@
1
+ import "core-js/modules/es.string.replace.js";
1
2
  /**
2
3
  * Data modifier for urls
3
- */ /* eslint-disable camelcase */ // Contains a list of url parameter names that will be removed from the url
4
- var urlParameterBlacklist=[// Tracking parameters that come after switching from http to https
5
- 'SWITCHTOKEN','preview','emos_sid','emos_vid','_ga','__utma','__utmb','__utmc','__utmx','__utmz','__utmv','__utmk',// Ga parameter for external payment methods
6
- 'utm_nooverride'];/**
4
+ */
5
+
6
+ /* eslint-disable camelcase */
7
+
8
+ // Contains a list of url parameter names that will be removed from the url
9
+ const urlParameterBlacklist = [
10
+ // Tracking parameters that come after switching from http to https
11
+ 'SWITCHTOKEN', 'preview', 'emos_sid', 'emos_vid', '_ga', '__utma', '__utmb', '__utmc', '__utmx', '__utmz', '__utmv', '__utmk',
12
+ // Ga parameter for external payment methods
13
+ 'utm_nooverride'];
14
+
15
+ /**
7
16
  * Returns a path with optional parameters
8
17
  * @param {string} pageName Name of the page.
9
18
  * @param {string[]} path Path splitted by '/'.
10
19
  * @returns {string}
11
- */var pathWithParameters=function pathWithParameters(pageName,path){if(path.length===0){return pageName;}return"".concat(pageName,"/").concat(path.join('/'));};// Mapping function, returns an array.
20
+ */
21
+ const pathWithParameters = (pageName, path) => {
22
+ if (path.length === 0) {
23
+ return pageName;
24
+ }
25
+ return `${pageName}/${path.join('/')}`;
26
+ };
27
+
28
+ // Mapping function, returns an array.
12
29
  // The first value will be for shopgate account, second for merchant
13
- var mapping={'':function _(){return'index';},favourite_list:function favourite_list(path,data){var isEmpty=true;if(typeof data.favouriteList.products!=='undefined'&&data.favouriteList.products.length){isEmpty=false;}return isEmpty?'favourite_list_empty':'favourite_list';},cart:function cart(path,data){var isEmpty=data.cart.productsCount===0;return isEmpty?'cart_empty':'cart';},payment_success:function payment_success(path){if(path.length>=2){return"checkout_success/".concat(path[1]);}if(path.length===1){return"checkout_success/".concat(path[0]);}return'checkout_success';},checkout_payment:function checkout_payment(path){return pathWithParameters('checkout_payment_and_shipping',path);},checkout:function checkout(){return'checkout';},payment_failure:function payment_failure(path){return pathWithParameters('payment_failure',path);}};/**
30
+ const mapping = {
31
+ '': () => 'index',
32
+ favourite_list(path, data) {
33
+ let isEmpty = true;
34
+ if (typeof data.favouriteList.products !== 'undefined' && data.favouriteList.products.length) {
35
+ isEmpty = false;
36
+ }
37
+ return isEmpty ? 'favourite_list_empty' : 'favourite_list';
38
+ },
39
+ cart(path, data) {
40
+ const isEmpty = data.cart.productsCount === 0;
41
+ return isEmpty ? 'cart_empty' : 'cart';
42
+ },
43
+ payment_success: path => {
44
+ if (path.length >= 2) {
45
+ return `checkout_success/${path[1]}`;
46
+ }
47
+ if (path.length === 1) {
48
+ return `checkout_success/${path[0]}`;
49
+ }
50
+ return 'checkout_success';
51
+ },
52
+ checkout_payment: path => pathWithParameters('checkout_payment_and_shipping', path),
53
+ checkout: () => 'checkout',
54
+ payment_failure: path => pathWithParameters('payment_failure', path)
55
+ };
56
+
57
+ /**
14
58
  * Maps an internal url to an external url that we can send to tracking providers
15
59
  *
16
60
  * @param {string} url internal url
17
61
  * @param {Object} [data] sgData object
18
62
  *
19
63
  * @returns {Object} external url
20
- */function sgTrackingUrlMapper(url,data){// In our development system urls start with /php/shopgate/ always
21
- var developmentPath='/php/shopgate/';var appRegex=/sg_app_resources\/[0-9]*\//;// Build regex that will remove all blacklisted parameters
22
- var regex='';urlParameterBlacklist.forEach(function(entry){regex+="((\\?|^){0,1}(".concat(entry,"=.*?(&|$)))");regex+='|';});var params=url.indexOf('?')!==-1?url.split('?')[1]:'';var urlParams=params.replace(new RegExp(regex,'g'),'');urlParams=urlParams===''?'':"?".concat(urlParams.replace(/&$/,''));// Get rid of hash and app urls
23
- var urlPath=url.split('?')[0].split('#')[0].replace(appRegex,'');// Get path from url
24
- if(url.indexOf(developmentPath)!==-1){urlPath=urlPath.substring(urlPath.indexOf(developmentPath)+developmentPath.length);}else if(urlPath.indexOf('http')!==-1){urlPath=urlPath.substring(urlPath.indexOf('/',urlPath.indexOf('//')+2)+1);}else{urlPath=urlPath.substring(1);}// Get action from path
25
- var action=urlPath.substring(0,urlPath.indexOf('/')!==-1?urlPath.indexOf('/'):undefined);// If no mapping function is available for the action, continue
26
- if(typeof mapping[action]==='undefined'){return{"public":urlPath+urlParams,"private":action};}// Call mapping function and return its result
27
- var pathWithoutAction=urlPath.substring(action.length+1);var pathWithoutActionArray=[];if(pathWithoutAction.length!==0){pathWithoutActionArray=pathWithoutAction.split('/');}var mappedUrls=mapping[action](pathWithoutActionArray,data||{});if(Array.isArray(mappedUrls)){return{"public":mappedUrls[1]+urlParams,"private":mappedUrls[0]};}return{"public":mappedUrls+urlParams,"private":mappedUrls};}export default sgTrackingUrlMapper;/* eslint-enable camelcase */
64
+ */
65
+ function sgTrackingUrlMapper(url, data) {
66
+ // In our development system urls start with /php/shopgate/ always
67
+ const developmentPath = '/php/shopgate/';
68
+ const appRegex = /sg_app_resources\/[0-9]*\//;
69
+
70
+ // Build regex that will remove all blacklisted parameters
71
+ let regex = '';
72
+ urlParameterBlacklist.forEach(entry => {
73
+ regex += `((\\?|^){0,1}(${entry}=.*?(&|$)))`;
74
+ regex += '|';
75
+ });
76
+ const params = url.indexOf('?') !== -1 ? url.split('?')[1] : '';
77
+ let urlParams = params.replace(new RegExp(regex, 'g'), '');
78
+ urlParams = urlParams === '' ? '' : `?${urlParams.replace(/&$/, '')}`;
79
+
80
+ // Get rid of hash and app urls
81
+ let urlPath = url.split('?')[0].split('#')[0].replace(appRegex, '');
82
+
83
+ // Get path from url
84
+ if (url.indexOf(developmentPath) !== -1) {
85
+ urlPath = urlPath.substring(urlPath.indexOf(developmentPath) + developmentPath.length);
86
+ } else if (urlPath.indexOf('http') !== -1) {
87
+ urlPath = urlPath.substring(urlPath.indexOf('/', urlPath.indexOf('//') + 2) + 1);
88
+ } else {
89
+ urlPath = urlPath.substring(1);
90
+ }
91
+
92
+ // Get action from path
93
+ const action = urlPath.substring(0, urlPath.indexOf('/') !== -1 ? urlPath.indexOf('/') : undefined);
94
+
95
+ // If no mapping function is available for the action, continue
96
+ if (typeof mapping[action] === 'undefined') {
97
+ return {
98
+ public: urlPath + urlParams,
99
+ private: action
100
+ };
101
+ }
102
+
103
+ // Call mapping function and return its result
104
+ const pathWithoutAction = urlPath.substring(action.length + 1);
105
+ let pathWithoutActionArray = [];
106
+ if (pathWithoutAction.length !== 0) {
107
+ pathWithoutActionArray = pathWithoutAction.split('/');
108
+ }
109
+ const mappedUrls = mapping[action](pathWithoutActionArray, data || {});
110
+ if (Array.isArray(mappedUrls)) {
111
+ return {
112
+ public: mappedUrls[1] + urlParams,
113
+ private: mappedUrls[0]
114
+ };
115
+ }
116
+ return {
117
+ public: mappedUrls + urlParams,
118
+ private: mappedUrls
119
+ };
120
+ }
121
+ export default sgTrackingUrlMapper;
122
+
123
+ /* eslint-enable camelcase */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shopgate/tracking-core",
3
- "version": "7.30.0-alpha.7",
3
+ "version": "7.30.0-alpha.8",
4
4
  "description": "Tracking core library for the Shopgate Connect PWA.",
5
5
  "author": "Shopgate <support@shopgate.com>",
6
6
  "license": "Apache-2.0",
@@ -16,8 +16,8 @@
16
16
  "connect"
17
17
  ],
18
18
  "devDependencies": {
19
- "@shopgate/eslint-config": "7.30.0-alpha.7",
20
- "@shopgate/pwa-core": "7.30.0-alpha.7",
19
+ "@shopgate/eslint-config": "7.30.0-alpha.8",
20
+ "@shopgate/pwa-core": "7.30.0-alpha.8",
21
21
  "chai": "^3.5.0",
22
22
  "jsdom": "^10.0.0",
23
23
  "mocha": "^3.1.0",