mirror of
https://github.com/r-freeman/portfolio.git
synced 2024-11-22 12:55:42 +00:00
Added analytics to home page
This commit is contained in:
parent
126b66ac09
commit
abd626a646
@ -9,9 +9,12 @@ type ViewsProps = {
|
|||||||
slug: string
|
slug: string
|
||||||
className?: string
|
className?: string
|
||||||
shouldUpdateViews?: boolean
|
shouldUpdateViews?: boolean
|
||||||
|
shouldRender?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export function Views({as: Component = 'span', slug, className, shouldUpdateViews = false}: ViewsProps) {
|
const isProd = process.env.NODE_ENV === 'production'
|
||||||
|
|
||||||
|
export function Views({as: Component = 'span', slug, className, shouldUpdateViews = true, shouldRender = true}: ViewsProps) {
|
||||||
const {data} = useSWR(`/api/views/${slug}`, fetcher) as { data: { views: number } }
|
const {data} = useSWR(`/api/views/${slug}`, fetcher) as { data: { views: number } }
|
||||||
const {mutate} = useSWRConfig()
|
const {mutate} = useSWRConfig()
|
||||||
|
|
||||||
@ -37,7 +40,7 @@ export function Views({as: Component = 'span', slug, className, shouldUpdateView
|
|||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (shouldUpdateViews) {
|
if (shouldUpdateViews && isProd) {
|
||||||
const registerView = async () => {
|
const registerView = async () => {
|
||||||
await fetcher(`/api/views/${slug}`,
|
await fetcher(`/api/views/${slug}`,
|
||||||
{
|
{
|
||||||
@ -50,6 +53,8 @@ export function Views({as: Component = 'span', slug, className, shouldUpdateView
|
|||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
if (!shouldRender) return null
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Component className={className}>
|
<Component className={className}>
|
||||||
{` · ${data?.views > 0 ? numberFormat(data.views) : '—'} views`}
|
{` · ${data?.views > 0 ? numberFormat(data.views) : '—'} views`}
|
||||||
|
@ -28,7 +28,11 @@ function Article(article: Article) {
|
|||||||
<Card.Eyebrow as="time" dateTime={article.date} decorate={false}>
|
<Card.Eyebrow as="time" dateTime={article.date} decorate={false}>
|
||||||
{formatDate(article.date)}
|
{formatDate(article.date)}
|
||||||
</Card.Eyebrow>
|
</Card.Eyebrow>
|
||||||
<Views slug={article.slug} className="text-sm text-zinc-500 dark:text-zinc-400"/>
|
<Views
|
||||||
|
slug={article.slug}
|
||||||
|
className="text-sm text-zinc-500 dark:text-zinc-400"
|
||||||
|
shouldUpdateViews={false}
|
||||||
|
/>
|
||||||
</p>
|
</p>
|
||||||
</Card>
|
</Card>
|
||||||
</article>
|
</article>
|
||||||
@ -134,6 +138,7 @@ export default function Home({articles}: { articles: Article[] }) {
|
|||||||
<Resume/>
|
<Resume/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<Views slug='home' shouldRender={false}/>
|
||||||
</Container>
|
</Container>
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
@ -19,7 +19,11 @@ function Article({article}: { article: Article }) {
|
|||||||
<Card.Eyebrow as="time" dateTime={article.date} decorate={false}>
|
<Card.Eyebrow as="time" dateTime={article.date} decorate={false}>
|
||||||
{formatDate(article.date)}
|
{formatDate(article.date)}
|
||||||
</Card.Eyebrow>
|
</Card.Eyebrow>
|
||||||
<Views slug={article.slug} className="text-sm text-zinc-500 dark:text-zinc-400"/>
|
<Views
|
||||||
|
slug={article.slug}
|
||||||
|
className="text-sm text-zinc-500 dark:text-zinc-400"
|
||||||
|
shouldUpdateViews={false}
|
||||||
|
/>
|
||||||
</p>
|
</p>
|
||||||
</Card>
|
</Card>
|
||||||
</article>
|
</article>
|
||||||
|
Loading…
Reference in New Issue
Block a user