From c861a15e70bcc121d26bd847304f135b47cc725c Mon Sep 17 00:00:00 2001 From: shedonnatnael-cb <143427418+shedonnatnael-cb@users.noreply.github.com> Date: Mon, 12 Feb 2024 13:56:27 -0500 Subject: [PATCH] Adds feature to disable event and/or metric tracking (#36) --- .changeset/slimy-nails-study.md | 5 +++++ CHANGELOG.md | 6 ++++++ package.json | 2 +- src/storage/config.test.ts | 8 ++++++++ src/storage/config.ts | 3 ++- src/trackEvent.test.ts | 11 +++++++++++ src/trackEvent.ts | 4 ++++ src/trackMetric.test.ts | 11 +++++++++++ src/trackMetric.ts | 5 +++++ src/types/config.ts | 2 ++ 10 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 .changeset/slimy-nails-study.md diff --git a/.changeset/slimy-nails-study.md b/.changeset/slimy-nails-study.md new file mode 100644 index 0000000..5cbec2c --- /dev/null +++ b/.changeset/slimy-nails-study.md @@ -0,0 +1,5 @@ +--- +'client-analytics': minor +--- + +New feature to disable metric/event api calls diff --git a/CHANGELOG.md b/CHANGELOG.md index 9de2348..b25806f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # client-analytics +## 0.0.9 + +### Patch Changes + +- [Feature] Added new parameters to provide the option of disabling event/metric calls + ## 0.0.8 ### Patch Changes diff --git a/package.json b/package.json index 29259ad..a2ffdc8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "client-analytics", - "version": "0.0.8", + "version": "0.0.9", "type": "module", "main": "./dist/client-analytics.umd.cjs", "module": "./dist/client-analytics.js", diff --git a/src/storage/config.test.ts b/src/storage/config.test.ts index 8e37bff..3b31ce5 100644 --- a/src/storage/config.test.ts +++ b/src/storage/config.test.ts @@ -12,6 +12,8 @@ describe('Config', () => { isDebug: false, eventPath: '/events', metricPath: '/metrics', + disableEventApi: false, + disableMetricApi: false, onError: expect.any(Function), disabled: false, apiEndpoint: 'https://cca-lite.coinbase.com', @@ -25,6 +27,8 @@ describe('Config', () => { isDebug: true, eventPath: '/custom-events', metricPath: '/custom-metrics', + disableEventApi: true, + disableMetricApi: true, onError: expect.any(Function), apiEndpoint: 'https://client.analytics', disabled: false, @@ -36,6 +40,8 @@ describe('Config', () => { isDebug: true, eventPath: '/custom-events', metricPath: '/custom-metrics', + disableEventApi: true, + disableMetricApi: true, onError: expect.any(Function), apiEndpoint: 'https://client.analytics', disabled: false, @@ -76,6 +82,8 @@ describe('Config', () => { isDebug: false, eventPath: '/events', metricPath: '/metrics', + disableEventApi: false, + disableMetricApi: false, onError: expect.any(Function), disabled: false, apiEndpoint: 'https://cca-lite.coinbase.com', diff --git a/src/storage/config.ts b/src/storage/config.ts index 72774f7..b91d663 100644 --- a/src/storage/config.ts +++ b/src/storage/config.ts @@ -10,7 +10,8 @@ export const DEFAULT_CONFIG = { eventPath: '/events', metricPath: '/metrics', apiEndpoint: 'https://cca-lite.coinbase.com', // works for production only - + disableEventApi: false, + disableMetricApi: false, disabled: false, isAlwaysAuthed: false, version: null, diff --git a/src/trackEvent.test.ts b/src/trackEvent.test.ts index 3c7759d..b325f73 100644 --- a/src/trackEvent.test.ts +++ b/src/trackEvent.test.ts @@ -43,6 +43,17 @@ describe('trackEvent', () => { }); }); + test('should return null when disableEventApi is true', async () => { + const config = getConfig(); + Object.assign(config, { disableEventApi: true }); + const event = await trackEvent({ + action: 'test', + component: 'testComponent', + name: 'testName', + }); + expect(event).toBe(null); + }); + test('should return null when identity.isOptOut is true', async () => { const identity = getIdentity(); Object.assign(identity, { isOptOut: true }); diff --git a/src/trackEvent.ts b/src/trackEvent.ts index cb2a3cd..6f5afb1 100644 --- a/src/trackEvent.ts +++ b/src/trackEvent.ts @@ -14,6 +14,10 @@ export const trackEvent = ( ): Promise => { const { config, identity } = getStorage(); + if (config.disableEventApi) { + return Promise.resolve(null); + } + // TODO: combine validation in a set of validators if (identity.isOptOut) { return Promise.resolve(null); diff --git a/src/trackMetric.test.ts b/src/trackMetric.test.ts index d4d8490..b67ce99 100644 --- a/src/trackMetric.test.ts +++ b/src/trackMetric.test.ts @@ -45,6 +45,17 @@ describe('trackMetric', () => { expect(metric).toBe(null); }); + test('should return null when metric api is disabled', async () => { + const config = getConfig(); + Object.assign(config, { disableMetricApi: true }); + const metric = await trackMetric({ + metricName: 'test', + metricType: MetricType.count, + value: 1, + }); + expect(metric).toBe(null); + }); + test('should return null when platform is not web', async () => { const config = setConfig({ platform: 'unknown', diff --git a/src/trackMetric.ts b/src/trackMetric.ts index 3d7e7b2..f184f42 100644 --- a/src/trackMetric.ts +++ b/src/trackMetric.ts @@ -9,6 +9,11 @@ import { getConfig, getMetricScheduler } from './storage/storage'; */ export const trackMetric = (metric: Metric): Promise => { const config = getConfig(); + + if (config.disableMetricApi) { + return Promise.resolve(null); + } + if (config.disabled) { return Promise.resolve(null); } diff --git a/src/types/config.ts b/src/types/config.ts index 5dab71e..ed391b8 100644 --- a/src/types/config.ts +++ b/src/types/config.ts @@ -24,7 +24,9 @@ export type RequiredConfig = { */ export type CustomConfig = { eventPath: string; + disableEventApi?: boolean; metricPath: string; + disableMetricApi?: boolean; disabled?: boolean; onError: (err: Error, metadata?: Record) => void; isDebug?: boolean;