Skip to content

Commit

Permalink
add analytics base
Browse files Browse the repository at this point in the history
  • Loading branch information
Kluskey committed Oct 10, 2024
1 parent 8783825 commit 1f8af5b
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions mirror-2/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ NEXT_PUBLIC_SUPABASE_URL=http://127.0.0.1:54321
NEXT_PUBLIC_SUPABASE_ANON_KEY=SUPABASE_CLIENT_API_KEY
NEXT_PUBLIC_APP_NAME="The Mirror" # only use The Mirror or Reflekt
NEXT_PUBLIC_DISCORD_INVITE_URL=https://themirror.space/discord
NEXT_PUBLIC_GA_KEY_GAME_PUBLIC=
NEXT_PUBLIC_GA_KEY_SECRET= # I asked about this to GA support; why secret public?? Using throwaway keys for now
3 changes: 3 additions & 0 deletions mirror-2/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { Montserrat } from 'next/font/google'
import "./globals.css";
import { Metadata } from "next";
import { appName, appDescription, faviconPath } from "@/lib/theme-service";
import Analytics from "@/utils/analytics/analytics";
export const metadata: Metadata = {
title: appName(),
description: appDescription(),
}

const montserrat = Montserrat({
subsets: ['latin'],
display: 'swap',
Expand All @@ -20,6 +22,7 @@ export default function RootLayout({
<link rel="icon" href={faviconPath()} sizes="any" />
</head>
<body className="h-screen bg-background text-foreground ">
<Analytics />
<ClientLayout>
{children}
</ClientLayout>
Expand Down
2 changes: 2 additions & 0 deletions mirror-2/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"version": "0.0.1",
"private": true,
"scripts": {
"dev": "next dev & supabase start",
Expand Down Expand Up @@ -32,6 +33,7 @@
"autoprefixer": "10.4.17",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.1",
"gameanalytics": "^4.4.7",
"geist": "^1.2.1",
"lucide-react": "^0.446.0",
"next": "latest",
Expand Down
13 changes: 12 additions & 1 deletion mirror-2/state/local.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { RootState } from '@/state/store';
import { setAnalyticsUserId } from '@/utils/analytics/analytics';
import { Database } from '@/utils/database.types';
import type { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';
import { createListenerMiddleware, createSlice } from '@reduxjs/toolkit';

export type ControlBarView = "assets" | "hierarchy" | "scenes" | "code" | "database" | "versions" | "settings";

Expand Down Expand Up @@ -106,6 +107,16 @@ export const {
insertAutomaticallyExpandedSceneIds
} = localSlice.actions;

// Middleware
export const listenerMiddlewareLocal = createListenerMiddleware()
listenerMiddlewareLocal.startListening({
actionCreator: updateLocalUserState,
effect: async (action, listenerApi) => {
setAnalyticsUserId(action.payload.id)
}
})


// Selectors
export const selectUiSoundsCanPlay = (state: RootState) => state.local.uiSoundsCanPlay;
export const selectControlBarCurrentView = (state: RootState) => state.local.controlBarCurrentView;
Expand Down
3 changes: 2 additions & 1 deletion mirror-2/state/store.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client"
import { configureStore } from '@reduxjs/toolkit'
import { setupListeners } from '@reduxjs/toolkit/query/react'
import { localSlice } from '@/state/local'
import { listenerMiddlewareLocal, localSlice } from '@/state/local'
import { spacesApi } from '@/state/spaces'
import { scenesApi } from '@/state/scenes'
import { entitiesApi } from '@/state/entities'
Expand All @@ -27,6 +27,7 @@ export const store = configureStore({
.concat(scenesApi.middleware)
.concat(entitiesApi.middleware)
.concat(componentsApi.middleware)
.concat(listenerMiddlewareLocal.middleware)
})

// optional, but required for refetchOnFocus/refetchOnReconnect behaviors
Expand Down
21 changes: 21 additions & 0 deletions mirror-2/utils/analytics/analytics.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"use client"
import { useEffect } from 'react';
import packageJson from '../../package.json';
import { GameAnalytics } from 'gameanalytics'

export function setAnalyticsUserId(userId: string) {
GameAnalytics("configureUserId", userId);
}

const Analytics = function () {
useEffect(() => {
if (typeof window !== 'undefined') {
GameAnalytics("configureBuild", `web ${packageJson.version}`);
GameAnalytics("initialize", process.env.NEXT_PUBLIC_GA_KEY_GAME_PUBLIC, process.env.NEXT_PUBLIC_GA_KEY_SECRET);
}
}, [])

return <></>
}

export default Analytics
5 changes: 5 additions & 0 deletions mirror-2/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1368,6 +1368,11 @@ function-bind@^1.1.2:
resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.2.tgz#2c02d864d97f3ea6c8830c464cbd11ab6eab7a1c"
integrity sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==

gameanalytics@^4.4.7:
version "4.4.7"
resolved "https://registry.yarnpkg.com/gameanalytics/-/gameanalytics-4.4.7.tgz#f619861acb499a61beea4c65bc650db1246ebe5d"
integrity sha512-yXC6+j/OtkjRtrA1J2E3kVUDR8bdUm25QUzee1JPSTMOajbFV5zMg4+3kFJxhTvDVpj6qkO9QvHBrQCMNWLPVQ==

geist@^1.2.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/geist/-/geist-1.3.1.tgz#bbd95db23b2a00baf6020e3b1b63a5752f4787d2"
Expand Down

0 comments on commit 1f8af5b

Please sign in to comment.