Skip to content

Commit

Permalink
Adds feature to disable event and/or metric tracking (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
shedonnatnael-cb authored Feb 12, 2024
1 parent 5e69339 commit c861a15
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-nails-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'client-analytics': minor
---

New feature to disable metric/event api calls
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 8 additions & 0 deletions src/storage/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion src/storage/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions src/trackEvent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
4 changes: 4 additions & 0 deletions src/trackEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export const trackEvent = (
): Promise<Event | null> => {
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);
Expand Down
11 changes: 11 additions & 0 deletions src/trackMetric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
5 changes: 5 additions & 0 deletions src/trackMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { getConfig, getMetricScheduler } from './storage/storage';
*/
export const trackMetric = (metric: Metric): Promise<Metric | null> => {
const config = getConfig();

if (config.disableMetricApi) {
return Promise.resolve(null);
}

if (config.disabled) {
return Promise.resolve(null);
}
Expand Down
2 changes: 2 additions & 0 deletions src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>) => void;
isDebug?: boolean;
Expand Down

0 comments on commit c861a15

Please sign in to comment.