Skip to content

Commit

Permalink
Merge pull request #837 from NordicSemiconductor/enhance/telemetry_api
Browse files Browse the repository at this point in the history
Enhance telemetry API
  • Loading branch information
datenreisender authored Jan 23, 2024
2 parents 2e604e3 + 48960e2 commit 1d8f67e
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 60 deletions.
25 changes: 24 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ This project does _not_ adhere to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html) but contrary to it
every new version is a new major version.

## 151.0.0 - 2024-01-23

### Changed

- Renamed exported object `usageData` to `telemetry` and type
`UsageDataMetadata` to `TelemetryMetadata`.
- Renamed several function in the `telemetry` object:
- `enable()``setUsersAgreedToTelemetry(true)`
- `disable()``setUsersAgreedToTelemetry(false)`
- `reset()``setUsersWithdrewTelemetryAgreement()`
- `isEnabled()``getIsSendingTelemetry()` (which now does not log
anymore)
- `sendUsageData()``sendEvent()`
- In the component `ErrorBoundary` the property `sendUsageData` is renamed to
`sendTelemetryEvent`.

### Steps to upgrade when using this package

- If they are imported from shared, rename `usageData` and `UsageDataMetadata`
as well as the renamed functions mentioned above.
- In usages of the component `ErrorBoundary`, rename the property
`sendUsageData` to `sendTelemetryEvent`.

## 150.0.0 - 2024-01-18

### Removed
Expand Down Expand Up @@ -216,7 +239,7 @@ every new version is a new major version.

### Fixed

- Telemtry: Metadata was not removed on request, when being in the main
- Telemetry: Metadata was not removed on request, when being in the main
process. This is not critical because this code isn't yet executed in real
life.

Expand Down
4 changes: 2 additions & 2 deletions nrfutil/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export class NrfutilSandbox {
) =>
new Promise<void>((resolve, reject) => {
let aborting = false;
telemetry.sendUsageData(`running nrfutil ${this.module}`, {
telemetry.sendEvent(`running nrfutil ${this.module}`, {
args,
exec: command,
});
Expand Down Expand Up @@ -513,7 +513,7 @@ export class NrfutilSandbox {
) =>
new Promise<void>((resolve, reject) => {
let aborting = false;
telemetry.sendUsageData(`running nrfutil ${this.module}`, {
telemetry.sendEvent(`running nrfutil ${this.module}`, {
args,
exec: command,
});
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nordicsemiconductor/pc-nrfconnect-shared",
"version": "150.0.0",
"version": "151.0.0",
"description": "Shared commodities for developing pc-nrfconnect-* packages",
"repository": {
"type": "git",
Expand All @@ -25,7 +25,7 @@
"prepare": "ts-node scripts/installHusky.ts",
"release-shared": "ts-node scripts/release-shared.ts",
"prepare-shared-release": "ts-node scripts/prepare-shared-release.ts",
"clean": "rimraf dist typings/generated dist scripts/nordic-publish.js",
"clean": "rimraf dist typings/generated scripts/nordic-publish.js",
"postinstall": "ts-node scripts/postinstall.ts"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/Device/DeviceSelector/DeviceSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default ({
const doDeselectDevice = useCallback(
(device?: Device) => {
if (device) {
telemetry.sendUsageData(
telemetry.sendEvent(
'device deselected ',
simplifyDevice(device)
);
Expand Down Expand Up @@ -120,7 +120,7 @@ export default ({
dispatch(setSelectedDeviceInfo(deviceInfo));
onDeviceSelected(device, autoReselected);

telemetry.sendUsageData('device selected', {
telemetry.sendEvent('device selected', {
device: simplifyDevice(device),
deviceInfo,
});
Expand Down
2 changes: 1 addition & 1 deletion src/Device/deviceLister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ export const startWatchingDevices =

const action = async (device: Device) => {
if (hasValidDeviceTraits(device.traits, deviceListing)) {
telemetry.sendUsageData(
telemetry.sendEvent(
'device connected',
simplifyDevice(device)
);
Expand Down
6 changes: 3 additions & 3 deletions src/ErrorBoundary/ErrorBoundary.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ describe('ErrorBoundary', () => {
});

it('can take custom reporting functions', () => {
const sendUsageData = jest.fn();
const sendTelemetryEvent = jest.fn();

render(
<ErrorBoundary sendUsageData={sendUsageData}>
<ErrorBoundary sendTelemetryEvent={sendTelemetryEvent}>
<Child />
</ErrorBoundary>
);
expect(sendUsageData).toHaveBeenCalled();
expect(sendTelemetryEvent).toHaveBeenCalled();
});

it('should render error boundary component when there is an error', () => {
Expand Down
14 changes: 9 additions & 5 deletions src/ErrorBoundary/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface Props {
devices?: Device[];
appName?: string;
restoreDefaults?: () => void;
sendUsageData?: (message: string) => void;
sendTelemetryEvent?: (message: string) => void;
}

const genericRestoreDefaults = () => {
Expand Down Expand Up @@ -61,11 +61,15 @@ class ErrorBoundary extends React.Component<
}

componentDidCatch(error: Error) {
const { devices, selectedDevice, selectedSerialNumber, sendUsageData } =
this.props;
const {
devices,
selectedDevice,
selectedSerialNumber,
sendTelemetryEvent,
} = this.props;

sendUsageData != null
? sendUsageData(error.message)
sendTelemetryEvent != null
? sendTelemetryEvent(error.message)
: sendErrorReport(error.message);

generateSystemReport(
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export {
export { openUrl, openFile } from './utils/open';
export { default as systemReport } from './utils/systemReport';

export { default as usageData } from './telemetry/telemetry';
export { type default as UsageDataMetadata } from './telemetry/TelemetryMetadata';
export { default as telemetry } from './telemetry/telemetry';
export { type default as TelemetryMetadata } from './telemetry/TelemetryMetadata';
export { default as classNames } from './utils/classNames';
export { truncateMiddle } from './utils/truncateMiddle';

Expand Down
50 changes: 24 additions & 26 deletions src/telemetry/TelemetrySender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,44 +33,42 @@ export default abstract class TelemetrySender {
this.isTelemetryAllowedForCurrentApp &&
getHasUserAgreedToTelemetry() === true;

/**
* @deprecated Use `getIsSendingTelemetry` instead
* @returns {boolean} If telemetry is enabled
*/
isEnabled() {
const isSendingTelemetry = this.getIsSendingTelemetry();
this.logger?.debug(`Telemetry is ${isSendingTelemetry}`);

return isSendingTelemetry;
}

async enable() {
persistHasUserAgreedToTelemetry(true);
this.sendUsageData('Telemetry Opt-In');
async sendAgreementEvent() {
this.sendEvent('Telemetry Opt-In');

const { platform, arch } = await si.osInfo();
this.sendUsageData('Report OS info', { platform, arch });
this.sendEvent('Report OS info', { platform, arch });
}

this.logger?.debug('Usage data has been enabled');
sendDisagreementEvent() {
this.sendMinimalEvent('Telemetry Opt-Out');
}

disable() {
persistHasUserAgreedToTelemetry(false);
this.sendMinimalUsageData('Telemetry Opt-Out');
this.logger?.debug('Usage data has been disabled');
async setUsersAgreedToTelemetry(hasAgreed: boolean) {
persistHasUserAgreedToTelemetry(hasAgreed);

if (hasAgreed) {
await this.sendAgreementEvent();
} else {
this.sendDisagreementEvent();
}

this.logger?.debug(
`Telemetry has been ${hasAgreed ? 'enabled' : 'disabled'}`
);
}

reset() {
setUsersWithdrewTelemetryAgreement() {
deleteHasUserAgreedToTelemetry();
this.sendMinimalUsageData('Telemetry Opt-Reset');
this.logger?.debug('Usage data setting has been reset');
this.sendMinimalEvent('Telemetry Opt-Reset');
this.logger?.debug('Telemetry has been reset');
}

sendMinimalUsageData(action: string) {
this.sendUsageData(action, { removeAllMetadata: true });
sendMinimalEvent(action: string) {
this.sendEvent(action, { removeAllMetadata: true });
}

abstract sendUsageData(
abstract sendEvent(
action: string,
metadata?: TelemetryMetadata
): MaybePromise<void>;
Expand Down
4 changes: 2 additions & 2 deletions src/telemetry/TelemetrySenderInMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ export default class TelemetrySenderInMain extends TelemetrySender {

getClient = () => this.client ?? this.initClient();

sendUsageData = (action: string, metadata?: TelemetryMetadata) => {
sendEvent = (action: string, metadata?: TelemetryMetadata) => {
this.getClient().trackEvent({ name: action, properties: metadata });
this.logger?.debug(`Sending usage data ${JSON.stringify(action)}`);
this.logger?.debug(`Sending event ${JSON.stringify(action)}`);
};

sendPageView = (pageName: string) =>
Expand Down
4 changes: 2 additions & 2 deletions src/telemetry/TelemetrySenderInRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ export default class TelemetrySenderInRenderer extends TelemetrySender {

getClient = () => this.client ?? this.initClient();

sendUsageData = async (action: string, metadata?: TelemetryMetadata) => {
sendEvent = async (action: string, metadata?: TelemetryMetadata) => {
(await this.getClient()).trackEvent({ name: action }, metadata);
this.logger?.debug(`Sending usage data ${JSON.stringify(action)}`);
this.logger?.debug(`Sending event ${JSON.stringify(action)}`);
};

sendPageView = async (pageName: string) =>
Expand Down
23 changes: 12 additions & 11 deletions src/telemetry/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ const getTelemetrySenderIfEnabled = () => {

const setLogger = (logger: Logger) =>
getTelemetrySenderUnconditionally().setLogger(logger);
const enable = () => getTelemetrySenderUnconditionally().enable();
const isEnabled = () => getTelemetrySenderUnconditionally().isEnabled();
const disable = () => getTelemetrySenderUnconditionally().disable();
const reset = () => getTelemetrySenderUnconditionally().reset();
const setUsersAgreedToTelemetry = (hasAgreed: boolean) =>
getTelemetrySenderUnconditionally().setUsersAgreedToTelemetry(hasAgreed);
const getIsSendingTelemetry = () =>
getTelemetrySenderUnconditionally().getIsSendingTelemetry();
const setUsersWithdrewTelemetryAgreement = () =>
getTelemetrySenderUnconditionally().setUsersWithdrewTelemetryAgreement();
const enableTelemetry = () =>
getTelemetrySenderUnconditionally().allowTelemetryForCurrentApp();

Expand All @@ -59,8 +61,8 @@ const enableTelemetry = () =>
const getFriendlyAppName = () =>
packageJson().name.replace('pc-nrfconnect-', '');

const sendUsageData = (action: string, metadata?: TelemetryMetadata) =>
getTelemetrySenderIfEnabled()?.sendUsageData(
const sendEvent = (action: string, metadata?: TelemetryMetadata) =>
getTelemetrySenderIfEnabled()?.sendEvent(
`${getFriendlyAppName()}: ${action}`,
flatObject(metadata)
);
Expand All @@ -83,12 +85,11 @@ const sendErrorReport = (error: string | Error) =>

export default {
setLogger,
disable,
enable,
isEnabled,
reset,
setUsersAgreedToTelemetry,
setUsersWithdrewTelemetryAgreement,
getIsSendingTelemetry,
sendErrorReport,
sendUsageData,
sendEvent,
sendPageView,
sendMetric,
sendTrace,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/useHotKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const useNewHotKey = (shortcut: Shortcut, deps: DependencyList = []) => {

Mousetrap.bind(shortcut.hotKey, (_e, combo) => {
shortcut.action();
telemetry.sendUsageData('pressed hotkey', { combo });
telemetry.sendEvent('pressed hotkey', { combo });
});

return () => {
Expand Down

0 comments on commit 1d8f67e

Please sign in to comment.