From 7ef2a7d985567c1aad267b2889cd9601b713fc98 Mon Sep 17 00:00:00 2001 From: Come Grellard Date: Tue, 29 Oct 2024 11:20:36 +0100 Subject: [PATCH] support(llm): flush analytics events when putting the app in background --- .changeset/big-lions-refuse.md | 5 +++++ .../src/analytics/SegmentSetup.tsx | 3 +++ apps/ledger-live-mobile/src/analytics/segment.ts | 5 +++++ .../src/analytics/useFlushInBackground.tsx | 13 +++++++++++++ 4 files changed, 26 insertions(+) create mode 100644 .changeset/big-lions-refuse.md create mode 100644 apps/ledger-live-mobile/src/analytics/useFlushInBackground.tsx diff --git a/.changeset/big-lions-refuse.md b/.changeset/big-lions-refuse.md new file mode 100644 index 000000000000..ba178690e929 --- /dev/null +++ b/.changeset/big-lions-refuse.md @@ -0,0 +1,5 @@ +--- +"live-mobile": patch +--- + +LLM - Flushes all analytics events when the app goes in the background diff --git a/apps/ledger-live-mobile/src/analytics/SegmentSetup.tsx b/apps/ledger-live-mobile/src/analytics/SegmentSetup.tsx index 545af745ea7d..c40e79eb4453 100644 --- a/apps/ledger-live-mobile/src/analytics/SegmentSetup.tsx +++ b/apps/ledger-live-mobile/src/analytics/SegmentSetup.tsx @@ -1,6 +1,7 @@ import { useEffect } from "react"; import { useStore } from "react-redux"; import { start } from "./segment"; +import useFlushInBackground from "./useFlushInBackground"; const SegmentSetup = (): null => { const store = useStore(); @@ -10,6 +11,8 @@ const SegmentSetup = (): null => { // eslint-disable-next-line react-hooks/exhaustive-deps }, []); + useFlushInBackground(); + return null; }; diff --git a/apps/ledger-live-mobile/src/analytics/segment.ts b/apps/ledger-live-mobile/src/analytics/segment.ts index bf70f149af19..26b9b56b7c2a 100644 --- a/apps/ledger-live-mobile/src/analytics/segment.ts +++ b/apps/ledger-live-mobile/src/analytics/segment.ts @@ -371,6 +371,11 @@ export const trackWithRoute = ( }; track(event, newProperties, mandatory); }; + +export const flush = () => { + segmentClient?.flush(); +}; + export const useTrack = () => { const route = useRoute(); const track = useCallback( diff --git a/apps/ledger-live-mobile/src/analytics/useFlushInBackground.tsx b/apps/ledger-live-mobile/src/analytics/useFlushInBackground.tsx new file mode 100644 index 000000000000..34cf07716c7b --- /dev/null +++ b/apps/ledger-live-mobile/src/analytics/useFlushInBackground.tsx @@ -0,0 +1,13 @@ +import { useEffect } from "react"; +import useIsAppInBackground from "../components/useIsAppInBackground"; +import { flush } from "./segment"; + +export default function useFlushInBackground() { + const isAppInBakcground = useIsAppInBackground(); + + useEffect(() => { + if (isAppInBakcground) { + flush(); + } + }, [isAppInBakcground]); +}