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: amplitude add support for unset #2941

Merged
merged 10 commits into from
Dec 26, 2023
11 changes: 10 additions & 1 deletion src/v0/destinations/am/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down Expand Up @@ -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
Expand Down
57 changes: 57 additions & 0 deletions src/v0/destinations/am/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { getUnsetObj } = require('./utils');

describe('getUnsetObj', () => {
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();
});
});
26 changes: 26 additions & 0 deletions src/v0/destinations/am/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,31 @@ const getEventId = (payload, sourceKey) => {
return undefined;
};

/**
* generates the unsetObject and returns it
sanpj2292 marked this conversation as resolved.
Show resolved Hide resolved
* @param {*} message
* @returns
*
* Example message = {
integrations: {
Amplitude: { fieldsToUnset: ['Unset1', 'Unset2'] },
All: true,
},
};
return unsetObj = {
"Unset1": "-",
"Unset2": "-"
}
*/
const getUnsetObj = (message) => {
const fieldsToUnset = get(message, 'integrations.Amplitude.fieldsToUnset');
let unsetObject;
if (fieldsToUnset) {
sanpj2292 marked this conversation as resolved.
Show resolved Hide resolved
unsetObject = Object.fromEntries(fieldsToUnset.map((field) => [field, '-']));
}

return unsetObject;
};
module.exports = {
getOSName,
getOSVersion,
Expand All @@ -90,4 +115,5 @@ module.exports = {
getPlatform,
getBrand,
getEventId,
getUnsetObj,
};
14 changes: 11 additions & 3 deletions test/integrations/destinations/am/processor/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ export const data = [
{
message: {
channel: 'web',
integrations: {
Amplitude: { fieldsToUnset: ['email'] },
All: true,
},
context: {
externalId: [
{
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -791,6 +796,9 @@ export const data = [
insert_id: '84e26acc-56a5-4835-8233-591137fca468',
ip: '0.0.0.0',
user_properties: {
$unset: {
'testObj.unsetField1': '-',
yashasvibajpai marked this conversation as resolved.
Show resolved Hide resolved
},
initial_referrer: 'https://docs.rudderstack.com',
initial_referring_domain: 'docs.rudderstack.com',
anonymousId: '123456',
Expand Down
Loading