From 8bdabf2aff3ebdf7fc75b99a71794001b4c86d2f Mon Sep 17 00:00:00 2001 From: Danica Shen Date: Fri, 22 Mar 2024 20:55:50 +0000 Subject: [PATCH] feat(2184): modify dapp visited event in terms of isFirstVisited --- .../handlers/request-accounts.js | 8 ++- test/e2e/tests/metrics/dapp-viewed.spec.js | 71 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/app/scripts/lib/rpc-method-middleware/handlers/request-accounts.js b/app/scripts/lib/rpc-method-middleware/handlers/request-accounts.js index abd626aea642..0a73b5e0519a 100644 --- a/app/scripts/lib/rpc-method-middleware/handlers/request-accounts.js +++ b/app/scripts/lib/rpc-method-middleware/handlers/request-accounts.js @@ -108,6 +108,12 @@ async function requestEthereumAccountsHandler( res.result = accounts; const numberOfConnectedAccounts = getPermissionsForOrigin(origin).eth_accounts.caveats[0].value.length; + // first time connection to dapp will lead to no log in the permissionHistory + // and if user has connected to dapp before, the dapp origin will be included in the permissionHistory state + // we will leverage that to identify `is_first_visit` for metrics + const isFirstVisit = !Object.keys(metamaskState.permissionHistory).includes( + origin, + ); sendMetrics({ event: MetaMetricsEventName.DappViewed, category: MetaMetricsEventCategory.InpageProvider, @@ -115,7 +121,7 @@ async function requestEthereumAccountsHandler( url: origin, }, properties: { - is_first_visit: true, + is_first_visit: isFirstVisit, number_of_accounts: Object.keys(metamaskState.accounts).length, number_of_accounts_connected: numberOfConnectedAccounts, }, diff --git a/test/e2e/tests/metrics/dapp-viewed.spec.js b/test/e2e/tests/metrics/dapp-viewed.spec.js index e7a93f47e268..a32b8190055a 100644 --- a/test/e2e/tests/metrics/dapp-viewed.spec.js +++ b/test/e2e/tests/metrics/dapp-viewed.spec.js @@ -7,6 +7,7 @@ const { openDapp, waitForAccountRendered, WINDOW_TITLES, + DAPP_URL, } = require('../../helpers'); const FixtureBuilder = require('../../fixture-builder'); const { @@ -271,4 +272,74 @@ describe('Dapp viewed Event @no-mmi', function () { }, ); }); + + it('is sent when reconnect to a dapp that has been connected before', async function () { + async function mockSegment(mockServer) { + return [ + await mockedDappViewedEndpoint(mockServer), + await mockedDappViewedEndpoint(mockServer), + ]; + } + + await withFixtures( + { + dapp: true, + fixtures: new FixtureBuilder() + .withMetaMetricsController({ + metaMetricsId: 'fake-metrics-id', + participateInMetaMetrics: true, + }) + .build(), + title: this.test.fullTitle(), + testSpecificMock: mockSegment, + }, + async ({ driver, mockedEndpoint: mockedEndpoints }) => { + await driver.navigate(); + await unlockWallet(driver); + await waitForAccountRendered(driver); + await connectToDapp(driver); + await waitForDappConnected(driver); + + // close test dapp window to avoid future confusion + const windowHandles = await driver.getAllWindowHandles(); + await driver.closeWindowHandle(windowHandles[1]); + // disconnect dapp in fullscreen view + await driver.switchToWindowWithTitle( + WINDOW_TITLES.ExtensionInFullScreenView, + ); + await driver.clickElement( + '[data-testid ="account-options-menu-button"]', + ); + await driver.clickElement({ text: 'Connected sites', tag: 'div' }); + await driver.findElement({ + text: DAPP_URL, + tag: 'bdi', + }); + await driver.clickElement({ text: 'Disconnect', tag: 'a' }); + await driver.clickElement({ + text: `Disconnect ${DAPP_URL}`, + tag: 'h2', + }); + await driver.clickElement({ text: 'Disconnect', tag: 'button' }); + // validate dapp is not connected + await driver.clickElement( + '[data-testid ="account-options-menu-button"]', + ); + await driver.clickElement({ text: 'Connected sites', tag: 'div' }); + await driver.findElement({ + text: 'Account 1 is not connected to any sites.', + tag: 'p', + }); + // reconnect again + await connectToDapp(driver); + const events = await getEventPayloads(driver, mockedEndpoints); + assert.equal(events.length, 2); + // events are original dapp viewed, new dapp viewed when reconnected + const dappViewedEventProperties = events[1].properties; + assert.equal(dappViewedEventProperties.is_first_visit, false); + assert.equal(dappViewedEventProperties.number_of_accounts, 1); + assert.equal(dappViewedEventProperties.number_of_accounts_connected, 1); + }, + ); + }); });