Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: criteo add support for multiple hash methods #1812

Merged
merged 4 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import md5 from 'md5';
import { handleCommonFields, handleProductAdded } from '../../../src/integrations/Criteo/utils';
import sha256 from 'crypto-js/sha256';
import {
handleCommonFields,
handleProductAdded,
getEmailHashes,
getEmail,
} from '../../../src/integrations/Criteo/utils';

describe('handleCommonFields', () => {
const inputEvent = {
Expand Down Expand Up @@ -175,3 +181,40 @@ describe('handleProductAdded', () => {
expect(finalPayload[0].item[0].quantity).toBe(5);
});
});

describe('getEmailHashes', () => {
const email = '[email protected]';
// Returns correct hashes when hashMethod is 'both'
it('should return correct hashes when hashMethod is "both"', () => {
const hashMethod = 'both';
const result = getEmailHashes(email, hashMethod);
expect(result).toEqual([
{ event: 'setEmail', hash_method: 'sha256', email: getEmail(email, 'sha256') },
{ event: 'setEmail', hash_method: 'md5', email: getEmail(email, 'md5') },
]);
});

// Handles empty email input
it('should handle empty email input', () => {
const email = '';
const hashMethod = 'both';
const result = getEmailHashes(email, hashMethod);
expect(result).toEqual([
{ event: 'setEmail', hash_method: 'sha256', email: getEmail(email, 'sha256') },
{ event: 'setEmail', hash_method: 'md5', email: getEmail(email, 'md5') },
]);
});

it('should return correct hash when hashMethod is sha256', () => {
const hashMethod = 'sha256';
const expectedHash = sha256(email).toString();
const result = getEmailHashes(email, hashMethod);
expect(result).toEqual([{ event: 'setEmail', hash_method: 'sha256', email: expectedHash }]);
});

it('should return correct hash when hashMethod is none', () => {
const hashMethod = 'none';
const result = getEmailHashes(email, hashMethod);
expect(result).toEqual([{ event: 'setEmail', email }]);
});
});
anantjain45823 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,40 @@ const getEmail = (email, hashMethod) => {
if (hashMethod === 'sha256') {
return sha256(email).toString();
}

return email;
};

/**
* This function generates different hashes based upon hashMethod and returns an array of object
* Criteo Docs: https://help.criteo.com/kb/guide/en/intro-to-the-criteo-onetag-8fjCDwCENw/Steps/775595,853887,2616497,2616569,2616738,2617588
* @param {*} email
* @param {*} hashMethod
* @returns
*/
const getEmailHashes = (email, hashMethod) => {
const emailHashMethods = [];
const emailHashes = [];
if (hashMethod === 'both') {
emailHashMethods.push('sha256', 'md5');
} else if (hashMethod === 'none') {
// in case customer don't want to hash email before sending
emailHashes.push({
event: 'setEmail',
email,
});
return emailHashes;
} else {
emailHashMethods.push(hashMethod);
}
emailHashMethods.forEach(method => {
emailHashes.push({
event: 'setEmail',
hash_method: method,
email: getEmail(email, method),
});
});
return emailHashes;
};
anantjain45823 marked this conversation as resolved.
Show resolved Hide resolved
/**
* Ref : https://help.criteo.com/kb/guide/en/all-criteo-onetag-events-and-parameters-vZbzbEeY86/Steps/775825,868657,868659
* Ref : https://help.criteo.com/kb/guide/en/all-criteo-onetag-events-and-parameters-vZbzbEeY86/Steps/775825
Expand All @@ -35,7 +65,6 @@ const handleCommonFields = (rudderElement, hashMethod) => {
const { message } = rudderElement;
const { properties, userId, anonymousId } = message;

const setEmail = {};
const setZipcode = {};

const finalRequest = [
Expand All @@ -45,12 +74,8 @@ const handleCommonFields = (rudderElement, hashMethod) => {

if (properties?.email) {
const email = properties.email.trim().toLowerCase();
setEmail.event = 'setEmail';
if (hashMethod !== 'none') {
setEmail.hash_method = hashMethod;
}
setEmail.email = getEmail(email, hashMethod);
finalRequest.push(setEmail);
const emailHashes = getEmailHashes(email, hashMethod);
finalRequest.push(...emailHashes);
}

if (properties?.zipCode) {
Expand Down Expand Up @@ -390,6 +415,8 @@ export {
handleProductView,
generateExtraData,
handleCommonFields,
getEmail,
getProductInfo,
getEmailHashes,
handleProductAdded,
};