Skip to content

Commit

Permalink
Merge pull request #11 from stape-io/not-send-empty-conversion-value
Browse files Browse the repository at this point in the history
Not send empty conversionValue
  • Loading branch information
Bukashk0zzz authored Jul 4, 2024
2 parents 14b05a8 + 2ac0a3d commit 58a4245
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
2 changes: 2 additions & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
homepage: 'https://stape.io/'
documentation: "https://stape.io/blog/linkedin-conversion-api-tag-for-server-google-tag-manager"
versions:
- sha: 962b61b2dcbbe560e3571033ea3023d783171828
changeNotes: Not send empty conversionValue.
- sha: fd368e01b863881794c62752d9da0a138c2f8ddd
changeNotes: Linkedin Marketing June 2024.
- sha: 97210adfaf94ca8c16f4f44a7c2809510bbc9aea
Expand Down
13 changes: 8 additions & 5 deletions template.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,18 @@ function getRequestHeaders() {
}

function getPostBody() {
return {
const result = {
conversion: getConversionRuleUrn(),
conversionHappenedAt: getConversionHappenedAt(),
conversionValue: getConversionValue(),
eventId: getEventId(),
user: {
userIds: getUserIds(),
userInfo: getUserInfo()
}
};
const conversionValue = getConversionValue();
if (conversionValue) result.conversionValue = conversionValue;
return result;
}

function getConversionRuleUrn() {
Expand All @@ -169,16 +171,17 @@ function getConversionHappenedAt() {

function getConversionValue() {
const hasItems = getType(eventData.items) === 'array' && !!eventData.items[0];
const itemsCurrency = hasItems ? eventData.items[0].currency : 'NA?';
const itemsCurrency = hasItems ? eventData.items[0].currency : '';
const currencyCode =
eventDataOverride.currency || eventData.currency || itemsCurrency;
if (!currencyCode) return null;
const itemsValue = hasItems
? eventData.items.reduce((acc, item) => {
const price = item.price || 0;
const quantity = item.quantity || 1;
return acc + price * quantity;
}, 0)
: 0;
const currencyCode =
eventDataOverride.currency || eventData.currency || itemsCurrency;
const amount = eventDataOverride.amount || eventData.value || itemsValue;
return {
currencyCode: currencyCode,
Expand Down
19 changes: 11 additions & 8 deletions template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ if (isLoggingEnabled) {
}

// 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()){
if (validateUserData()) {
sendConversionToLinkedIn();
} else {
if (isLoggingEnabled) {
Expand All @@ -383,7 +383,8 @@ if (validateUserData()){
TraceId: traceId,
EventName: postBody.eventId,
Message: 'No conversion event was sent to LinkedIn CAPI.',
Reason: '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.',
Reason:
'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.'
});
}

Expand All @@ -398,7 +399,6 @@ function validateUserData() {
return postBody.user.userInfo.firstName && postBody.user.userInfo.lastName;
}


function sendConversionToLinkedIn() {
sendHttpRequest(
postUrl,
Expand Down Expand Up @@ -445,16 +445,18 @@ function getRequestHeaders() {
}

function getPostBody() {
return {
const result = {
conversion: getConversionRuleUrn(),
conversionHappenedAt: getConversionHappenedAt(),
conversionValue: getConversionValue(),
eventId: getEventId(),
user: {
userIds: getUserIds(),
userInfo: getUserInfo()
}
};
const conversionValue = getConversionValue();
if (conversionValue) result.conversionValue = conversionValue;
return result;
}

function getConversionRuleUrn() {
Expand All @@ -472,16 +474,17 @@ function getConversionHappenedAt() {

function getConversionValue() {
const hasItems = getType(eventData.items) === 'array' && !!eventData.items[0];
const itemsCurrency = hasItems ? eventData.items[0].currency : 'NA?';
const itemsCurrency = hasItems ? eventData.items[0].currency : '';
const currencyCode =
eventDataOverride.currency || eventData.currency || itemsCurrency;
if (!currencyCode) return null;
const itemsValue = hasItems
? eventData.items.reduce((acc, item) => {
const price = item.price || 0;
const quantity = item.quantity || 1;
return acc + price * quantity;
}, 0)
: 0;
const currencyCode =
eventDataOverride.currency || eventData.currency || itemsCurrency;
const amount = eventDataOverride.amount || eventData.value || itemsValue;
return {
currencyCode: currencyCode,
Expand Down

0 comments on commit 58a4245

Please sign in to comment.