Skip to content

Commit 1d50543

Browse files
authored
analytics: use FaunaDB to track posts visitors (#6)
1 parent eac79bb commit 1d50543

File tree

14 files changed

+902
-116
lines changed

14 files changed

+902
-116
lines changed

components/blog-post.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import PublishedDate from '@/components/published-date';
88
import ReadMore from '@/components/read-more';
99
import ViewsCounter from '@/components/views-counter';
1010
import fetcher from '@/lib/fetcher';
11-
import { getViewsEndpoint } from '@/lib/goat-counter';
1211
import { getLocalizedPath } from '@/lib/util';
1312

1413
const BlogPost = ({ post }) => {
@@ -20,15 +19,18 @@ const BlogPost = ({ post }) => {
2019
locale,
2120
asPath: blogPostPath,
2221
});
23-
const { data } = useSWR(getViewsEndpoint(localizedPath), fetcher);
24-
const views = data?.count_unique || 0;
22+
const { data } = useSWR(
23+
`/api/visitors?slug=${encodeURIComponent(localizedPath)}&justRead=true`,
24+
fetcher
25+
);
26+
const views = data?.visitors || 0;
2527

2628
return (
2729
<article className='mb-4 border-b last:border-b-0'>
2830
<Heading linkTo={blogPostPath}>{post.frontMatter?.title}</Heading>
2931
<div className='flex justify-between my-2 text-sm text-gray-600'>
3032
<PublishedDate date={post.frontMatter?.date} locale={locale} />
31-
<ViewsCounter views={views} />
33+
<ViewsCounter loading={!data} views={views} />
3234
</div>
3335
{post?.frontMatter?.image ? (
3436
<Link href={blogPostPath}>

components/nav-links.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ const NavLinks = () => {
5454
</ActiveLink>
5555
<ActiveLink activeClassName='border-middle text-middle' href='/uses'>
5656
<a
57-
aria-label={t('uses')}
57+
aria-label={`/${t('uses')}`}
5858
className='px-1 font-semibold tracking-tighter text-current uppercase transition-colors border-l-4 border-transparent lg:border-l-0 lg:border-b-4 lg:pt-1 hover:border-secondary'
5959
onClick={onTap}
60-
title={t('uses')}
60+
title={`/${t('uses')}`}
6161
>
62-
{t('uses')}
62+
{`/${t('uses')}`}
6363
</a>
6464
</ActiveLink>
6565
<a

components/views-counter.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
import clsx from 'clsx';
12
import useTranslation from '@/i18n/useTranslation';
23

3-
const ViewsCounter = ({ views }) => {
4+
const ViewsCounter = ({ loading = false, views }) => {
45
const { t } = useTranslation();
56

67
return (
7-
<span>
8+
<span className={clsx('transition-opacity', loading && 'opacity-0')}>
89
{views} {views !== 1 ? t('views') : t('view')}
910
</span>
1011
);

content/en/uses.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: My Setup
2+
title: /uses
33
description: A list of the software and hardware that I use as an engineer
44
---
55

content/es/uses.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Mi Configuración
2+
title: /uses
33
description: Una lista del software y hardware que utilizo como ingeniero de front-end
44
---
55

i18n/strings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const LangStrings = {
4949
resume: 'Currículo',
5050
slogan: 'Arturo Campos - Blog de Desarrollo Web y Tecnología',
5151
sorry: 'Lo siento',
52-
uses: 'Uso',
52+
uses: 'Uses',
5353
view: 'vista',
5454
views: 'vistas',
5555
},

lib/goat-counter.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,4 @@ const pageView = url => {
88
}
99
};
1010

11-
const getViewsEndpoint = path =>
12-
`https://arturocampos.goatcounter.com/counter/${encodeURIComponent(
13-
path
14-
)}.json`;
15-
16-
export { pageView, getViewsEndpoint };
11+
export { pageView };

0 commit comments

Comments
 (0)