Skip to content

Commit

Permalink
fix: review comments address
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 committed Mar 11, 2024
1 parent 3d4634e commit e192352
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const CONVERSION_CUSTOM_VARIABLE_CACHE_TTL = process.env.CONVERSION_CUSTOM_VARIA

const MAPPING_CONFIG = getMappingConfig(CONFIG_CATEGORIES, __dirname);

const consentFields = ['adUserData', 'adPersonalization'];

module.exports = {
trackClickConversionsMapping:
MAPPING_CONFIG[CONFIG_CATEGORIES.TRACK_CLICK_CONVERSIONS_CONFIG.name],
Expand All @@ -58,4 +60,5 @@ module.exports = {
MAPPING_CONFIG[CONFIG_CATEGORIES.TRACK_STORE_CONVERSION_CONFIG_ADD_CONVERSION.name],
trackAddStoreAddressConversionsMapping:
MAPPING_CONFIG[CONFIG_CATEGORIES.TRACK_STORE_ADDRESS_IDENTIFIER.name],
consentFields,
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,31 @@ const {
defaultBatchRequestConfig,
getSuccessRespEvents,
combineBatchRequestsWithSameJobIds,
getIntegrationsObj,
} = require('../../util');
const {
CALL_CONVERSION,
trackCallConversionsMapping,
STORE_CONVERSION_CONFIG,
consentFields,
} = require('./config');
const {
validateDestinationConfig,
getStoreConversionPayload,
requestBuilder,
getClickConversionPayloadAndEndpoint,
populateConsentForGAOC,
} = require('./utils');
const { finaliseConsent } = require('../../util/googleUtils');
const helper = require('./helper');

const getConsentFromIntegrationObj = (message, conversionType) => {
const integrationObj =
conversionType === 'store'
? {}
: getIntegrationsObj(message, 'GOOGLE_ADWORDS_OFFLINE_CONVERSIONS') || {};
return integrationObj?.consents || {};
};

/**
* get conversions depending on the type set from dashboard
* i.e click conversions or call conversions
Expand All @@ -42,21 +52,24 @@ const getConversions = (message, metadata, { Config }, event, conversionType) =>
const { properties, timestamp, originalTimestamp } = message;

const filteredCustomerId = removeHyphens(customerId);
const userSentConsentValues = getConsentFromIntegrationObj(message, conversionType);

if (conversionType === 'click') {
// click conversion
const convertedPayload = getClickConversionPayloadAndEndpoint(
message,
Config,
filteredCustomerId,
userSentConsentValues,
);
payload = convertedPayload.payload;
endpoint = convertedPayload.endpoint;
} else if (conversionType === 'store') {
payload = getStoreConversionPayload(message, Config, filteredCustomerId);
payload = getStoreConversionPayload(message, Config, filteredCustomerId, userSentConsentValues);
endpoint = STORE_CONVERSION_CONFIG.replace(':customerId', filteredCustomerId);
} else {
// call conversions
const consentObject = populateConsentForGAOC(message, conversionType, Config);
const consentObject = finaliseConsent(userSentConsentValues, Config, consentFields);
payload = constructPayload(message, trackCallConversionsMapping);
endpoint = CALL_CONVERSION.replace(':customerId', filteredCustomerId);
payload.conversions[0].consent = consentObject;
Expand Down Expand Up @@ -122,7 +135,6 @@ const trackResponseBuilder = (message, metadata, destination) => {

const process = async (event) => {
const { message, metadata, destination } = event;

if (!message.type) {
throw new InstrumentationError('Message type is not present. Aborting message.');
}
Expand Down
21 changes: 13 additions & 8 deletions src/v0/destinations/google_adwords_offline_conversions/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ const {
trackAddStoreAddressConversionsMapping,
trackClickConversionsMapping,
CLICK_CONVERSION,
consentFields,
} = require('./config');
const { processAxiosResponse } = require('../../../adapters/utils/networkUtils');
const Cache = require('../../util/cache');
const helper = require('./helper');
const { populateConsentForGAOC } = require('../../util/googleUtils');
const { finaliseConsent } = require('../../util/googleUtils');

const conversionActionIdCache = new Cache(CONVERSION_ACTION_ID_CACHE_TTL);

Expand Down Expand Up @@ -221,7 +222,7 @@ function getExisitingUserIdentifier(userIdentifierInfo, defaultUserIdentifier) {
* This Function create the add conversion payload
* and returns the payload
*/
const getAddConversionPayload = (message, Config) => {
const getAddConversionPayload = (message, Config, eventLevelConsent) => {
const { properties } = message;
const { validateOnly, hashUserIdentifier, defaultUserIdentifier } = Config;
const payload = constructPayload(message, trackAddStoreConversionsMapping);
Expand Down Expand Up @@ -274,24 +275,29 @@ const getAddConversionPayload = (message, Config) => {
}
}
// add consent support for store conversions
const consentObject = populateConsentForGAOC(message, 'store', Config);
const consentObject = finaliseConsent(eventLevelConsent, Config, consentFields);
set(payload, 'operations.create.consent', consentObject);
return payload;
};

const getStoreConversionPayload = (message, Config, event) => {
const getStoreConversionPayload = (message, Config, event, eventLevelConsent) => {
const { validateOnly } = Config;
const payload = {
event,
isStoreConversion: true,
createJobPayload: getCreateJobPayload(message),
addConversionPayload: getAddConversionPayload(message, Config),
addConversionPayload: getAddConversionPayload(message, Config, eventLevelConsent),
executeJobPayload: { validate_only: validateOnly },
};
return payload;
};

const getClickConversionPayloadAndEndpoint = (message, Config, filteredCustomerId) => {
const getClickConversionPayloadAndEndpoint = (
message,
Config,
filteredCustomerId,
eventLevelConsent,
) => {
const email = getFieldValueFromMessage(message, 'emailOnly');
const phone = getFieldValueFromMessage(message, 'phone');
const { hashUserIdentifier, defaultUserIdentifier, UserIdentifierSource, conversionEnvironment } =
Expand Down Expand Up @@ -365,7 +371,7 @@ const getClickConversionPayloadAndEndpoint = (message, Config, filteredCustomerI
}

// add consent support for click conversions
const consentObject = populateConsentForGAOC(message, 'click', Config);
const consentObject = finaliseConsent(eventLevelConsent, Config, consentFields);
set(payload, 'conversions[0].consent', consentObject);
return { payload, endpoint };
};
Expand All @@ -380,5 +386,4 @@ module.exports = {
buildAndGetAddress,
getClickConversionPayloadAndEndpoint,
getExisitingUserIdentifier,
populateConsentForGAOC,
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const {
getClickConversionPayloadAndEndpoint,
buildAndGetAddress,
getExisitingUserIdentifier,
populateConsentForGAOC,
} = require('./utils');

const getTestMessage = () => {
Expand Down Expand Up @@ -246,7 +245,7 @@ describe('getClickConversionPayloadAndEndpoint util tests', () => {
).toThrow('Either of email or phone is required for user identifier');
});

it('populateConsentForGAOC', () => {
it('finaliseConsent', () => {
let fittingPayload = { ...getTestMessage() };
fittingPayload.properties.products = [
{
Expand Down
17 changes: 4 additions & 13 deletions src/v0/util/googleUtils/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const { getIntegrationsObj } = require('..');

const GOOGLE_ALLOWED_CONSENT_STATUS = ['UNSPECIFIED', 'UNKNOWN', 'GRANTED', 'DENIED'];

const UNSPECIFIED_CONSENT = 'UNSPECIFIED';
Expand Down Expand Up @@ -55,12 +53,8 @@ const populateConsentFromConfig = (config) => {
* b) https://developers.google.com/google-ads/api/reference/rpc/v16/UserData#consent
*/

const populateConsentForGAOC = (message, conversionType, destConfig) => {
const integrationObj =
conversionType === 'store'
? {}
: getIntegrationsObj(message, 'GOOGLE_ADWORDS_OFFLINE_CONVERSIONS') || {};
const consents = integrationObj?.consents || {};
const finaliseConsent = (eventLevelConsent, destConfig, destinationAllowedConsentKeys) => {
const consents = eventLevelConsent || {};

const defaultConsentBlock = populateConsentFromConfig(destConfig);

Expand All @@ -75,11 +69,8 @@ const populateConsentForGAOC = (message, conversionType, destConfig) => {
return defaultConsentBlock[consentType] || UNKNOWN_CONSENT;
};

// Common consent fields to process
const consentFields = ['adUserData', 'adPersonalization'];

// Construct consentObj based on the common consent fields
const consentObj = consentFields.reduce((obj, consentType) => {
const consentObj = destinationAllowedConsentKeys.reduce((obj, consentType) => {
// eslint-disable-next-line no-param-reassign
obj[consentType] = processConsent(consentType);
return obj;
Expand All @@ -93,5 +84,5 @@ module.exports = {
UNSPECIFIED_CONSENT,
UNKNOWN_CONSENT,
GOOGLE_ALLOWED_CONSENT_STATUS,
populateConsentForGAOC,
finaliseConsent,
};
Loading

0 comments on commit e192352

Please sign in to comment.