@@ -14,6 +14,10 @@ import InstallOverview from "@skedwards88/shared-components/src/components/Insta
1414import PWAInstall from "@skedwards88/shared-components/src/components/PWAInstall" ;
1515import { timerInit } from "../logic/timerInit" ;
1616import { timerReducer } from "../logic/timerReducer" ;
17+ import { getUserId } from "@skedwards88/shared-components/src/logic/getUserId" ;
18+ import { v4 as uuidv4 } from "uuid" ;
19+ import { sendAnalyticsCF } from "@skedwards88/shared-components/src/logic/sendAnalyticsCF" ;
20+ import { isRunningStandalone } from "@skedwards88/shared-components/src/logic/isRunningStandalone" ;
1721
1822function parseURLQuery ( ) {
1923 // Get the seed and game settings from the query params
@@ -126,6 +130,52 @@ export default function App() {
126130 document . removeEventListener ( "visibilitychange" , handleVisibilityChange ) ;
127131 } ) ;
128132
133+ // ******
134+ // Start analytics setup
135+ // ******
136+
137+ // Store userID so I don't have to read local storage every time
138+ const userId = getUserId ( "gribbles_uid" ) ;
139+
140+ // Store sessionID as a ref so I have the same session ID until app refresh
141+ const sessionIdRef = React . useRef ( uuidv4 ( ) ) ;
142+ const sessionId = sessionIdRef . current ;
143+
144+ // Send analytics on load
145+ React . useEffect ( ( ) => {
146+ sendAnalyticsCF ( {
147+ userId,
148+ sessionId,
149+ analyticsToLog : [
150+ {
151+ eventName : "app_load" ,
152+ // os, browser, and isMobile are parsed on the server from the user agent headers
153+ screenWidth : window . screen . width ,
154+ screenHeight : window . screen . height ,
155+ isStandalone : isRunningStandalone ( ) ,
156+ devicePixelRatio : window . devicePixelRatio ,
157+ } ,
158+ ] ,
159+ } ) ;
160+ // Just run once on app load
161+ // eslint-disable-next-line react-hooks/exhaustive-deps
162+ } , [ ] ) ;
163+
164+ // Send analytics following reducer updates, if needed
165+ React . useEffect ( ( ) => {
166+ const analyticsToLog = gameState . analyticsToLog ;
167+
168+ if ( ! analyticsToLog || ! analyticsToLog . length ) {
169+ return ;
170+ }
171+
172+ sendAnalyticsCF ( { userId, sessionId, analyticsToLog} ) ;
173+ } , [ gameState ?. analyticsToLog , sessionId , userId ] ) ;
174+
175+ // ******
176+ // End analytics setup
177+ // ******
178+
129179 switch ( display ) {
130180 case "settings" :
131181 return (
0 commit comments