diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 0000000..35275aa --- /dev/null +++ b/.prettierrc @@ -0,0 +1,5 @@ +{ + "singleQuote": true, + "trailingComma": "none", + "printWidth": 150 +} diff --git a/template.js b/template.js index 01d1868..bb3dc93 100644 --- a/template.js +++ b/template.js @@ -15,86 +15,104 @@ const isLoggingEnabled = determinateIsLoggingEnabled(); const traceId = getRequestHeader('trace-id'); if (data.type === 'page_view') { - const url = getEventData('page_location') || getRequestHeader('referer'); + const url = getEventData('page_location') || getRequestHeader('referer'); - if (url) { - const value = parseUrl(url).searchParams[data.clickIdParameterName]; + if (url) { + const value = parseUrl(url).searchParams[data.clickIdParameterName]; - if (value) { - const options = { - domain: 'auto', - path: '/', - secure: true, - httpOnly: false - }; + if (value) { + const options = { + domain: 'auto', + path: '/', + secure: true, + httpOnly: false + }; - if (data.expiration > 0) options['max-age'] = data.expiration; + if (data.expiration > 0) options['max-age'] = data.expiration; - setCookie('taboola_cid', value, options, false); - } + setCookie('taboola_cid', value, options, false); } + } - data.gtmOnSuccess(); + data.gtmOnSuccess(); } else { - const clickId = getCookieValues('taboola_cid')[0] || ''; - - if (!clickId) { - data.gtmOnSuccess(); + const clickId = getCookieValues('taboola_cid')[0] || ''; - return; - } - - let requestUrl = 'https://trc.taboola.com/actions-handler/log/3/s2s-action?name='+enc(data.eventName)+'&click-id='+enc(clickId)+'&revenue='+enc(data.revenue)+'¤cy='+enc(data.currencyCode)+'&orderid='+enc(data.orderId); - - if (isLoggingEnabled) { - logToConsole(JSON.stringify({ - 'Name': 'Taboola', - 'Type': 'Request', - 'TraceId': traceId, - 'EventName': data.eventName, - 'RequestMethod': 'POST', - 'RequestUrl': requestUrl, - })); - } + if (!clickId) { + data.gtmOnSuccess(); - sendHttpRequest(requestUrl, (statusCode, headers, body) => { - if (isLoggingEnabled) { - logToConsole(JSON.stringify({ - 'Name': 'Taboola', - 'Type': 'Response', - 'TraceId': traceId, - 'EventName': data.eventName, - 'ResponseStatusCode': statusCode, - 'ResponseHeaders': headers, - 'ResponseBody': body, - })); - } - - if (statusCode >= 200 && statusCode < 300) { - data.gtmOnSuccess(); - } else { - data.gtmOnFailure(); - } - }, {method: 'POST'}); + return; + } + + let requestUrl = + 'https://trc.taboola.com/actions-handler/log/3/s2s-action?name=' + + enc(data.eventName) + + '&click-id=' + + enc(clickId) + + '&revenue=' + + enc(data.revenue) + + '¤cy=' + + enc(data.currencyCode) + + '&orderid=' + + enc(data.orderId); + + if (isLoggingEnabled) { + logToConsole( + JSON.stringify({ + Name: 'Taboola', + Type: 'Request', + TraceId: traceId, + EventName: data.eventName, + RequestMethod: 'POST', + RequestUrl: requestUrl + }) + ); + } + + sendHttpRequest( + requestUrl, + (statusCode, headers, body) => { + if (isLoggingEnabled) { + logToConsole( + JSON.stringify({ + Name: 'Taboola', + Type: 'Response', + TraceId: traceId, + EventName: data.eventName, + ResponseStatusCode: statusCode, + ResponseHeaders: headers, + ResponseBody: body + }) + ); + } + + if (statusCode >= 200 && statusCode < 300) { + data.gtmOnSuccess(); + } else { + data.gtmOnFailure(); + } + }, + { method: 'POST' } + ); } function enc(data) { - data = data || ''; - return encodeUriComponent(data); + data = data || ''; + return encodeUriComponent(data); } function determinateIsLoggingEnabled() { - if (!data.logType) { - return isDebug; - } + if (!data.logType) { + return isDebug; + } - if (data.logType === 'no') { - return false; - } + if (data.logType === 'no') { + return false; + } - if (data.logType === 'debug') { - return isDebug; - } + if (data.logType === 'debug') { + return isDebug; + } - return data.logType === 'always'; + return data.logType === 'always'; } diff --git a/template.tpl b/template.tpl index d974245..7e22756 100644 --- a/template.tpl +++ b/template.tpl @@ -190,88 +190,106 @@ const isLoggingEnabled = determinateIsLoggingEnabled(); const traceId = getRequestHeader('trace-id'); if (data.type === 'page_view') { - const url = getEventData('page_location') || getRequestHeader('referer'); + const url = getEventData('page_location') || getRequestHeader('referer'); - if (url) { - const value = parseUrl(url).searchParams[data.clickIdParameterName]; + if (url) { + const value = parseUrl(url).searchParams[data.clickIdParameterName]; - if (value) { - const options = { - domain: 'auto', - path: '/', - secure: true, - httpOnly: false - }; + if (value) { + const options = { + domain: 'auto', + path: '/', + secure: true, + httpOnly: false + }; - if (data.expiration > 0) options['max-age'] = data.expiration; + if (data.expiration > 0) options['max-age'] = data.expiration; - setCookie('taboola_cid', value, options, false); - } + setCookie('taboola_cid', value, options, false); } + } - data.gtmOnSuccess(); + data.gtmOnSuccess(); } else { - const clickId = getCookieValues('taboola_cid')[0] || ''; + const clickId = getCookieValues('taboola_cid')[0] || ''; - if (!clickId) { - data.gtmOnSuccess(); + if (!clickId) { + data.gtmOnSuccess(); - return; - } + return; + } - let requestUrl = 'https://trc.taboola.com/actions-handler/log/3/s2s-action?name='+enc(data.eventName)+'&click-id='+enc(clickId)+'&revenue='+enc(data.revenue)+'¤cy='+enc(data.currencyCode)+'&orderid='+enc(data.orderId); - - if (isLoggingEnabled) { - logToConsole(JSON.stringify({ - 'Name': 'Taboola', - 'Type': 'Request', - 'TraceId': traceId, - 'EventName': data.eventName, - 'RequestMethod': 'POST', - 'RequestUrl': requestUrl, - })); - } + let requestUrl = + 'https://trc.taboola.com/actions-handler/log/3/s2s-action?name=' + + enc(data.eventName) + + '&click-id=' + + enc(clickId) + + '&revenue=' + + enc(data.revenue) + + '¤cy=' + + enc(data.currencyCode) + + '&orderid=' + + enc(data.orderId); + + if (isLoggingEnabled) { + logToConsole( + JSON.stringify({ + Name: 'Taboola', + Type: 'Request', + TraceId: traceId, + EventName: data.eventName, + RequestMethod: 'POST', + RequestUrl: requestUrl + }) + ); + } - sendHttpRequest(requestUrl, (statusCode, headers, body) => { - if (isLoggingEnabled) { - logToConsole(JSON.stringify({ - 'Name': 'Taboola', - 'Type': 'Response', - 'TraceId': traceId, - 'EventName': data.eventName, - 'ResponseStatusCode': statusCode, - 'ResponseHeaders': headers, - 'ResponseBody': body, - })); - } + sendHttpRequest( + requestUrl, + (statusCode, headers, body) => { + if (isLoggingEnabled) { + logToConsole( + JSON.stringify({ + Name: 'Taboola', + Type: 'Response', + TraceId: traceId, + EventName: data.eventName, + ResponseStatusCode: statusCode, + ResponseHeaders: headers, + ResponseBody: body + }) + ); + } - if (statusCode >= 200 && statusCode < 300) { - data.gtmOnSuccess(); - } else { - data.gtmOnFailure(); - } - }, {method: 'POST'}); + if (statusCode >= 200 && statusCode < 300) { + data.gtmOnSuccess(); + } else { + data.gtmOnFailure(); + } + }, + { method: 'POST' } + ); } function enc(data) { - data = data || ''; - return encodeUriComponent(data); + data = data || ''; + return encodeUriComponent(data); } function determinateIsLoggingEnabled() { - if (!data.logType) { - return isDebug; - } + if (!data.logType) { + return isDebug; + } - if (data.logType === 'no') { - return false; - } + if (data.logType === 'no') { + return false; + } - if (data.logType === 'debug') { - return isDebug; - } + if (data.logType === 'debug') { + return isDebug; + } - return data.logType === 'always'; + return data.logType === 'always'; }