Skip to content

Commit 8a96efa

Browse files
committed
analytics
1 parent 372680c commit 8a96efa

File tree

4 files changed

+71
-24
lines changed

4 files changed

+71
-24
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/App.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ import InstallOverview from "@skedwards88/shared-components/src/components/Insta
1414
import PWAInstall from "@skedwards88/shared-components/src/components/PWAInstall";
1515
import {timerInit} from "../logic/timerInit";
1616
import {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

1822
function 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 (

src/index.html

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
<!doctype html>
22
<html>
33
<head lang="en">
4-
<!-- Google tag (gtag.js) -->
5-
<script
6-
async
7-
src="https://www.googletagmanager.com/gtag/js?id=G-YYJ7HQ3XLG"
8-
></script>
9-
<script>
10-
const host = window.location.hostname;
11-
if (host != "localhost") {
12-
window.dataLayer = window.dataLayer || [];
13-
function gtag() {
14-
dataLayer.push(arguments);
15-
}
16-
gtag("js", new Date());
17-
18-
gtag("config", "G-YYJ7HQ3XLG");
19-
}
20-
</script>
214
<meta charset="UTF-8" />
225
<meta name="viewport" content="width=device-width,initial-scale=1" />
236
<title>Gribbles</title>

src/logic/gameInit.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ export function gameInit({
7373
savedGameState.easyMode == easyMode &&
7474
savedGameState.seed === seed
7575
) {
76-
return {...savedGameState, playedIndexes: [], result: ""};
76+
return {
77+
...savedGameState,
78+
playedIndexes: [],
79+
result: "",
80+
analyticsToLog: [],
81+
};
7782
}
7883

7984
// use the specified settings, otherwise check local storage, otherwise use default
@@ -98,5 +103,14 @@ export function gameInit({
98103
allWords: allWords,
99104
easyMode: easyMode,
100105
seed: seed,
106+
analyticsToLog: [
107+
{
108+
eventName: "new_game",
109+
eventInfo: {
110+
minWordLength,
111+
easyMode,
112+
},
113+
},
114+
],
101115
};
102116
}

0 commit comments

Comments
 (0)