Skip to content

Commit

Permalink
fix: remove redundant ids and userIdentifier when gbraid or wbraid ar…
Browse files Browse the repository at this point in the history
…e there (#3910)

* fix: remove redundant ids and userIdentifier when gbraid or wbraid are there

* chore: address comment

* chore: fix sonar cloud issue

* chore: fix sonar cloud issue

* chore: decrease Cognitive Complexity from 17 to the 15

* chore: fix lint error

* chore: specify head commit hash for checkout temporarily

* chore: fix fetch depth

* chore: fix fetch depth to 0

* chore: reset commit sha

---------

Co-authored-by: Sai Kumar Battinoju <[email protected]>
  • Loading branch information
ItsSudip and saikumarrs authored Dec 6, 2024
1 parent f44f148 commit 313710c
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 46 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dt-test-and-report-code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Checkout
uses: actions/[email protected]
with:
fetch-depth: 1
fetch-depth: 0

- name: Setup Node
uses: actions/[email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
getClickConversionPayloadAndEndpoint,
getConsentsDataFromIntegrationObj,
getCallConversionPayload,
updateConversion,
} = require('./utils');
const helper = require('./helper');

Expand Down Expand Up @@ -48,6 +49,9 @@ const getConversions = (message, metadata, { Config }, event, conversionType) =>
filteredCustomerId,
eventLevelConsentsData,
);
convertedPayload.payload.conversions[0] = updateConversion(
convertedPayload.payload.conversions[0],
);
payload = convertedPayload.payload;
endpoint = convertedPayload.endpoint;
} else if (conversionType === 'store') {
Expand Down
62 changes: 51 additions & 11 deletions src/v0/destinations/google_adwords_offline_conversions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,35 @@ const getStoreConversionPayload = (message, Config, event) => {
return payload;
};

const hasClickId = (conversion) => {
const { gbraid, wbraid, gclid } = conversion;
return gclid || wbraid || gbraid;
};
const populateUserIdentifier = ({ email, phone, properties, payload, UserIdentifierSource }) => {
const copiedPayload = cloneDeep(payload);
// userIdentifierSource
// if userIdentifierSource doesn't exist in properties
// then it is taken from the webapp config
if (!properties.userIdentifierSource && UserIdentifierSource !== 'none') {
set(
copiedPayload,
'conversions[0].userIdentifiers[0].userIdentifierSource',
UserIdentifierSource,
);
// one of email or phone must be provided when none of gclid, wbraid and gbraid provided
}
if (!email && !phone) {
if (!hasClickId(copiedPayload.conversions[0])) {
throw new InstrumentationError(
`Either an email address or a phone number is required for user identification when none of gclid, wbraid, or gbraid is provided.`,
);
} else {
// we are deleting userIdentifiers if any one of gclid, wbraid and gbraid is there but email or phone is not present
delete copiedPayload.conversions[0].userIdentifiers;
}
}
return copiedPayload;
};
const getClickConversionPayloadAndEndpoint = (
message,
Config,
Expand All @@ -335,7 +364,7 @@ const getClickConversionPayloadAndEndpoint = (
updatedClickMapping = removeHashToSha256TypeFromMappingJson(updatedClickMapping);
}

const payload = constructPayload(message, updatedClickMapping);
let payload = constructPayload(message, updatedClickMapping);

const endpoint = CLICK_CONVERSION.replace(':customerId', filteredCustomerId);

Expand All @@ -353,17 +382,8 @@ const getClickConversionPayloadAndEndpoint = (
set(payload, 'conversions[0].cartData.items', itemList);
}

// userIdentifierSource
// if userIdentifierSource doesn't exist in properties
// then it is taken from the webapp config
if (!properties.userIdentifierSource && UserIdentifierSource !== 'none') {
set(payload, 'conversions[0].userIdentifiers[0].userIdentifierSource', UserIdentifierSource);
payload = populateUserIdentifier({ email, phone, properties, payload, UserIdentifierSource });

// one of email or phone must be provided
if (!email && !phone) {
throw new InstrumentationError(`Either of email or phone is required for user identifier`);
}
}
// either of email or phone should be passed
// defaultUserIdentifier depends on the webapp configuration
// Ref - https://developers.google.com/google-ads/api/rest/reference/rest/v11/customers/uploadClickConversions#ClickConversion
Expand Down Expand Up @@ -411,6 +431,25 @@ const getConsentsDataFromIntegrationObj = (message) => {
return integrationObj?.consents || {};
};

/**
* remove redundant ids
* @param {*} conversionCopy
*/
const updateConversion = (conversion) => {
const conversionCopy = cloneDeep(conversion);
if (conversionCopy.gclid) {
delete conversionCopy.wbraid;
delete conversionCopy.gbraid;
}
if (conversionCopy.wbraid && conversionCopy.gbraid) {
throw new InstrumentationError(`You can't use both wbraid and gbraid.`);
}
if (conversionCopy.wbraid || conversionCopy.gbraid) {
delete conversionCopy.userIdentifiers;
}
return conversionCopy;
};

module.exports = {
validateDestinationConfig,
generateItemListFromProducts,
Expand All @@ -423,4 +462,5 @@ module.exports = {
getExisitingUserIdentifier,
getConsentsDataFromIntegrationObj,
getCallConversionPayload,
updateConversion,
};
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ describe('getClickConversionPayloadAndEndpoint util tests', () => {
};
expect(() =>
getClickConversionPayloadAndEndpoint(fittingPayload, config, '9625812972'),
).toThrow('Either of email or phone is required for user identifier');
).toThrow(
'Either an email address or a phone number is required for user identification when none of gclid, wbraid, or gbraid is provided.',
);
});

it('finaliseConsent', () => {
Expand Down
Loading

0 comments on commit 313710c

Please sign in to comment.