Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: app event manager and attribution id parameters #11318

Merged
merged 18 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/core/Analytics/MetaMetrics.events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const ONBOARDING_WIZARD_STEP_DESCRIPTION: { [key: number]: string } = {
* Analytics Tracking Events
*/
enum EVENT_NAME {
// App
APP_OPENED = 'App Opened',
NicolasMassart marked this conversation as resolved.
Show resolved Hide resolved

// Error
ERROR = 'Error occurred',
ERROR_SCREEN_VIEWED = 'Error Screen Viewed',
Expand Down Expand Up @@ -432,6 +435,7 @@ enum ACTIONS {
}

const events = {
APP_OPENED: generateOpt(EVENT_NAME.APP_OPENED),
ERROR: generateOpt(EVENT_NAME.ERROR),
ERROR_SCREEN_VIEWED: generateOpt(EVENT_NAME.ERROR_SCREEN_VIEWED),
APPROVAL_STARTED: generateOpt(EVENT_NAME.APPROVAL_STARTED),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('extractURLParams', () => {
channelId: '123',
comm: 'test',
v: '2',
attributionId: '',
};

mockUrlParser.mockImplementation(
Expand Down Expand Up @@ -81,6 +82,7 @@ describe('extractURLParams', () => {
comm: '',
pubkey: '',
v: '',
attributionId: '',
});
});

Expand Down Expand Up @@ -113,6 +115,7 @@ describe('extractURLParams', () => {
comm: '',
pubkey: '',
v: '',
attributionId: '',
});

expect(alertSpy).toHaveBeenCalledWith(
Expand All @@ -133,6 +136,7 @@ describe('extractURLParams', () => {
rpc: '',
sdkVersion: '',
pubkey: 'xyz',
attributionId: '',
};

mockUrlParser.mockImplementation(
Expand Down
2 changes: 2 additions & 0 deletions app/core/DeeplinkManager/ParseManager/extractURLParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface DeeplinkUrlParams {
message?: string;
originatorInfo?: string;
request?: string;
attributionId?: string;
account?: string; // This is the format => "address@chainId"
}

Expand All @@ -39,6 +40,7 @@ function extractURLParams(url: string) {
originatorInfo: '',
channelId: '',
comm: '',
attributionId: '',
};

DevLogger.log(`extractParams:: urlObj`, urlObj);
Expand Down
165 changes: 152 additions & 13 deletions app/core/DeeplinkManager/ParseManager/parseDeeplink.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,43 @@ import handleMetaMaskDeeplink from './handleMetaMaskDeeplink';
import handleUniversalLink from './handleUniversalLink';
import connectWithWC from './connectWithWC';
import parseDeeplink from './parseDeeplink';
import { MetaMetrics, MetaMetricsEvents } from '../../Analytics';
import { store } from '../../../store';

jest.mock('../../../constants/deeplinks', () => ({
PROTOCOLS: {
HTTP: 'http',
HTTPS: 'https',
WC: 'wc',
ETHEREUM: 'ethereum',
DAPP: 'dapp',
METAMASK: 'metamask',
},
}));
jest.mock('../../../core/SDKConnect/SDKConnect', () => ({
getInstance: jest.fn(() => ({
hasInitialized: jest.fn(() => true),
})),
}));
jest.mock('../../../store', () => ({
store: {
getState: jest.fn(() => ({
security: {
dataCollectionForMarketing: true,
},
})),
},
}));

jest.mock('../../../store', () => ({
store: {
getState: jest.fn(() => ({
security: {
dataCollectionForMarketing: true,
},
})),
},
}));

jest.mock('../../../constants/deeplinks');
jest.mock('../../../util/Logger');
Expand All @@ -17,6 +54,14 @@ jest.mock('./connectWithWC');
jest.mock('../../../../locales/i18n', () => ({
strings: jest.fn((key) => key),
}));
jest.mock('../../Analytics', () => ({
MetaMetrics: {
getInstance: jest.fn(),
},
MetaMetricsEvents: {
APP_OPENED: 'APP_OPENED',
},
}));

const invalidUrls = [
'htp://incorrect-format-url',
Expand Down Expand Up @@ -55,29 +100,118 @@ describe('parseDeeplink', () => {
} as unknown as DeeplinkManager;
});

it('should call handleUniversalLinks for HTTP protocol', () => {
const url = 'http://example.com/';
const browserCallBackMock = jest.fn();
const onHandledMock = jest.fn();
describe('Attribution ID handling', () => {
let mockTrackEvent: jest.Mock;

const { urlObj, params } = extractURLParams(url);
beforeEach(() => {
mockTrackEvent = jest.fn();
(MetaMetrics.getInstance as jest.Mock).mockReturnValue({
trackEvent: mockTrackEvent,
});
});

it('should track event with attributionId when data collection is enabled', () => {
const url = 'https://example.com/?attributionId=test123';
(store.getState as jest.Mock).mockReturnValue({
security: { dataCollectionForMarketing: true },
});

parseDeeplink({
deeplinkManager: instance,
url,
origin: 'testOrigin',
browserCallBack: mockBrowserCallBack,
onHandled: mockOnHandled,
});

expect(mockTrackEvent).toHaveBeenCalledWith(
MetaMetricsEvents.APP_OPENED,
{ attributionId: 'test123' },
true
);
});

it('should not track event when attributionId is present but data collection is disabled', () => {
const url = 'https://example.com/?attributionId=test123';
(store.getState as jest.Mock).mockReturnValue({
security: { dataCollectionForMarketing: false },
});

parseDeeplink({
deeplinkManager: instance,
url,
origin: 'testOrigin',
browserCallBack: mockBrowserCallBack,
onHandled: mockOnHandled,
});

expect(mockTrackEvent).not.toHaveBeenCalled();
});

it('should not track event when attributionId is not present', () => {
const url = 'https://example.com/';
(store.getState as jest.Mock).mockReturnValue({
security: { dataCollectionForMarketing: true },
});

parseDeeplink({
deeplinkManager: instance,
url,
origin: 'testOrigin',
browserCallBack: mockBrowserCallBack,
onHandled: mockOnHandled,
});

expect(mockTrackEvent).not.toHaveBeenCalled();
});

it('should handle attributionId with different protocols', () => {
const protocols = ['http', 'https', 'wc', 'ethereum', 'dapp', 'metamask'];

protocols.forEach(protocol => {
const url = `${protocol}://example.com/?attributionId=test_${protocol}`;
(store.getState as jest.Mock).mockReturnValue({
security: { dataCollectionForMarketing: true },
});

parseDeeplink({
deeplinkManager: instance,
url,
origin: 'testOrigin',
browserCallBack: mockBrowserCallBack,
onHandled: mockOnHandled,
});

expect(mockTrackEvent).toHaveBeenCalledWith(
MetaMetricsEvents.APP_OPENED,
{ attributionId: `test_${protocol}` },
true
);

mockTrackEvent.mockClear();
});
});
});

it('should call handleUniversalLinks for HTTP protocol', () => {
const url = 'http://example.com/';
parseDeeplink({
deeplinkManager: instance,
url,
origin: 'testOrigin',
browserCallBack: browserCallBackMock,
onHandled: onHandledMock,
browserCallBack: mockBrowserCallBack,
onHandled: mockOnHandled,
});

expect(mockHandleUniversalLinks).toHaveBeenCalledWith(
expect.objectContaining({
instance,
urlObj,
params,
browserCallBack: browserCallBackMock,
urlObj: expect.any(Object),
params: expect.any(Object),
browserCallBack: mockBrowserCallBack,
origin: 'testOrigin',
wcURL: url,
url,
}),
);
});
Expand Down Expand Up @@ -111,7 +245,6 @@ describe('parseDeeplink', () => {

it('should call handleWCProtocol for WC protocol', () => {
const url = 'wc://example.com';

parseDeeplink({
deeplinkManager: instance,
url,
Expand All @@ -120,12 +253,18 @@ describe('parseDeeplink', () => {
onHandled: mockOnHandled,
});

expect(mockHandleWCProtocol).toHaveBeenCalled();
expect(mockHandleWCProtocol).toHaveBeenCalledWith(
expect.objectContaining({
handled: expect.any(Function),
wcURL: url,
origin: 'testOrigin',
params: expect.any(Object),
}),
);
});

it('should handle Ethereum URL', () => {
const url = 'ethereum://example.com';

parseDeeplink({
deeplinkManager: instance,
url,
Expand Down
22 changes: 19 additions & 3 deletions app/core/DeeplinkManager/ParseManager/parseDeeplink.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Alert } from 'react-native';
import { strings } from '../../../../locales/i18n';
import { PROTOCOLS } from '../../../constants/deeplinks';
import SDKConnect from '../../../core/SDKConnect/SDKConnect';
import { RootState } from '../../../reducers';
import { store } from '../../../store';
import Logger from '../../../util/Logger';
import { MetaMetrics, MetaMetricsEvents } from '../../Analytics';
import DevLogger from '../../SDKConnect/utils/DevLogger';
import DeeplinkManager from '../DeeplinkManager';
import connectWithWC from './connectWithWC';
import extractURLParams from './extractURLParams';
import handleDappUrl from './handleDappUrl';
import handleMetaMaskDeeplink from './handleMetaMaskDeeplink';
import handleUniversalLink from './handleUniversalLink';
import connectWithWC from './connectWithWC';
import { Alert } from 'react-native';
import { strings } from '../../../../locales/i18n';

function parseDeeplink({
deeplinkManager: instance,
Expand Down Expand Up @@ -41,6 +44,19 @@ function parseDeeplink({
const handled = () => (onHandled ? onHandled() : false);

const wcURL = params?.uri || urlObj.href;
const attributionId = params?.attributionId;

const { security } = store.getState() as RootState;
const isDataCollectionForMarketingEnabled = security.dataCollectionForMarketing;
DevLogger.log(`DeepLinkManager:parse isDataCollectionForMarketingEnabled=${isDataCollectionForMarketingEnabled} attributionId=${attributionId}`);

if(attributionId && isDataCollectionForMarketingEnabled) {
MetaMetrics.getInstance().trackEvent(
MetaMetricsEvents.APP_OPENED,
{ attributionId },
true,
);
}
abretonc7s marked this conversation as resolved.
Show resolved Hide resolved

switch (urlObj.protocol.replace(':', '')) {
case PROTOCOLS.HTTP:
Expand Down
Loading