Skip to content

Commit

Permalink
feat(2184): modify dapp visited event in terms of isFirstVisited
Browse files Browse the repository at this point in the history
  • Loading branch information
DDDDDanica committed Mar 24, 2024
1 parent 09a833d commit 9ded093
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,20 @@ 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,
referrer: {
url: origin,
},
properties: {
is_first_visit: true,
is_first_visit: isFirstVisit,
number_of_accounts: Object.keys(metamaskState.accounts).length,
number_of_accounts_connected: numberOfConnectedAccounts,
},
Expand Down
74 changes: 74 additions & 0 deletions test/e2e/tests/metrics/dapp-viewed.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
openDapp,
waitForAccountRendered,
WINDOW_TITLES,
DAPP_URL,
} = require('../../helpers');
const FixtureBuilder = require('../../fixture-builder');
const {
Expand Down Expand Up @@ -271,4 +272,77 @@ 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);

const aaaa = await getEventPayloads(driver, mockedEndpoints);
console.log(aaaa.length, aaaa);

// 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);
},
);
});
});

0 comments on commit 9ded093

Please sign in to comment.