Skip to content

Commit

Permalink
fix: swetrix in client-only
Browse files Browse the repository at this point in the history
  • Loading branch information
ndom91 committed Jul 11, 2024
1 parent bf56993 commit bea2ae3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
30 changes: 30 additions & 0 deletions src/components/analytics.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use client'

import * as Swetrix from 'swetrix'
import { Suspense } from 'react'
import { usePathname, useSearchParams } from 'next/navigation'

function SwetrixComponent() {
const pathname = usePathname()
const searchParams = useSearchParams()

if (window.location.host === 'briefkastenhq.com') {
Swetrix.init(process.env.NEXT_PUBLIC_SWETRIX_PROJECT, {
apiURL: process.env.NEXT_PUBLIC_SWETRIX_API_HOST,
})
let url = pathname
if (searchParams.toString() !== '') {
url += `?${searchParams.toString()}`
}
Swetrix.trackPageview(url)
}
return <div></div>
}

export default function Analytics() {
return (
<Suspense>
<SwetrixComponent />
</Suspense>
)
}
21 changes: 2 additions & 19 deletions src/components/meta.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,7 @@
import Head from 'next/head'
import { usePathname, useSearchParams } from 'next/navigation'
import * as Swetrix from 'swetrix'
import Analytics from "./analytics"

const Meta = () => {
const pathname = usePathname()
const searchParams = useSearchParams()

if (window.location.host === 'briefkastenhq.com') {
Swetrix.init(process.env.NEXT_PUBLIC_SWETRIX_PROJECT, {
apiURL: process.env.NEXT_PUBLIC_SWETRIX_API_HOST,
})
let url = pathname
if (searchParams.toString() !== '') {
url += `?${searchParams.toString()}`
}

if (typeof document !== 'undefined') {
Swetrix.trackPageview(url)
}
}

return (
<>
<Head>
Expand Down Expand Up @@ -64,6 +46,7 @@ const Meta = () => {
<meta name="twitter:image" content={''} />
<meta name="darkreader-lock" />
<title>Briefkasten</title>
<Analytics />
</Head>
</>
)
Expand Down

0 comments on commit bea2ae3

Please sign in to comment.