diff --git a/metadata.yaml b/metadata.yaml index b7d9e3e..a991b41 100644 --- a/metadata.yaml +++ b/metadata.yaml @@ -1,5 +1,7 @@ homepage: "https://stape.io/" versions: + - sha: 81fefccc9ebf0f95b71475cf07e65b45b7132879 + changeNotes: Data validation improvements. - sha: a811c915bc0c838675f6050483eaf49f8f484c0c changeNotes: Fix error with custom_data. - sha: 1c14665d881cb8bc1c62d545cabae6b2693b2c64 diff --git a/template.js b/template.js index 3411bcf..1d54477 100644 --- a/template.js +++ b/template.js @@ -32,6 +32,11 @@ if (url && url.lastIndexOf('https://gtm-msr.appspot.com/', 0) === 0) { return data.gtmOnSuccess(); } +const pixelOrAppId = data.eventConversionType === 'MOBILE_APP' ? data.snapAppId : data.pixelId; +if(!pixelOrAppId || !data.accessToken) { + return data.gtmOnFailure(); +} + sendTrackRequest(mapEvent(eventData, data)); function sendTrackRequest(mappedEvent) { @@ -110,8 +115,7 @@ if (data.useOptimisticScenario) { } function getPostUrl() { - const id = data.eventConversionType === 'MOBILE_APP' ? data.snapAppId : data.pixelId; - let postUrl = 'https://tr.snapchat.com/v3/' + encodeUriComponent(id) + 'events'; + let postUrl = 'https://tr.snapchat.com/v3/' + encodeUriComponent(pixelOrAppId) + '/events'; if (data.validate) { postUrl = postUrl + '/validate'; } @@ -239,7 +243,9 @@ function addCustomData(eventData, mappedData) { if (data.customDataList) { data.customDataList.forEach((d) => { - mappedData.custom_data[d.name] = d.value; + if(isValidValue(d.value)) { + mappedData.custom_data[d.name] = d.value; + } }); } @@ -262,7 +268,9 @@ function addAppData(eventData, mappedData) { if (data.appDataList) { data.appDataList.forEach((d) => { - mappedData.app_data[d.name] = d.value; + if (isValidValue(d.value)) { + mappedData.app_data[d.name] = d.value; + } }); } @@ -286,7 +294,9 @@ function addServerData(eventData, mappedData) { if (data.serverDataList) { data.serverDataList.forEach((d) => { - mappedData[d.name] = d.value; + if (isValidValue(d.value)) { + mappedData[d.name] = d.value; + } }); } @@ -415,7 +425,9 @@ function addUserData(eventData, mappedData) { if (data.userDataList) { data.userDataList.forEach((d) => { - mappedData[d.name] = d.value; + if (isValidValue(d.value)) { + mappedData.user_data[d.name] = d.value; + } }); } @@ -492,3 +504,8 @@ function isConsentGivenOrNotRequired() { const xGaGcs = eventData['x-ga-gcs'] || ''; // x-ga-gcs is a string like "G110" return xGaGcs[2] === '1'; } + +function isValidValue(value) { + const valueType = getType(value); + return valueType !== 'null' && valueType !== 'undefined' && value !== ''; +} diff --git a/template.tpl b/template.tpl index 3ed1b31..897f439 100644 --- a/template.tpl +++ b/template.tpl @@ -668,6 +668,11 @@ if (url && url.lastIndexOf('https://gtm-msr.appspot.com/', 0) === 0) { return data.gtmOnSuccess(); } +const pixelOrAppId = data.eventConversionType === 'MOBILE_APP' ? data.snapAppId : data.pixelId; +if(!pixelOrAppId || !data.accessToken) { + return data.gtmOnFailure(); +} + sendTrackRequest(mapEvent(eventData, data)); function sendTrackRequest(mappedEvent) { @@ -746,8 +751,7 @@ if (data.useOptimisticScenario) { } function getPostUrl() { - const id = data.eventConversionType === 'MOBILE_APP' ? data.snapAppId : data.pixelId; - let postUrl = 'https://tr.snapchat.com/v3/' + encodeUriComponent(id) + 'events'; + let postUrl = 'https://tr.snapchat.com/v3/' + encodeUriComponent(pixelOrAppId) + '/events'; if (data.validate) { postUrl = postUrl + '/validate'; } @@ -875,7 +879,9 @@ function addCustomData(eventData, mappedData) { if (data.customDataList) { data.customDataList.forEach((d) => { - mappedData.custom_data[d.name] = d.value; + if(isValidValue(d.value)) { + mappedData.custom_data[d.name] = d.value; + } }); } @@ -898,7 +904,9 @@ function addAppData(eventData, mappedData) { if (data.appDataList) { data.appDataList.forEach((d) => { - mappedData.app_data[d.name] = d.value; + if (isValidValue(d.value)) { + mappedData.app_data[d.name] = d.value; + } }); } @@ -922,7 +930,9 @@ function addServerData(eventData, mappedData) { if (data.serverDataList) { data.serverDataList.forEach((d) => { - mappedData[d.name] = d.value; + if (isValidValue(d.value)) { + mappedData[d.name] = d.value; + } }); } @@ -1051,7 +1061,9 @@ function addUserData(eventData, mappedData) { if (data.userDataList) { data.userDataList.forEach((d) => { - mappedData[d.name] = d.value; + if (isValidValue(d.value)) { + mappedData.user_data[d.name] = d.value; + } }); } @@ -1129,6 +1141,11 @@ function isConsentGivenOrNotRequired() { return xGaGcs[2] === '1'; } +function isValidValue(value) { + const valueType = getType(value); + return valueType !== 'null' && valueType !== 'undefined' && value !== ''; +} + ___SERVER_PERMISSIONS___