@@ -2,6 +2,11 @@ import {
22 getInternalOrderCode ,
33 type RampsOrder ,
44} from '@metamask/ramps-controller' ;
5+ import ReduxService from '../../../redux' ;
6+ import {
7+ clearTerminalOrderAnalyticsEntries ,
8+ markTerminalOrderAnalyticsEmittedEntry ,
9+ } from '../../../redux/slices/terminalOrderAnalytics' ;
510
611/**
712 * Terminal-order analytics dedup registry (TRAM-3691).
@@ -29,9 +34,10 @@ import {
2934 * so `getInternalOrderCode` yields an identical key for the same order. Keying
3035 * off the raw `providerOrderId` would NOT be stable across those two paths.
3136 *
32- * In-memory, same-session only (mirrors `headlessOrderContextRegistry`). A
33- * process relaunch clears it; that is acceptable here because a terminal order
34- * added after relaunch is a first observation in that session and SHOULD emit.
37+ * The key is persisted in the `terminalOrderAnalytics` redux slice so app
38+ * relaunches and repeated callback returns do not turn the same order settlement
39+ * into multiple Mixpanel events. The local Set is retained as an immediate
40+ * same-tick guard in case Redux persistence is temporarily unavailable.
3541 */
3642const emittedTerminalOrders = new Set < string > ( ) ;
3743
@@ -41,7 +47,9 @@ const emittedTerminalOrders = new Set<string>();
4147 * @param order - The order whose terminal event was just emitted.
4248 */
4349export function markTerminalOrderAnalyticsEmitted ( order : RampsOrder ) : void {
44- emittedTerminalOrders . add ( getInternalOrderCode ( order ) ) ;
50+ const orderCode = getInternalOrderCode ( order ) ;
51+ emittedTerminalOrders . add ( orderCode ) ;
52+ ReduxService . store . dispatch ( markTerminalOrderAnalyticsEmittedEntry ( orderCode ) ) ;
4553}
4654
4755/**
@@ -52,7 +60,11 @@ export function markTerminalOrderAnalyticsEmitted(order: RampsOrder): void {
5260 * @returns True when a terminal event was already emitted for the order.
5361 */
5462export function hasEmittedTerminalOrderAnalytics ( order : RampsOrder ) : boolean {
55- return emittedTerminalOrders . has ( getInternalOrderCode ( order ) ) ;
63+ const orderCode = getInternalOrderCode ( order ) ;
64+ return (
65+ emittedTerminalOrders . has ( orderCode ) ||
66+ Boolean ( ReduxService . store . getState ( ) . terminalOrderAnalytics [ orderCode ] )
67+ ) ;
5668}
5769
5870/**
@@ -61,4 +73,5 @@ export function hasEmittedTerminalOrderAnalytics(order: RampsOrder): boolean {
6173 */
6274export function __resetTerminalOrderAnalyticsRegistryForTests ( ) : void {
6375 emittedTerminalOrders . clear ( ) ;
76+ ReduxService . store . dispatch ( clearTerminalOrderAnalyticsEntries ( ) ) ;
6477}
0 commit comments