diff --git a/src/v0/destinations/am/transform.js b/src/v0/destinations/am/transform.js index 05a130d6e0..911ec51be0 100644 --- a/src/v0/destinations/am/transform.js +++ b/src/v0/destinations/am/transform.js @@ -297,6 +297,15 @@ const identifyBuilder = (message, destination, rawPayload) => { } }); } + // update identify call request with unset fields + // AM docs https://www.docs.developers.amplitude.com/analytics/apis/http-v2-api/#keys-for-the-event-argument:~:text=exceed%2040%20layers.-,user_properties,-Optional.%20Object.%20A + const unsetObject = AMUtils.getUnsetObj(message); + if (unsetObject) { + // Example unsetObject = { + // "testObj.del1": "-" + // } + set(rawPayload, `user_properties.$unset`, unsetObject); + } return rawPayload; }; @@ -334,7 +343,7 @@ const getResponseData = (evType, destination, rawPayload, message, groupInfo) => case EventType.IDENTIFY: // event_type for identify event is $identify rawPayload.event_type = IDENTIFY_AM; - identifyBuilder(message, destination, rawPayload); + rawPayload = identifyBuilder(message, destination, rawPayload); break; case EventType.GROUP: // event_type for identify event is $identify diff --git a/src/v0/destinations/am/util.test.js b/src/v0/destinations/am/util.test.js new file mode 100644 index 0000000000..faaa9170f0 --- /dev/null +++ b/src/v0/destinations/am/util.test.js @@ -0,0 +1,66 @@ +const { getUnsetObj } = require('./utils'); + +describe('getUnsetObj', () => { + it("should return undefined when 'message.integrations.Amplitude.fieldsToUnset' is not array", () => { + const message = { + integrations: { + Amplitude: { fieldsToUnset: 'field_name' }, + }, + }; + const result = getUnsetObj(message); + expect(result).toBeUndefined(); + }); + it("should return undefined when 'message.integrations.Amplitude.fieldsToUnset' is undefined", () => { + const message = { + integrations: { + Amplitude: {}, + }, + }; + const result = getUnsetObj(message); + expect(result).toBeUndefined(); + }); + + it("should return an empty objecty when 'message.integrations.Amplitude.fieldsToUnset' is an empty array", () => { + const message = { + integrations: { + Amplitude: { fieldsToUnset: [] }, + }, + }; + const result = getUnsetObj(message); + expect(result).toEqual({}); + }); + + it("should return an object with keys and values set to '-' when 'message.integrations.Amplitude.fieldsToUnset' is an array of strings", () => { + const message = { + integrations: { + Amplitude: { fieldsToUnset: ['Unset1', 'Unset2'] }, + }, + }; + const result = getUnsetObj(message); + expect(result).toEqual({ + Unset1: '-', + Unset2: '-', + }); + }); + + it("should handle missing 'message' parameter", () => { + const result = getUnsetObj(); + expect(result).toBeUndefined(); + }); + + // Should handle missing 'integrations' property in 'message' parameter + it("should handle missing 'integrations' property in 'message' parameter", () => { + const message = {}; + const result = getUnsetObj(message); + expect(result).toBeUndefined(); + }); + + // Should handle missing 'Amplitude' property in 'message.integrations' parameter + it("should handle missing 'Amplitude' property in 'message.integrations' parameter", () => { + const message = { + integrations: {}, + }; + const result = getUnsetObj(message); + expect(result).toBeUndefined(); + }); +}); diff --git a/src/v0/destinations/am/utils.js b/src/v0/destinations/am/utils.js index b9925c20d8..71fe0ab459 100644 --- a/src/v0/destinations/am/utils.js +++ b/src/v0/destinations/am/utils.js @@ -82,6 +82,32 @@ const getEventId = (payload, sourceKey) => { return undefined; }; +/** + * generates the unsetObject and returns it + * @param {*} message + * @returns + * + * Example message = { + integrations: { + Amplitude: { fieldsToUnset: ['Unset1', 'Unset2'] }, + All: true, + }, + }; + return unsetObj = { + "Unset1": "-", + "Unset2": "-" + } + AM docs: https://www.docs.developers.amplitude.com/analytics/apis/http-v2-api/#keys-for-the-event-argument:~:text=exceed%2040%20layers.-,user_properties,-Optional.%20Object.%20A + */ +const getUnsetObj = (message) => { + const fieldsToUnset = get(message, 'integrations.Amplitude.fieldsToUnset'); + let unsetObject; + if (Array.isArray(fieldsToUnset)) { + unsetObject = Object.fromEntries(fieldsToUnset.map((field) => [field, '-'])); + } + + return unsetObject; +}; module.exports = { getOSName, getOSVersion, @@ -90,4 +116,5 @@ module.exports = { getPlatform, getBrand, getEventId, + getUnsetObj, }; diff --git a/test/integrations/destinations/am/processor/data.ts b/test/integrations/destinations/am/processor/data.ts index 5e941e07f8..f28606da0c 100644 --- a/test/integrations/destinations/am/processor/data.ts +++ b/test/integrations/destinations/am/processor/data.ts @@ -488,6 +488,10 @@ export const data = [ { message: { channel: 'web', + integrations: { + Amplitude: { fieldsToUnset: ['email'] }, + All: true, + }, context: { externalId: [ { @@ -562,9 +566,6 @@ export const data = [ originalTimestamp: '2019-10-14T09:03:17.562Z', anonymousId: '123456', userId: '123456', - integrations: { - All: true, - }, sentAt: '2019-10-14T09:03:22.563Z', }, metadata: { @@ -616,6 +617,9 @@ export const data = [ insert_id: '84e26acc-56a5-4835-8233-591137fca468', ip: '0.0.0.0', user_properties: { + $unset: { + email: '-', + }, initial_referrer: 'https://docs.rudderstack.com', initial_referring_domain: 'docs.rudderstack.com', anonymousId: '123456', @@ -743,6 +747,7 @@ export const data = [ anonymousId: '123456', userId: '123456', integrations: { + Amplitude: { fieldsToUnset: ['testObj.unsetField1'] }, All: true, }, sentAt: '2019-10-14T09:03:22.563Z', @@ -791,6 +796,9 @@ export const data = [ insert_id: '84e26acc-56a5-4835-8233-591137fca468', ip: '0.0.0.0', user_properties: { + $unset: { + 'testObj.unsetField1': '-', + }, initial_referrer: 'https://docs.rudderstack.com', initial_referring_domain: 'docs.rudderstack.com', anonymousId: '123456',