Skip to content

Commit

Permalink
feat: amplitude add support for unset (#2941)
Browse files Browse the repository at this point in the history
* feat: amplitude add support for unset

* Update src/v0/destinations/am/transform.js

Co-authored-by: Yashasvi Bajpai <[email protected]>

* Update src/v0/destinations/am/transform.js

Co-authored-by: Yashasvi Bajpai <[email protected]>

* chore:comment addresed

* Update transform.js

* chore:comment addresed

* chore: added docs

* Update utils.js

---------

Co-authored-by: Yashasvi Bajpai <[email protected]>
  • Loading branch information
anantjain45823 and yashasvibajpai authored Dec 26, 2023
1 parent ca76297 commit 429ca71
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 4 deletions.
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
66 changes: 66 additions & 0 deletions src/v0/destinations/am/util.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
27 changes: 27 additions & 0 deletions src/v0/destinations/am/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -90,4 +116,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': '-',
},
initial_referrer: 'https://docs.rudderstack.com',
initial_referring_domain: 'docs.rudderstack.com',
anonymousId: '123456',
Expand Down

0 comments on commit 429ca71

Please sign in to comment.