Skip to content

Commit

Permalink
feat(browser): set optOut config in analytics connector (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Wallach committed Aug 22, 2023
1 parent 502a080 commit dd5408d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 26 deletions.
24 changes: 15 additions & 9 deletions packages/analytics-browser/src/browser-client.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { AmplitudeCore, Destination, Identify, returnWrapper, Revenue, UUID } from '@amplitude/analytics-core';
import {
IdentityEventSender,
getAnalyticsConnector,
getAttributionTrackingConfig,
getPageViewTrackingConfig,
IdentityEventSender,
isAttributionTrackingEnabled,
isSessionTrackingEnabled,
isFileDownloadTrackingEnabled,
isFormInteractionTrackingEnabled,
isSessionTrackingEnabled,
setConnectorDeviceId,
setConnectorOptOut,
setConnectorUserId,
} from '@amplitude/analytics-client-common';
import { AmplitudeCore, Destination, Identify, Revenue, UUID, returnWrapper } from '@amplitude/analytics-core';
import {
BrowserClient,
BrowserConfig,
Expand All @@ -21,15 +22,15 @@ import {
Revenue as IRevenue,
TransportType,
} from '@amplitude/analytics-types';
import { convertProxyObjectToRealObject, isInstanceProxy } from './utils/snippet-helper';
import { Context } from './plugins/context';
import { useBrowserConfig, createTransport } from './config';
import { webAttributionPlugin } from '@amplitude/plugin-web-attribution-browser';
import { pageViewTrackingPlugin } from '@amplitude/plugin-page-view-tracking-browser';
import { formInteractionTracking } from './plugins/form-interaction-tracking';
import { fileDownloadTracking } from './plugins/file-download-tracking';
import { webAttributionPlugin } from '@amplitude/plugin-web-attribution-browser';
import { createTransport, useBrowserConfig } from './config';
import { DEFAULT_SESSION_END_EVENT, DEFAULT_SESSION_START_EVENT } from './constants';
import { detNotify } from './det-notification';
import { Context } from './plugins/context';
import { fileDownloadTracking } from './plugins/file-download-tracking';
import { formInteractionTracking } from './plugins/form-interaction-tracking';
import { convertProxyObjectToRealObject, isInstanceProxy } from './utils/snippet-helper';

export class AmplitudeBrowser extends AmplitudeCore implements BrowserClient {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
Expand Down Expand Up @@ -149,6 +150,11 @@ export class AmplitudeBrowser extends AmplitudeCore implements BrowserClient {
setConnectorDeviceId(deviceId, this.config.instanceName);
}

setOptOut(optOut: boolean): void {
setConnectorOptOut(optOut, this.config.instanceName);
super.setOptOut(optOut);
}

reset() {
this.setDeviceId(UUID());
this.setUserId(undefined);
Expand Down
26 changes: 19 additions & 7 deletions packages/analytics-browser/test/browser-client.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { AmplitudeBrowser } from '../src/browser-client';
import * as core from '@amplitude/analytics-core';
import * as Config from '../src/config';
import * as CookieMigration from '../src/cookie-migration';
import { UserSession } from '@amplitude/analytics-types';
import {
CookieStorage,
FetchTransport,
getAnalyticsConnector,
getCookieName,
} from '@amplitude/analytics-client-common';
import * as SnippetHelper from '../src/utils/snippet-helper';
import * as core from '@amplitude/analytics-core';
import { UserSession } from '@amplitude/analytics-types';
import * as webAttributionPlugin from '@amplitude/plugin-web-attribution-browser';
import { AmplitudeBrowser } from '../src/browser-client';
import * as Config from '../src/config';
import * as CookieMigration from '../src/cookie-migration';
import * as fileDownloadTracking from '../src/plugins/file-download-tracking';
import * as formInteractionTracking from '../src/plugins/form-interaction-tracking';
import * as webAttributionPlugin from '@amplitude/plugin-web-attribution-browser';
import * as SnippetHelper from '../src/utils/snippet-helper';

describe('browser-client', () => {
let apiKey = '';
Expand Down Expand Up @@ -404,6 +404,18 @@ describe('browser-client', () => {
});
});

describe('setOptOut', () => {
test('should set opt out config', async () => {
const client = new AmplitudeBrowser();
await client.init(API_KEY, undefined, {
...attributionConfig,
}).promise;
expect(client.config.optOut).toBe(false);
client.setOptOut(true);
expect(client.config.optOut).toBe(true);
});
});

describe('reset', () => {
test('should reset user id and generate new device id config', async () => {
await client.init(apiKey, { defaultTracking }).promise;
Expand Down
2 changes: 1 addition & 1 deletion packages/analytics-client-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"url": "https://github.com/amplitude/Amplitude-TypeScript/issues"
},
"dependencies": {
"@amplitude/analytics-connector": "^1.4.8",
"@amplitude/analytics-connector": "^1.5.0",
"@amplitude/analytics-core": "^2.0.3",
"@amplitude/analytics-types": "^2.1.1",
"tslib": "^2.4.1"
Expand Down
4 changes: 4 additions & 0 deletions packages/analytics-client-common/src/analytics-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ export const setConnectorUserId = (userId: string | undefined, instanceName?: st
export const setConnectorDeviceId = (deviceId: string, instanceName?: string): void => {
getAnalyticsConnector(instanceName).identityStore.editIdentity().setDeviceId(deviceId).commit();
};

export const setConnectorOptOut = (optOut: boolean, instanceName?: string): void => {
getAnalyticsConnector(instanceName).identityStore.editIdentity().setOptOut(optOut).commit();
};
21 changes: 13 additions & 8 deletions packages/analytics-client-common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
export {
getAnalyticsConnector,
setConnectorDeviceId,
setConnectorOptOut,
setConnectorUserId,
} from './analytics-connector';
export { CampaignParser } from './attribution/campaign-parser';
export { CampaignTracker } from './attribution/campaign-tracker';
export { getQueryParams } from './query-params';
export { getCookieName, getOldCookieName } from './cookie-name';
export { CookieStorage } from './storage/cookie';
export { FetchTransport } from './transports/fetch';
export { getAnalyticsConnector, setConnectorDeviceId, setConnectorUserId } from './analytics-connector';
export { IdentityEventSender } from './plugins/identity';
export { getLanguage } from './language';
export { BASE_CAMPAIGN } from './attribution/constants';
export { getGlobalScope } from './global-scope';
export { getCookieName, getOldCookieName } from './cookie-name';
export {
getPageViewTrackingConfig,
getAttributionTrackingConfig,
Expand All @@ -18,3 +17,9 @@ export {
isPageViewTrackingEnabled,
isSessionTrackingEnabled,
} from './default-tracking';
export { getGlobalScope } from './global-scope';
export { getLanguage } from './language';
export { IdentityEventSender } from './plugins/identity';
export { getQueryParams } from './query-params';
export { CookieStorage } from './storage/cookie';
export { FetchTransport } from './transports/fetch';
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { AnalyticsConnector } from '@amplitude/analytics-connector';
import { getAnalyticsConnector, setConnectorDeviceId, setConnectorUserId } from '../src/analytics-connector';
import {
getAnalyticsConnector,
setConnectorDeviceId,
setConnectorOptOut,
setConnectorUserId,
} from '../src/analytics-connector';

describe('analytics-connector', () => {
describe('getAnalyticsConnector', () => {
Expand Down Expand Up @@ -27,6 +32,9 @@ describe('analytics-connector', () => {
updateUserProperties: function () {
return this;
},
setOptOut: function () {
return this;
},
commit,
};
const instance = new AnalyticsConnector();
Expand Down Expand Up @@ -54,6 +62,9 @@ describe('analytics-connector', () => {
updateUserProperties: function () {
return this;
},
setOptOut: function () {
return this;
},
commit,
};
const instance = new AnalyticsConnector();
Expand All @@ -64,4 +75,34 @@ describe('analytics-connector', () => {
expect(commit).toHaveBeenCalledTimes(1);
});
});

describe('setConnectorOptOut', () => {
test('should commit opt out to connector instance', () => {
const commit = jest.fn();
const identityEditor = {
setUserId: function () {
return this;
},
setDeviceId: function () {
return this;
},
setUserProperties: function () {
return this;
},
updateUserProperties: function () {
return this;
},
setOptOut: function () {
return this;
},
commit,
};
const instance = new AnalyticsConnector();
jest.spyOn(instance.identityStore, 'editIdentity').mockReturnValueOnce(identityEditor);
const getInstance = jest.spyOn(AnalyticsConnector, 'getInstance').mockReturnValueOnce(instance);
expect(setConnectorOptOut(true)).toBe(undefined);
expect(getInstance).toHaveBeenCalledTimes(1);
expect(commit).toHaveBeenCalledTimes(1);
});
});
});

0 comments on commit dd5408d

Please sign in to comment.