1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-25 20:02:58 +01:00

add an extra identifier on anonymized duplicate events (#17080)

This commit is contained in:
Brad Decker 2023-01-05 09:49:55 -05:00 committed by Dan Miller
parent da18f15950
commit 6ac899b96a
2 changed files with 201 additions and 5 deletions

View File

@ -35,15 +35,18 @@ const defaultCaptureException = (err) => {
// The function is used to build a unique messageId for segment messages // The function is used to build a unique messageId for segment messages
// It uses actionId and uniqueIdentifier from event if present // It uses actionId and uniqueIdentifier from event if present
const buildUniqueMessageId = (args) => { const buildUniqueMessageId = (args) => {
let messageId = ''; const messageIdParts = [];
if (args.uniqueIdentifier) { if (args.uniqueIdentifier) {
messageId += `${args.uniqueIdentifier}-`; messageIdParts.push(args.uniqueIdentifier);
} }
if (args.actionId) { if (args.actionId) {
messageId += args.actionId; messageIdParts.push(args.actionId);
} }
if (messageId.length) { if (messageIdParts.length && args.isDuplicateAnonymizedEvent) {
return messageId; messageIdParts.push('0x000');
}
if (messageIdParts.length) {
return messageIdParts.join('-');
} }
return generateRandomId(); return generateRandomId();
}; };
@ -536,6 +539,7 @@ export default class MetaMetricsController {
this._buildEventPayload({ this._buildEventPayload({
...payload, ...payload,
properties: combinedProperties, properties: combinedProperties,
isDuplicateAnonymizedEvent: true,
}), }),
{ ...options, excludeMetaMetricsId: true }, { ...options, excludeMetaMetricsId: true },
), ),

View File

@ -159,6 +159,7 @@ describe('MetaMetricsController', function () {
clock = sinon.useFakeTimers(now.getTime()); clock = sinon.useFakeTimers(now.getTime());
sinon.stub(Utils, 'generateRandomId').returns('DUMMY_RANDOM_ID'); sinon.stub(Utils, 'generateRandomId').returns('DUMMY_RANDOM_ID');
}); });
describe('constructor', function () { describe('constructor', function () {
it('should properly initialize', function () { it('should properly initialize', function () {
const mock = sinon.mock(segment); const mock = sinon.mock(segment);
@ -681,6 +682,197 @@ describe('MetaMetricsController', function () {
}); });
}); });
describe('deterministic messageId', function () {
it('should use the actionId as messageId when provided', function () {
const metaMetricsController = getMetaMetricsController();
const spy = sinon.spy(segment, 'track');
metaMetricsController.submitEvent({
event: 'Fake Event',
category: 'Unit Test',
properties: { foo: 'bar' },
actionId: '0x001',
});
assert.ok(spy.calledOnce);
assert.ok(
spy.calledWith({
event: 'Fake Event',
userId: TEST_META_METRICS_ID,
context: DEFAULT_TEST_CONTEXT,
properties: {
foo: 'bar',
...DEFAULT_EVENT_PROPERTIES,
},
messageId: '0x001',
timestamp: new Date(),
}),
);
});
it('should append 0x000 to the actionId of anonymized event when tracking sensitiveProperties', function () {
const metaMetricsController = getMetaMetricsController();
const spy = sinon.spy(segment, 'track');
metaMetricsController.submitEvent({
event: 'Fake Event',
category: 'Unit Test',
sensitiveProperties: { foo: 'bar' },
actionId: '0x001',
});
assert.ok(spy.calledTwice);
assert.ok(
spy.calledWith({
event: 'Fake Event',
anonymousId: METAMETRICS_ANONYMOUS_ID,
context: DEFAULT_TEST_CONTEXT,
properties: {
foo: 'bar',
...DEFAULT_EVENT_PROPERTIES,
},
messageId: '0x001-0x000',
timestamp: new Date(),
}),
);
assert.ok(
spy.calledWith({
event: 'Fake Event',
userId: TEST_META_METRICS_ID,
context: DEFAULT_TEST_CONTEXT,
properties: {
...DEFAULT_EVENT_PROPERTIES,
},
messageId: '0x001',
timestamp: new Date(),
}),
);
});
it('should use the uniqueIdentifier as messageId when provided', function () {
const metaMetricsController = getMetaMetricsController();
const spy = sinon.spy(segment, 'track');
metaMetricsController.submitEvent({
event: 'Fake Event',
category: 'Unit Test',
properties: { foo: 'bar' },
uniqueIdentifier: 'transaction-submitted-0000',
});
assert.ok(spy.calledOnce);
assert.ok(
spy.calledWith({
event: 'Fake Event',
userId: TEST_META_METRICS_ID,
context: DEFAULT_TEST_CONTEXT,
properties: {
foo: 'bar',
...DEFAULT_EVENT_PROPERTIES,
},
messageId: 'transaction-submitted-0000',
timestamp: new Date(),
}),
);
});
it('should append 0x000 to the uniqueIdentifier of anonymized event when tracking sensitiveProperties', function () {
const metaMetricsController = getMetaMetricsController();
const spy = sinon.spy(segment, 'track');
metaMetricsController.submitEvent({
event: 'Fake Event',
category: 'Unit Test',
sensitiveProperties: { foo: 'bar' },
uniqueIdentifier: 'transaction-submitted-0000',
});
assert.ok(spy.calledTwice);
assert.ok(
spy.calledWith({
event: 'Fake Event',
anonymousId: METAMETRICS_ANONYMOUS_ID,
context: DEFAULT_TEST_CONTEXT,
properties: {
foo: 'bar',
...DEFAULT_EVENT_PROPERTIES,
},
messageId: 'transaction-submitted-0000-0x000',
timestamp: new Date(),
}),
);
assert.ok(
spy.calledWith({
event: 'Fake Event',
userId: TEST_META_METRICS_ID,
context: DEFAULT_TEST_CONTEXT,
properties: {
...DEFAULT_EVENT_PROPERTIES,
},
messageId: 'transaction-submitted-0000',
timestamp: new Date(),
}),
);
});
it('should combine the uniqueIdentifier and actionId as messageId when both provided', function () {
const metaMetricsController = getMetaMetricsController();
const spy = sinon.spy(segment, 'track');
metaMetricsController.submitEvent({
event: 'Fake Event',
category: 'Unit Test',
properties: { foo: 'bar' },
actionId: '0x001',
uniqueIdentifier: 'transaction-submitted-0000',
});
assert.ok(spy.calledOnce);
assert.ok(
spy.calledWith({
event: 'Fake Event',
userId: TEST_META_METRICS_ID,
context: DEFAULT_TEST_CONTEXT,
properties: {
foo: 'bar',
...DEFAULT_EVENT_PROPERTIES,
},
messageId: 'transaction-submitted-0000-0x001',
timestamp: new Date(),
}),
);
});
it('should append 0x000 to the combined uniqueIdentifier and actionId of anonymized event when tracking sensitiveProperties', function () {
const metaMetricsController = getMetaMetricsController();
const spy = sinon.spy(segment, 'track');
metaMetricsController.submitEvent({
event: 'Fake Event',
category: 'Unit Test',
sensitiveProperties: { foo: 'bar' },
actionId: '0x001',
uniqueIdentifier: 'transaction-submitted-0000',
});
assert.ok(spy.calledTwice);
assert.ok(
spy.calledWith({
event: 'Fake Event',
anonymousId: METAMETRICS_ANONYMOUS_ID,
context: DEFAULT_TEST_CONTEXT,
properties: {
foo: 'bar',
...DEFAULT_EVENT_PROPERTIES,
},
messageId: 'transaction-submitted-0000-0x001-0x000',
timestamp: new Date(),
}),
);
assert.ok(
spy.calledWith({
event: 'Fake Event',
userId: TEST_META_METRICS_ID,
context: DEFAULT_TEST_CONTEXT,
properties: {
...DEFAULT_EVENT_PROPERTIES,
},
messageId: 'transaction-submitted-0000-0x001',
timestamp: new Date(),
}),
);
});
});
describe('_buildUserTraitsObject', function () { describe('_buildUserTraitsObject', function () {
it('should return full user traits object on first call', function () { it('should return full user traits object on first call', function () {
const MOCK_ALL_TOKENS = { const MOCK_ALL_TOKENS = {