Skip to content

Commit

Permalink
refactor: refactor fathom with custom hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhojang6 committed Oct 25, 2023
1 parent 3ec59c7 commit bbd8ef2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ UNBODY_PROJECT_ID=
SIMPLECAST_ACCESS_TOKEN=
REVALIDATE_WEBHOOK_TOKEN=
DISCORD_LOGS_WEBHOOK_URL=
NEXT_PUBLIC_SITE_URL=
NEXT_PUBLIC_SITE_URL=
FATHOM_SITE_ID=
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ UNBODY_PROJECT_ID=
SIMPLECAST_ACCESS_TOKEN=
REVALIDATE_WEBHOOK_TOKEN=
NEXT_PUBLIC_SITE_URL=https://press.logos.co
FATHOM_SITE_ID=
```

This is a template for `.env.local`, which is included in `.gitignore`.
Expand Down
2 changes: 2 additions & 0 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { GlobalAudioPlayer } from '@/components/GlobalAudioPlayer'
import { ProgressBar } from '@/components/ProgressBar/ProgressBar'
import { uiConfigs } from '@/configs/ui.configs'
import { DefaultLayout } from '@/layouts/DefaultLayout'
import useFathomAnalytics from '@/utils/useFathomAnalytics'
import { css, Global } from '@emotion/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { NextComponentType, NextPageContext } from 'next'
Expand All @@ -27,6 +28,7 @@ const queryClient = new QueryClient()

export default function App({ Component, pageProps }: AppLayoutProps) {
const hydrated = useHydrated()
useFathomAnalytics()

const getLayout =
Component.getLayout ||
Expand Down
4 changes: 3 additions & 1 deletion src/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Head, Html, Main, NextScript } from 'next/document'
import { defaultThemeState } from '../states/themeState'

const fathom = process.env.FATHOM_SITE_ID || ''

export default function Document() {
return (
<Html lang="en" data-theme="light">
Expand All @@ -21,7 +23,7 @@ export default function Document() {
o.async=1; o.src=t; o.id='fathom-script';
m.parentNode.insertBefore(o,m)
})(document, window, '//fathom.status.im/tracker.js', 'fathom');
fathom('set', 'siteId', 'VERNO');
fathom('set', 'siteId', '${fathom}');
fathom('trackPageview');`,
}}
/>
Expand Down
24 changes: 24 additions & 0 deletions src/utils/useFathomAnalytics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Router from 'next/router'
import { useEffect } from 'react'

const useFathomAnalytics = () => {
useEffect(() => {
if (window.fathom) {
window.fathom('trackPageview')
}

const handleRouteChange = () => {
if (window.fathom) {
window.fathom('trackPageview')
}
}

Router.events.on('routeChangeComplete', handleRouteChange)

return () => {
Router.events.off('routeChangeComplete', handleRouteChange)
}
}, [])
}

export default useFathomAnalytics
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
"@/*": ["./src/*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "./types"],
"exclude": ["node_modules"]
}
8 changes: 8 additions & 0 deletions types/fathom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
interface Fathom {
(command: string, ...args: any[]): void
q?: Array<any>
}

interface Window {
fathom?: Fathom
}

0 comments on commit bbd8ef2

Please sign in to comment.