NextJS Analytics with Airtable
Published ....
Last modified ....
Share this post on BlueskySee discussion on Bluesky
I was recently looking for a quick and easy solution to track page hits on my
personal site for another small feature add to the site (blog post on that when
I get around to it!), and figured that I could probably spin up something very
low-fidelity using NextJS's /api routes and
Airtable.
To get started, you'll want to create a new Base on Airtable, and a new table
within that. I opted to use an empty base as the starting point.
Inside the new table created, I'd recommend making the following changes:
- Rename the table, it starts off named as
'Table 1'which isn't all that easy to find, I called minepage-hits - Set the primary column to something unique, I opted to name it
idand set the type toAutonumberso I don't need to worry about conflicts - Set the second column to be the
urlthat the user is visiting - Add a third column to track "hit counts" (each record will only have a value
of 1 here, I use this to automatically rely on Airtable's Sum feature on
columns), I called mine
hits
Once you have your Airtable base and table setup, head over to
api.airtable.com, and select the base that you
just created.
From there, you'll want to change the right-hand-side code preview to JavaScript, and select "Show API key"
For now, grab your api key and now jump to your NextJS project.
Note: This guide assumes you have a NextJS project up and running already, and you at least know of
/apiroutes
Within your NextJS project, if you don't yet have a file at the root of the project then create one, and add a new line that has some unique all-capitilized key name for the Airtable API key: