Skip to content

Commit

Permalink
chore: migrate to new amplitude node sdk (#1322)
Browse files Browse the repository at this point in the history
<!-- For Coveo Employees only. Fill this section.

CDX-1472
-->

## Proposed changes

Tldr;
https://www.docs.developers.amplitude.com/data/sdks/typescript-node/migration/

Also refactor dup code between core and commons, removed duplicate from
core and make it point to commons

## Breaking changes

`@coveo/cli-commons` some function sig changed, flagged as such in
appropriate commits, will rebase merge.

## Testing

- [x] Unit Tests
  • Loading branch information
louis-bompart authored Sep 8, 2023
1 parent bc067ff commit c2a59ff
Show file tree
Hide file tree
Showing 13 changed files with 78 additions and 627 deletions.
55 changes: 39 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/cli/commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"typescript": "4.9.5"
},
"dependencies": {
"@amplitude/node": "1.10.2",
"@amplitude/analytics-node": "^1.3.3",
"@amplitude/analytics-types": "^2.1.2",
"@coveo/platform-client": "44.1.0",
"@oclif/core": "1.24.0",
"abortcontroller-polyfill": "1.7.5",
Expand Down
13 changes: 3 additions & 10 deletions packages/cli/commons/src/analytics/amplitudeClient.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import {init, NodeClient} from '@amplitude/node';
import {init} from '@amplitude/analytics-node';
export {flush, track} from '@amplitude/analytics-node';

const analyticsAPIKey = 'af28cba7acfd392c324bebd399e2d9ea';

export interface AmplitudeClient extends NodeClient {
identified: boolean;
}

// TODO: CDX-667: support proxy
export const amplitudeClient = init(analyticsAPIKey);

export const flush = async () => {
await amplitudeClient.flush();
};
init(analyticsAPIKey);
27 changes: 13 additions & 14 deletions packages/cli/commons/src/analytics/identifier.spec.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,41 @@
jest.mock('@amplitude/node');
jest.mock('@amplitude/analytics-node');
jest.mock('@amplitude/identify');
jest.mock('@coveo/platform-client');
jest.mock('../platform/authenticatedClient');
jest.mock('../config/config');
jest.mock('../config/globalConfig');

import os from 'os';
import {Identify} from '@amplitude/identify';
import {
Identify,
identify as amplitudeIdentify,
} from '@amplitude/analytics-node';
import {Config, Configuration} from '../config/config';
import {AuthenticatedClient} from '../platform/authenticatedClient';
import {Identifier} from './identifier';
import PlatformClient from '@coveo/platform-client';
import {configurationMock, defaultConfiguration} from '../config/stub';
import type {Interfaces} from '@oclif/core';
import type {NodeClient} from '@amplitude/node';
import globalConfig from '../config/globalConfig';

describe('identifier', () => {
const mockedGlobalConfig = jest.mocked(globalConfig);
const mockedConfig = jest.mocked(Config);
const mockedIdentify = jest.mocked(Identify);
const mockedIdentifyClass = jest.mocked(Identify);
const mockedAmplitudeIdentifyFn = jest.mocked(amplitudeIdentify);
const mockedAuthenticatedClient = jest.mocked(AuthenticatedClient);
const mockedPlatformClient = jest.mocked(PlatformClient);
const mockUserGet = jest.fn();
const mockSetIdentity = jest.fn();
const mockedLogEvent = jest.fn();
const mockedOsVersion = jest.spyOn(os, 'release');

let identity: Awaited<ReturnType<Identifier['getIdentity']>>;

const getDummyAmplitudeClient = () =>
({
logEvent: mockedLogEvent,
} as unknown as NodeClient);

const doMockOS = () => {
mockedOsVersion.mockReturnValue('21.3.4');
};
const doMockIdentify = () => {
mockedIdentify.prototype.set.mockImplementation(mockSetIdentity);
mockedIdentifyClass.prototype.set.mockImplementation(mockSetIdentity);
};
const doMockPlatformClient = (email = '') => {
mockedPlatformClient.mockImplementation(
Expand Down Expand Up @@ -191,17 +188,19 @@ describe('identifier', () => {
describe('when logging for every user type', () => {
beforeEach(async () => {
identity = await new Identifier().getIdentity();
identity.identify(getDummyAmplitudeClient());
identity.identify();
});

it('should add the CLI version to the event', async () => {
expect(mockedLogEvent).toHaveBeenCalledWith(
expect(mockedAmplitudeIdentifyFn).toHaveBeenCalledWith(
expect.any(Identify),
expect.objectContaining({app_version: '1.2.3'})
);
});

it('should add the OS information to the event', async () => {
expect(mockedLogEvent).toHaveBeenCalledWith(
expect(mockedAmplitudeIdentifyFn).toHaveBeenCalledWith(
expect.any(Identify),
expect.objectContaining({
app_version: '1.2.3',
os_name: 'darwin',
Expand Down
16 changes: 10 additions & 6 deletions packages/cli/commons/src/analytics/identifier.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import os from 'os';
import {Identify} from '@amplitude/identify';
import {
Identify,
identify as amplitudeIdentify,
} from '@amplitude/analytics-node';
import {machineId} from 'node-machine-id';
import {createHash} from 'crypto';
import {AuthenticatedClient} from '../platform/authenticatedClient';
import PlatformClient from '@coveo/platform-client';
import {camelToSnakeCase} from '../utils/string';
import type {NodeClient} from '@amplitude/node';
import globalConfig from '../config/globalConfig';
import {Configuration} from '../config/config';
import type {EventOptions} from '@amplitude/analytics-types';

export class Identifier {
private authenticatedClient: AuthenticatedClient;
Expand Down Expand Up @@ -35,13 +38,14 @@ export class Identifier {
identifier.set(camelToSnakeCase(key), value);
});

const identify = (amplitudeClient: NodeClient) => {
const identifyEvent = {
...identifier.identifyUser(userId, deviceId),
const identify = () => {
const identifyEvent: EventOptions = {
user_id: userId,
device_id: deviceId,
...this.getAmplitudeBaseEventProperties(),
...this.getOrganizationIdentifier(),
};
amplitudeClient.logEvent(identifyEvent);
amplitudeIdentify(identifier, identifyEvent);
};

return {userId, deviceId, identify};
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"node": "^16.13.0 || ^18.12.0"
},
"dependencies": {
"@amplitude/analytics-node": "^1.3.3",
"@amplitude/identify": "^1.9.0",
"@amplitude/node": "^1.9.0",
"@coveo/cli-commons": "2.6.0",
"@coveo/cli-plugin-source": "2.0.12",
"@coveo/platform-client": "44.1.0",
Expand Down Expand Up @@ -40,6 +40,7 @@
"tslib": "2.5.0"
},
"devDependencies": {
"@amplitude/analytics-types": "^2.1.2",
"@amplitude/types": "1.10.2",
"@babel/core": "7.21.5",
"@coveo/angular": "1.36.0",
Expand Down
10 changes: 0 additions & 10 deletions packages/cli/core/src/hooks/analytics/amplitudeClient.ts

This file was deleted.

Loading

0 comments on commit c2a59ff

Please sign in to comment.