Skip to content

Commit

Permalink
use one segment instance (#10915)
Browse files Browse the repository at this point in the history
  • Loading branch information
brad-decker authored Apr 26, 2021
1 parent d97a9e8 commit f1825e8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 24 deletions.
16 changes: 8 additions & 8 deletions app/scripts/controllers/metametrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ export default class MetaMetricsController {
/**
* @param {Object} segment - an instance of analytics-node for tracking
* events that conform to the new MetaMetrics tracking plan.
* @param {Object} segmentLegacy - an instance of analytics-node for
* tracking legacy schema events. Will eventually be phased out
* @param {Object} preferencesStore - The preferences controller store, used
* to access and subscribe to preferences that will be attached to events
* @param {function} onNetworkDidChange - Used to attach a listener to the
Expand All @@ -73,7 +71,6 @@ export default class MetaMetricsController {
*/
constructor({
segment,
segmentLegacy,
preferencesStore,
onNetworkDidChange,
getCurrentChainId,
Expand Down Expand Up @@ -105,7 +102,6 @@ export default class MetaMetricsController {
this.network = getNetworkIdentifier();
});
this.segment = segment;
this.segmentLegacy = segmentLegacy;
}

generateMetaMetricsId() {
Expand Down Expand Up @@ -260,6 +256,12 @@ export default class MetaMetricsController {
}
payload[idType] = idValue;

// If this is an event on the old matomo schema, add a key to the payload
// to designate it as such
if (matomoEvent === true) {
payload.properties.legacy_event = true;
}

// Promises will only resolve when the event is sent to segment. For any
// event that relies on this promise being fulfilled before performing UI
// updates, or otherwise delaying user interaction, supply the
Expand All @@ -278,11 +280,9 @@ export default class MetaMetricsController {
return resolve();
};

const target = matomoEvent === true ? this.segmentLegacy : this.segment;

target.track(payload, callback);
this.segment.track(payload, callback);
if (flushImmediately) {
target.flush();
this.segment.flush();
}
});
}
Expand Down
6 changes: 2 additions & 4 deletions app/scripts/controllers/metametrics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import MetaMetricsController from './metametrics';
import { NETWORK_EVENTS } from './network';

const segment = createSegmentMock(2, 10000);
const segmentLegacy = createSegmentMock(2, 10000);

const VERSION = '0.0.1-test';
const NETWORK = 'Mainnet';
Expand Down Expand Up @@ -91,7 +90,6 @@ function getMetaMetricsController({
} = {}) {
return new MetaMetricsController({
segment,
segmentLegacy,
getNetworkIdentifier: networkController.getNetworkIdentifier.bind(
networkController,
),
Expand Down Expand Up @@ -286,7 +284,7 @@ describe('MetaMetricsController', function () {
});

it('should track a legacy event', function () {
const mock = sinon.mock(segmentLegacy);
const mock = sinon.mock(segment);
const metaMetricsController = getMetaMetricsController();
mock
.expects('track')
Expand All @@ -297,6 +295,7 @@ describe('MetaMetricsController', function () {
context: DEFAULT_TEST_CONTEXT,
properties: {
test: 1,
legacy_event: true,
...DEFAULT_EVENT_PROPERTIES,
},
});
Expand Down Expand Up @@ -544,7 +543,6 @@ describe('MetaMetricsController', function () {
afterEach(function () {
// flush the queues manually after each test
segment.flush();
segmentLegacy.flush();
sinon.restore();
});
});
10 changes: 0 additions & 10 deletions app/scripts/lib/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const isDevOrTestEnvironment = Boolean(
process.env.METAMASK_DEBUG || process.env.IN_TEST,
);
const SEGMENT_WRITE_KEY = process.env.SEGMENT_WRITE_KEY ?? null;
const SEGMENT_LEGACY_WRITE_KEY = process.env.SEGMENT_LEGACY_WRITE_KEY ?? null;
const SEGMENT_HOST = process.env.SEGMENT_HOST ?? null;

// flushAt controls how many events are sent to segment at once. Segment will
Expand Down Expand Up @@ -90,12 +89,3 @@ export const segment =
flushAt: SEGMENT_FLUSH_AT,
flushInterval: SEGMENT_FLUSH_INTERVAL,
});

export const segmentLegacy =
!SEGMENT_LEGACY_WRITE_KEY || (isDevOrTestEnvironment && !SEGMENT_HOST)
? createSegmentMock(SEGMENT_FLUSH_AT, SEGMENT_FLUSH_INTERVAL)
: new Analytics(SEGMENT_LEGACY_WRITE_KEY, {
host: SEGMENT_HOST,
flushAt: SEGMENT_FLUSH_AT,
flushInterval: SEGMENT_FLUSH_INTERVAL,
});
3 changes: 1 addition & 2 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import nodeify from './lib/nodeify';
import accountImporter from './account-import-strategies';
import seedPhraseVerifier from './lib/seed-phrase-verifier';
import MetaMetricsController from './controllers/metametrics';
import { segment, segmentLegacy } from './lib/segment';
import { segment } from './lib/segment';
import createMetaRPCHandler from './lib/createMetaRPCHandler';

export const METAMASK_CONTROLLER_EVENTS = {
Expand Down Expand Up @@ -128,7 +128,6 @@ export default class MetamaskController extends EventEmitter {

this.metaMetricsController = new MetaMetricsController({
segment,
segmentLegacy,
preferencesStore: this.preferencesController.store,
onNetworkDidChange: this.networkController.on.bind(
this.networkController,
Expand Down

0 comments on commit f1825e8

Please sign in to comment.