Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions packages/cli-repl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,25 +99,24 @@ bus.emit('mongosh:connect', {
})
```

### bus.on('mongosh:new-user', telemetryAnonymousId, enableTelemetry)
Where `telemetryAnonymousId` is a [BSON ObjectID][object-id] and `enableTelemetry` is a boolean flag.
This is used for telemetry tracking when the user initially uses mongosh.
### bus.on('mongosh:new-user', telemetryUserIdentity, enableTelemetry)
Where `telemetryUserIdentity` is `userId` and `anonymousId` which are both a [BSON ObjectID][object-id].
And `enableTelemetry` is a boolean flag.
This is used internally to update telemetry preferences.

Example:
```js
bus.emit('mongosh:new-user', '12394dfjvnaw3uw3erdf', true)
bus.emit('mongosh:new-user', { userId: '12394dfjvnaw3uw3erdf', anonymousId: '12394dfjvnaw3uw3erdf' }, true)
```

### bus.on('mongosh:update-user', telemetryUserIdentity, enableTelemetry)
Initially, we used `userId` as Segment user identifier, but this usage is being deprecated.
The `anonymousId` should be used instead. We keep sending `userId` to Segment for old users though to preserve their analytics.
Where `userID`/`anonymousId` is a [BSON ObjectID][object-id] and `enableTelemetry` is a boolean flag.
This is used internally to update telemetry preferences and `userID`/`anonymousId` in the
logger.
Where `telemetryUserIdentity` is `userId` and `anonymousId` which are both a [BSON ObjectID][object-id].
And `enableTelemetry` is a boolean flag.
This is used internally to update telemetry preferences.

Example:
```js
bus.emit('mongosh:update-user', { userId: undefined, anonymousId: '12394dfjvnaw3uw3erdf' } , false)
bus.emit('mongosh:update-user', { userId: '12394dfjvnaw3uw3erdf', anonymousId: null } , false)
```

### bus.on('mongosh:error', error)
Expand Down
18 changes: 9 additions & 9 deletions packages/cli-repl/src/cli-repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,23 +113,23 @@ describe('CliRepl', () => {
it('does not store config options on disk that have not been changed', async() => {
let content = await fs.readFile(path.join(tmpdir.path, 'config'), { encoding: 'utf8' });
expect(Object.keys(EJSON.parse(content))).to.deep.equal([
'telemetryAnonymousId', 'enableTelemetry', 'disableGreetingMessage'
'userId', 'telemetryAnonymousId', 'enableTelemetry', 'disableGreetingMessage'
]);

input.write('config.set("inspectDepth", config.get("inspectDepth"))\n');

await waitEval(cliRepl.bus);
content = await fs.readFile(path.join(tmpdir.path, 'config'), { encoding: 'utf8' });
expect(Object.keys(EJSON.parse(content))).to.deep.equal([
'telemetryAnonymousId', 'enableTelemetry', 'disableGreetingMessage', 'inspectDepth'
'userId', 'telemetryAnonymousId', 'enableTelemetry', 'disableGreetingMessage', 'inspectDepth'
]);

// When a new REPL is created:
cliRepl = new CliRepl(cliReplOptions);
await cliRepl.start('', {});
content = await fs.readFile(path.join(tmpdir.path, 'config'), { encoding: 'utf8' });
expect(Object.keys(EJSON.parse(content))).to.deep.equal([
'telemetryAnonymousId', 'enableTelemetry', 'disableGreetingMessage', 'inspectDepth'
'userId', 'telemetryAnonymousId', 'enableTelemetry', 'disableGreetingMessage', 'inspectDepth'
]);
});

Expand Down Expand Up @@ -286,16 +286,16 @@ describe('CliRepl', () => {
});

context('during startup', () => {
it('persists telemetryAnonymousId', async() => {
const telemetryAnonymousIds: string[] = [];
it('persists userId and telemetryAnonymousId', async() => {
const telemetryUserIdentitys: { userId?: string; anonymousId?: string }[] = [];
for (let i = 0; i < 2; i++) {
cliRepl = new CliRepl(cliReplOptions);
cliRepl.bus.on('mongosh:new-user', telemetryAnonymousId => telemetryAnonymousIds.push(telemetryAnonymousId));
cliRepl.bus.on('mongosh:update-user', telemetryUserIdentity => telemetryAnonymousIds.push(telemetryUserIdentity.anonymousId));
cliRepl.bus.on('mongosh:new-user', telemetryUserIdentity => telemetryUserIdentitys.push(telemetryUserIdentity));
cliRepl.bus.on('mongosh:update-user', telemetryUserIdentity => telemetryUserIdentitys.push(telemetryUserIdentity));
await cliRepl.start('', {});
}
expect(telemetryAnonymousIds).to.have.lengthOf(2);
expect([...new Set(telemetryAnonymousIds)]).to.have.lengthOf(1);
expect(telemetryUserIdentitys).to.have.lengthOf(2);
expect(telemetryUserIdentitys[0]).to.deep.equal(telemetryUserIdentitys[1])
});

it('emits error for invalid config', async() => {
Expand Down
11 changes: 7 additions & 4 deletions packages/cli-repl/src/cli-repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export type CliReplOptions = {
} & Pick<MongoshNodeReplOptions, 'nodeReplOptions'>;

/** The set of config options that is *always* available in config files stored on the file system. */
type CliUserConfigOnDisk = Partial<CliUserConfig> & Pick<CliUserConfig, 'enableTelemetry' | 'telemetryAnonymousId'>;
type CliUserConfigOnDisk = Partial<CliUserConfig> & Pick<CliUserConfig, 'enableTelemetry' | 'userId' | 'telemetryAnonymousId'>;

/**
* The REPL used from the terminal.
Expand Down Expand Up @@ -104,8 +104,11 @@ class CliRepl implements MongoshIOProvider {
this.output = options.output;
this.analyticsOptions = options.analyticsOptions;
this.onExit = options.onExit;

const id = new bson.ObjectId().toString();
this.config = {
telemetryAnonymousId: new bson.ObjectId().toString(),
userId: id,
telemetryAnonymousId: id,
enableTelemetry: true
};

Expand All @@ -118,11 +121,11 @@ class CliRepl implements MongoshIOProvider {
})
.on('new-config', (config: CliUserConfigOnDisk) => {
this.setTelemetryEnabled(config.enableTelemetry);
this.bus.emit('mongosh:new-user', config.telemetryAnonymousId);
this.bus.emit('mongosh:new-user', { userId: config.userId, anonymousId: config.telemetryAnonymousId });
})
.on('update-config', (config: CliUserConfigOnDisk) => {
this.setTelemetryEnabled(config.enableTelemetry);
this.bus.emit('mongosh:update-user', { userId: config.userId, anonymousId: config.telemetryAnonymousId ?? config.userId });
this.bus.emit('mongosh:update-user', { userId: config.userId, anonymousId: config.telemetryAnonymousId });
});

this.mongocryptdManager = new MongocryptdManager(
Expand Down
1 change: 1 addition & 0 deletions packages/cli-repl/test/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ describe('e2e', function() {
describe('config file', () => {
it('sets up a config file', async() => {
const config = await readConfig();
expect(config.userId).to.match(/^[a-f0-9]{24}$/);
expect(config.telemetryAnonymousId).to.match(/^[a-f0-9]{24}$/);
expect(config.enableTelemetry).to.be.true;
expect(config.disableGreetingMessage).to.be.true;
Expand Down
14 changes: 7 additions & 7 deletions packages/logging/src/analytics-helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@ describe('ToggleableAnalytics', () => {
const toggleable = new ToggleableAnalytics(target);
expect(events).to.have.lengthOf(0);

toggleable.identify({ anonymousId: 'me', traits: { platform: '1234' } });
toggleable.track({ anonymousId: 'me', event: 'something', properties: { mongosh_version: '1.2.3' } });
toggleable.identify({ userId: 'me', traits: { platform: '1234' } });
toggleable.track({ userId: 'me', event: 'something', properties: { mongosh_version: '1.2.3' } });
expect(events).to.have.lengthOf(0);

toggleable.enable();
expect(events).to.have.lengthOf(2);

toggleable.track({ anonymousId: 'me', event: 'something2', properties: { mongosh_version: '1.2.3' } });
toggleable.track({ userId: 'me', event: 'something2', properties: { mongosh_version: '1.2.3' } });
expect(events).to.have.lengthOf(3);

toggleable.pause();
toggleable.track({ anonymousId: 'me', event: 'something3', properties: { mongosh_version: '1.2.3' } });
toggleable.track({ userId: 'me', event: 'something3', properties: { mongosh_version: '1.2.3' } });
expect(events).to.have.lengthOf(3);

toggleable.disable();
expect(events).to.have.lengthOf(3);
toggleable.enable();

expect(events).to.deep.equal([
[ 'identify', { anonymousId: 'me', traits: { platform: '1234' } } ],
[ 'track', { anonymousId: 'me', event: 'something', properties: { mongosh_version: '1.2.3' } } ],
[ 'track', { anonymousId: 'me', event: 'something2', properties: { mongosh_version: '1.2.3' } } ]
[ 'identify', { userId: 'me', traits: { platform: '1234' } } ],
[ 'track', { userId: 'me', event: 'something', properties: { mongosh_version: '1.2.3' } } ],
[ 'track', { userId: 'me', event: 'something2', properties: { mongosh_version: '1.2.3' } } ]
]);
});
});
14 changes: 8 additions & 6 deletions packages/logging/src/analytics-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
export type MongoshAnalyticsIdentity = {
userId: string;
} | {
anonymousId: string;
}

/**
* General interface for an Analytics provider that mongosh can use.
*/
export interface MongoshAnalytics {
identify(message: {
userId?: string,
anonymousId: string,
identify(message: MongoshAnalyticsIdentity & {
traits: { platform: string }
}): void;

track(message: {
userId?: string,
anonymousId: string,
track(message: MongoshAnalyticsIdentity & {
event: string,
properties: {
// eslint-disable-next-line camelcase
Expand Down
10 changes: 5 additions & 5 deletions packages/logging/src/setup-logger-and-telemetry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('setupLoggerAndTelemetry', () => {
let analyticsOutput: ['identify'|'track'|'log', any][];
let bus: MongoshBus;

const telemetryAnonymousId = '53defe995fa47e6c13102d9d';
const userId = '53defe995fa47e6c13102d9d';
const logId = '5fb3c20ee1507e894e5340f3';

const logger = new MongoLogWriter(logId, `/tmp/${logId}_log`, {
Expand All @@ -36,8 +36,8 @@ describe('setupLoggerAndTelemetry', () => {
expect(logOutput).to.have.lengthOf(0);
expect(analyticsOutput).to.be.empty;

bus.emit('mongosh:new-user', telemetryAnonymousId);
bus.emit('mongosh:update-user', { anonymousId: telemetryAnonymousId });
bus.emit('mongosh:new-user', { userId, anonymousId: userId });
bus.emit('mongosh:update-user', { userId, anonymousId: userId });
bus.emit('mongosh:connect', {
uri: 'mongodb://localhost/',
is_localhost: true,
Expand Down Expand Up @@ -364,7 +364,7 @@ describe('setupLoggerAndTelemetry', () => {
expect(logOutput).to.have.lengthOf(0);
expect(analyticsOutput).to.be.empty;

bus.emit('mongosh:new-user', telemetryAnonymousId);
bus.emit('mongosh:new-user', { userId, anonymousId: userId });

logOutput = [];
analyticsOutput = [];
Expand Down Expand Up @@ -454,7 +454,7 @@ describe('setupLoggerAndTelemetry', () => {
],
]);

bus.emit('mongosh:new-user', telemetryAnonymousId);
bus.emit('mongosh:new-user', { userId, anonymousId: userId });
logOutput = [];
analyticsOutput = [];

Expand Down
Loading