Skip to content

Commit

Permalink
Prevent 400 - bad request by adding validation
Browse files Browse the repository at this point in the history
LinkedIn CAPI fails when there's no ID set and no user information (first and last name)
  • Loading branch information
mithredate committed Apr 28, 2024
1 parent 592aab4 commit e171e34
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 56 deletions.
77 changes: 49 additions & 28 deletions template.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,56 @@ if (isLoggingEnabled) {
);
}

sendHttpRequest(
postUrl,
(statusCode, headers, body) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'LinkedIn',
Type: 'Response',
TraceId: traceId,
EventName: postBody.eventId,
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body
})
);
}
// perform validation check on presence of 1/4 of the required IDs. If at least 1 ID is present, make the API call. If no IDs are present, log the warning and no call is made
if (validateUserData()){
sendConversionToLinkedIn();
} else {
logToConsole('No conversion event was sent to CAPI. You must set 1 out of the 4 acceptable IDs (SHA256_EMAIL, LINKEDIN_FIRST_PARTY_ADS_TRACKING_UUID, ACXIOM_ID, ORACLE_MOAT_ID) to resolve this issue or make certain to send both firstName and lastName.');

if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
},
{
headers: postHeaders,
method: 'POST'
},
JSON.stringify(postBody)
);
data.gtmOnFailure();
}

function validateUserData() {
if (postBody.user.userIds.length > 0) {
return true;
}

return postBody.user.userInfo.firstName != "" &&
postBody.user.userInfo.lastName != "";
}


function sendConversionToLinkedIn() {
sendHttpRequest(
postUrl,
(statusCode, headers, body) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'LinkedIn',
Type: 'Response',
TraceId: traceId,
EventName: postBody.eventId,
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body
})
);
}

if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
},
{
headers: postHeaders,
method: 'POST'
},
JSON.stringify(postBody)
);
}

function getRequestUrl() {
return 'https://api.linkedin.com/rest/conversionEvents';
Expand Down
77 changes: 49 additions & 28 deletions template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -372,35 +372,56 @@ if (isLoggingEnabled) {
);
}

sendHttpRequest(
postUrl,
(statusCode, headers, body) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'LinkedIn',
Type: 'Response',
TraceId: traceId,
EventName: postBody.eventId,
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body
})
);
}
// perform validation check on presence of 1/4 of the required IDs. If at least 1 ID is present, make the API call. If no IDs are present, log the warning and no call is made
if (validateUserData()){
sendConversionToLinkedIn();
} else {
logToConsole('No conversion event was sent to CAPI. You must set 1 out of the 4 acceptable IDs (SHA256_EMAIL, LINKEDIN_FIRST_PARTY_ADS_TRACKING_UUID, ACXIOM_ID, ORACLE_MOAT_ID) to resolve this issue or make certain to send both firstName and lastName.');
if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
},
{
headers: postHeaders,
method: 'POST'
},
JSON.stringify(postBody)
);
data.gtmOnFailure();
}

function validateUserData() {
if (postBody.user.userIds.length > 0) {
return true;
}

return postBody.user.userInfo.firstName != "" &&
postBody.user.userInfo.lastName != "";
}


function sendConversionToLinkedIn() {
sendHttpRequest(
postUrl,
(statusCode, headers, body) => {
if (isLoggingEnabled) {
logToConsole(
JSON.stringify({
Name: 'LinkedIn',
Type: 'Response',
TraceId: traceId,
EventName: postBody.eventId,
ResponseStatusCode: statusCode,
ResponseHeaders: headers,
ResponseBody: body
})
);
}

if (statusCode >= 200 && statusCode < 300) {
data.gtmOnSuccess();
} else {
data.gtmOnFailure();
}
},
{
headers: postHeaders,
method: 'POST'
},
JSON.stringify(postBody)
);
}

function getRequestUrl() {
return 'https://api.linkedin.com/rest/conversionEvents';
Expand Down

0 comments on commit e171e34

Please sign in to comment.