Skip to content

Commit

Permalink
Merge pull request #11 from stape-io/data-validation-improvements
Browse files Browse the repository at this point in the history
Data validation improvements
  • Loading branch information
kHorozhanov authored Apr 18, 2024
2 parents 6941e32 + f1e57e9 commit 6ac3401
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
2 changes: 2 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 23 additions & 6 deletions template.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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';
}
Expand Down Expand Up @@ -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;
}
});
}

Expand All @@ -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;
}
});
}

Expand All @@ -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;
}
});
}

Expand Down Expand Up @@ -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;
}
});
}

Expand Down Expand Up @@ -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 !== '';
}
29 changes: 23 additions & 6 deletions template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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';
}
Expand Down Expand Up @@ -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;
}
});
}

Expand All @@ -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;
}
});
}

Expand All @@ -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;
}
});
}

Expand Down Expand Up @@ -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;
}
});
}

Expand Down Expand Up @@ -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___

Expand Down

0 comments on commit 6ac3401

Please sign in to comment.