diff --git a/ui/analytics.js b/ui/analytics.js index 2fb6575c6..4f2e0b0a0 100644 --- a/ui/analytics.js +++ b/ui/analytics.js @@ -28,7 +28,9 @@ export const GA_DIMENSIONS = { TYPE: 'type', ACTION: 'action', VALUE: 'value', + START_TIME_MS: 'start_time_ms', DURATION_MS: 'duration_ms', + END_TIME_MS: 'end_time_ms', }; // import getConnectionSpeed from 'util/detect-user-bandwidth'; @@ -50,6 +52,8 @@ const WATCHMAN_BACKEND_ENDPOINT = 'https://watchman.na-backend.odysee.com/report const SEND_DATA_TO_WATCHMAN_INTERVAL = 10; // in seconds type Analytics = { + appStartTime: number, + eventStartTime: any, error: (string) => Promise, sentryError: ({} | string, {}) => Promise, setUser: (Object) => void, @@ -78,9 +82,11 @@ type Analytics = { emailProvidedEvent: () => void, emailVerifiedEvent: () => void, rewardEligibleEvent: () => void, - startupEvent: () => void, + initAppStartTime: (startTime: number) => void, + startupEvent: (time: number) => void, + eventStarted: (name: string, time: number, id?: string) => void, + eventCompleted: (name: string, time: number, id?: string) => void, purchaseEvent: (number) => void, - readyEvent: (number) => void, openUrlEvent: (string) => void, reportEvent: (string, any) => void, }; @@ -221,6 +227,9 @@ async function sendWatchmanData(body) { } const analytics: Analytics = { + appStartTime: 0, + eventStartTime: {}, + // receive buffer events from tracking plugin and save buffer amounts and times for backend call videoBufferEvent: async (claim, data) => { amountOfBufferEvents = amountOfBufferEvents + 1; @@ -424,14 +433,31 @@ const analytics: Analytics = { trending_algorithm: trendingAlgorithm, }); }, - startupEvent: () => { - // TODO: This can be removed (use the automated 'session_start' instead). - // sendGaEvent('app_diagnostics', 'startup'); + initAppStartTime: (startTime: number) => { + analytics.appStartTime = startTime; }, - readyEvent: (timeToReadyMs: number) => { - sendGaEvent('diag_app_ready', { - [GA_DIMENSIONS.DURATION_MS]: timeToReadyMs, - }); + startupEvent: (time: number) => { + if (analytics.appStartTime !== 0) { + sendGaEvent('diag_app_ready', { + [GA_DIMENSIONS.DURATION_MS]: time - analytics.appStartTime, + }); + } + }, + eventStarted: (name: string, time: number, id?: string) => { + const key = id || name; + analytics.eventStartTime[key] = time; + }, + eventCompleted: (name: string, time: number, id?: string) => { + const key = id || name; + if (analytics.eventStartTime[key]) { + sendGaEvent(name, { + [GA_DIMENSIONS.START_TIME_MS]: analytics.eventStartTime[key] - analytics.appStartTime, + [GA_DIMENSIONS.DURATION_MS]: time - analytics.eventStartTime[key], + [GA_DIMENSIONS.END_TIME_MS]: time - analytics.appStartTime, + }); + + delete analytics.eventStartTime[key]; + } }, purchaseEvent: (purchaseInt: number) => { sendGaEvent('purchase', { diff --git a/ui/index.jsx b/ui/index.jsx index 4fe93fdcd..b0bd15eca 100644 --- a/ui/index.jsx +++ b/ui/index.jsx @@ -97,8 +97,7 @@ Lbry.setOverride( ); // @endif -const startTime = Date.now(); -analytics.startupEvent(); +analytics.initAppStartTime(Date.now()); // @if TARGET='app' const { autoUpdater } = remote.require('electron-updater'); @@ -266,14 +265,13 @@ function AppWrapper() { if (DEFAULT_LANGUAGE) { app.store.dispatch(doFetchLanguage(DEFAULT_LANGUAGE)); } + app.store.dispatch(doUpdateIsNightAsync()); app.store.dispatch(doDaemonReady()); app.store.dispatch(doBlackListedOutpointsSubscribe()); app.store.dispatch(doFilteredOutpointsSubscribe()); - const appReadyTime = Date.now(); - const timeToStart = appReadyTime - startTime; - analytics.readyEvent(timeToStart); + analytics.startupEvent(Date.now()); } }, [readyToLaunch, persistDone]); diff --git a/ui/redux/middleware/analytics.js b/ui/redux/middleware/analytics.js index 99d4243aa..70a0a938c 100644 --- a/ui/redux/middleware/analytics.js +++ b/ui/redux/middleware/analytics.js @@ -60,6 +60,14 @@ function handleAnalyticsForAction(action: { type: string, data: any }) { }); break; + case ACTIONS.AUTHENTICATION_STARTED: + analytics.eventStarted('diag_authentication', Date.now()); + break; + + case ACTIONS.AUTHENTICATION_SUCCESS: + analytics.eventCompleted('diag_authentication', Date.now()); + break; + default: // Do nothing break;