From 50a7e83415c67d186e7b6028aedfe35bf52cc9f0 Mon Sep 17 00:00:00 2001 From: Anant Jain Date: Tue, 30 Jul 2024 11:16:30 +0530 Subject: [PATCH 1/3] feat: criteo add support for multiple hash methods --- .../src/integrations/Criteo/utils.js | 41 +++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/packages/analytics-js-integrations/src/integrations/Criteo/utils.js b/packages/analytics-js-integrations/src/integrations/Criteo/utils.js index 13983ae8a5..6307c8738b 100644 --- a/packages/analytics-js-integrations/src/integrations/Criteo/utils.js +++ b/packages/analytics-js-integrations/src/integrations/Criteo/utils.js @@ -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 @@ -35,7 +65,6 @@ const handleCommonFields = (rudderElement, hashMethod) => { const { message } = rudderElement; const { properties, userId, anonymousId } = message; - const setEmail = {}; const setZipcode = {}; const finalRequest = [ @@ -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) { From cd089eea572cd7c259f2cdb4ce8374f8ab758295 Mon Sep 17 00:00:00 2001 From: Anant Jain Date: Wed, 31 Jul 2024 11:01:59 +0530 Subject: [PATCH 2/3] chore: add util test cases --- .../integrations/Criteo/utils.test.js | 40 ++++++++++++++++++- .../src/integrations/Criteo/utils.js | 2 + 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/analytics-js-integrations/__tests__/integrations/Criteo/utils.test.js b/packages/analytics-js-integrations/__tests__/integrations/Criteo/utils.test.js index f012901d3c..eac4ea8f98 100644 --- a/packages/analytics-js-integrations/__tests__/integrations/Criteo/utils.test.js +++ b/packages/analytics-js-integrations/__tests__/integrations/Criteo/utils.test.js @@ -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 = { @@ -175,3 +181,35 @@ describe('handleProductAdded', () => { expect(finalPayload[0].item[0].quantity).toBe(5); }); }); + +describe('getEmailHashes', () => { + // Returns correct hashes when hashMethod is 'both' + it('should return correct hashes when hashMethod is "both"', () => { + const email = 'test@example.com'; + 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 email = 'test@example.com'; + const hashMethod = 'sha256'; + const expectedHash = sha256(email).toString(); + const result = getEmailHashes(email, hashMethod); + expect(result).toEqual([{ event: 'setEmail', hash_method: 'sha256', email: expectedHash }]); + }); +}); diff --git a/packages/analytics-js-integrations/src/integrations/Criteo/utils.js b/packages/analytics-js-integrations/src/integrations/Criteo/utils.js index 6307c8738b..bd17e54875 100644 --- a/packages/analytics-js-integrations/src/integrations/Criteo/utils.js +++ b/packages/analytics-js-integrations/src/integrations/Criteo/utils.js @@ -415,6 +415,8 @@ export { handleProductView, generateExtraData, handleCommonFields, + getEmail, getProductInfo, + getEmailHashes, handleProductAdded, }; From fa58765abf9890354a7adcd49d6a6c35b30bea5b Mon Sep 17 00:00:00 2001 From: Anant Jain Date: Thu, 1 Aug 2024 10:13:55 +0530 Subject: [PATCH 3/3] chore: add util test cases+1 --- .../__tests__/integrations/Criteo/utils.test.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/analytics-js-integrations/__tests__/integrations/Criteo/utils.test.js b/packages/analytics-js-integrations/__tests__/integrations/Criteo/utils.test.js index eac4ea8f98..5a6bcbd737 100644 --- a/packages/analytics-js-integrations/__tests__/integrations/Criteo/utils.test.js +++ b/packages/analytics-js-integrations/__tests__/integrations/Criteo/utils.test.js @@ -183,9 +183,9 @@ describe('handleProductAdded', () => { }); describe('getEmailHashes', () => { + const email = 'test@example.com'; // Returns correct hashes when hashMethod is 'both' it('should return correct hashes when hashMethod is "both"', () => { - const email = 'test@example.com'; const hashMethod = 'both'; const result = getEmailHashes(email, hashMethod); expect(result).toEqual([ @@ -206,10 +206,15 @@ describe('getEmailHashes', () => { }); it('should return correct hash when hashMethod is sha256', () => { - const email = 'test@example.com'; 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 }]); + }); });