Skip to content

Commit

Permalink
fix: global attribute values being affected when checking event attri…
Browse files Browse the repository at this point in the history
…butes (#27)

Co-authored-by: xiaoweii <[email protected]>
  • Loading branch information
zhu-xiaowei and xiaoweii authored Sep 25, 2023
1 parent 0e47d74 commit 9aa90af
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/provider/AnalyticsEventBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,25 @@ export class AnalyticsEventBuilder {
eventAttributes: ClickstreamAttribute,
globalAttributes = {}
): ClickstreamAttribute {
const resultAttributes: ClickstreamAttribute = globalAttributes;
const customAttributes: ClickstreamAttribute = {};
const { checkAttributes } = EventChecker;
const globalAttributesLength = Object.keys(globalAttributes).length;
for (const key in eventAttributes) {
const value = eventAttributes[key];
if (value !== null) {
const currentNumber = Object.keys(resultAttributes).length;
const currentNumber =
Object.keys(customAttributes).length + globalAttributesLength;
const result = checkAttributes(currentNumber, key, value);
const { ERROR_CODE, ERROR_MESSAGE } = Event.ReservedAttribute;
if (result.error_code > 0) {
resultAttributes[ERROR_CODE] = result.error_code;
resultAttributes[ERROR_MESSAGE] = result.error_message;
customAttributes[ERROR_CODE] = result.error_code;
customAttributes[ERROR_MESSAGE] = result.error_message;
} else {
resultAttributes[key] = value;
customAttributes[key] = value;
}
}
}
return resultAttributes;
return Object.assign(customAttributes, globalAttributes);
}

static getEventItemsWithCheck(
Expand Down
18 changes: 18 additions & 0 deletions test/provider/AnalyticsEventBuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,22 @@ describe('AnalyticsEventBuilder test', () => {
Event.ErrorCode.ITEM_VALUE_LENGTH_EXCEED
);
});

test('test check event attributes will not affect global attributes', () => {
const customAttributes: ClickstreamAttribute = {
testKey: 'testValue',
testKey1: 'testValue1',
};
const globalAttributes = {
level: 5,
_traffic_source_medium: 'Search engine',
};
const resultAttributes = AnalyticsEventBuilder.getEventAttributesWithCheck(
customAttributes,
globalAttributes
);
expect('level' in resultAttributes).toBeTruthy();
expect('_traffic_source_medium' in resultAttributes).toBeTruthy();
expect(Object.keys(globalAttributes).length).toBe(2);
});
});

0 comments on commit 9aa90af

Please sign in to comment.