Skip to content

Commit

Permalink
feat: utilize updateEventFragment before submitted tx metametrics eve…
Browse files Browse the repository at this point in the history
…nt is created
  • Loading branch information
digiwand committed Nov 1, 2024
1 parent 19f43c0 commit b4d0ca7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
64 changes: 60 additions & 4 deletions app/scripts/controllers/metametrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { ENVIRONMENT_TYPE_BACKGROUND } from '../../../shared/constants/app';
import {
METAMETRICS_ANONYMOUS_ID,
METAMETRICS_BACKGROUND_PAGE_OBJECT,
MetaMetricsEventCategory,
MetaMetricsEventName,
MetaMetricsEventFragment,
MetaMetricsUserTrait,
Expand Down Expand Up @@ -312,7 +313,11 @@ export default class MetaMetricsController {
// fragments that are not marked as persistent will be purged and the
// failure event will be emitted.
Object.values(abandonedFragments).forEach((fragment) => {
this.finalizeEventFragment(fragment.id, { abandoned: true });
if (fragment.canThrowAwayIfAbandoned) {
this.deleteEventFragment(fragment.id);
} else {
this.finalizeEventFragment(fragment.id, { abandoned: true });
}
});

// Code below submits any pending segmentApiCalls to Segment if/when the controller is re-instantiated
Expand Down Expand Up @@ -368,7 +373,11 @@ export default class MetaMetricsController {
fragment.lastUpdated &&
Date.now() - fragment.lastUpdated / 1000 > fragment.timeout
) {
this.finalizeEventFragment(fragment.id, { abandoned: true });
if (fragment.canThrowAwayIfAbandoned) {
this.deleteEventFragment(fragment.id);
} else {
this.finalizeEventFragment(fragment.id, { abandoned: true });
}
}
});
}
Expand Down Expand Up @@ -409,11 +418,30 @@ export default class MetaMetricsController {
const { fragments } = this.store.getState();

const id = options.uniqueIdentifier ?? uuidv4();
const fragment = {
let fragment = {
id,
...options,
lastUpdated: Date.now(),
};

/**
* HACK: "transaction-submitted-<id>" fragment hack
* A "transaction-submitted-<id>" fragment may exist following the "Transaction Added"
* event to persist accumulated event fragment props to the "Transaction Submitted" event
* which fires after a user confirms a transaction. Rejecting a confirmation does not fire the
* "Transaction Submitted" event. In this case, these abandoned fragments will be deleted
* instead of finalized with canThrowAwayIfAbandoned set to true.
*/
const hasExistingSubmittedFragment =
options.initialEvent === TransactionMetaMetricsEvent.submitted &&
fragments[id];

if (hasExistingSubmittedFragment) {
fragment = merge(fragments[id], fragment, {
canThrowAwayIfAbandoned: false,
});
}

this.store.updateState({
fragments: {
...fragments,
Expand Down Expand Up @@ -470,7 +498,22 @@ export default class MetaMetricsController {
const fragment = fragments[id];

if (!fragment) {
throw new Error(`Event fragment with id ${id} does not exist.`);
if (!id.includes('transaction-submitted-')) {
throw new Error(`Event fragment with id ${id} does not exist.`);
}

/**
* HACK: "transaction-submitted-<id>" fragment hack
* Creates a "transaction-submitted-<id>" fragment if it does not exist to persist
* accumulated event metrics. In the case it is unused, the abandoned fragment will
* eventually be deleted with canThrowAwayIfAbandoned set to true.
*/
fragments[id] = {
canThrowAwayIfAbandoned: true,
category: MetaMetricsEventCategory.Transactions,
successEvent: TransactionMetaMetricsEvent.finalized,
id,
};
}

this.store.updateState({
Expand All @@ -484,6 +527,19 @@ export default class MetaMetricsController {
});
}

/**
* Deletes an event fragment from state
*
* @param id - The fragment id to delete
*/
deleteEventFragment(id: string): void {
const { fragments } = this.store.getState();

if (!fragments[id]) {
delete fragments[id];
}
}

/**
* Finalizes a fragment, tracking either a success event or failure Event
* and then removes the fragment from state.
Expand Down
7 changes: 6 additions & 1 deletion app/scripts/lib/transaction/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,12 @@ function createTransactionEventFragment({
transactionMetricsRequest.getEventFragmentById,
eventName,
transactionMeta,
)
) &&
/**
* HACK: "transaction-submitted-<id>" fragment hack
* can continue to createEventFragment if "transaction-submitted-<id>" submitted fragment exists
*/
eventName !== TransactionMetaMetricsEvent.submitted
) {
return;
}
Expand Down
7 changes: 7 additions & 0 deletions shared/constants/metametrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,13 @@ export type MetaMetricsEventFragment = {
* The event name.
*/
event?: string;

/**
* HACK: "transaction-submitted-<id>" fragment hack
* If this is true and the fragment is found as an abandoned fragment,
* then delete the fragment instead of finalizing it.
*/
canThrowAwayIfAbandoned?: boolean;
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const useTransactionEventFragment = () => {
await createTransactionEventFragment(transactionId);
}
updateEventFragment(`transaction-added-${transactionId}`, params);
updateEventFragment(`transaction-submitted-${transactionId}`, params);
},
[fragmentExists, gasTransactionId],
);
Expand Down

0 comments on commit b4d0ca7

Please sign in to comment.