Skip to content

Commit

Permalink
feat: criteo add support for multiple hash methods (#1812)
Browse files Browse the repository at this point in the history
* feat: criteo add support for multiple hash methods

* chore: add util test cases

* chore: add util test cases+1
  • Loading branch information
anantjain45823 authored Aug 1, 2024
1 parent 1a8fee8 commit 23a0e37
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
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 }]);
});
});
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;
};
/**
* 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,
};

0 comments on commit 23a0e37

Please sign in to comment.