✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Incrementing values in Sanity for analytics purposes

15 replies
Last updated: May 30, 2022
Hello, I’m currently stuck at one point and I’m wondering if there is a way for autoincrement a value when a document gets fetched? For example to get the most read articles of a blog? Without getting into race condition? Thanks for your help
May 30, 2022, 2:00 PM
I would personally recommend not using Sanity as an analytics platform. You might want to use a different platform more suited to collect real-time data without putting a toll on your Sanity usage.
May 30, 2022, 2:01 PM
That’s sound advice from
user F

Buuuut, if you want to do it anyways, because there is not
that much traffic, and it’s a fun way to learn the platform. Let’s dive into it:
Because of the way we tackle drafts, I usually do things like this by making a separate document that refers to the blog post. You can use the
method in the JS client to increment a value. and you should be able to do so in just a few lines of code in a serverless function.
May 30, 2022, 2:11 PM
Provided you go ahead with it, I would also agree to decouple the analytics data from the document itself by storing it in a different document type that references your main document, as Knut explained. 🙂
May 30, 2022, 2:13 PM
Thanks for your reply, I’m not planning to use Sanity as Analytics tool, but in some cases it would be very handy. For example if you want to recommend the moste read articles at the end of the current article. if that makes sense. It would be easier to have this data already in sanity, instead of connecting to a other endpoint for this information. Thanks Knut, i will look into it
May 30, 2022, 2:17 PM
Right, that makes sense. I think the way I would approach that would be by backporting some normalized data from my analytics tool (GA, Plausible…) via a CRON job. Like every week, I would increment the relevant field for each article as Knut suggested, based on data extracted from the analytics tool.
May 30, 2022, 2:21 PM
If you truly want the most read articles, then you should only trigger it when they reach the bottom. Then I wouldn’t be too worried about API usage 😛 (unless you have a fairly successful blog!)
May 30, 2022, 2:26 PM
True, great point. 🙂
May 30, 2022, 2:27 PM
Ok thanks, thats are great tips! I like the idea of incrementing only at the end of the article. I don't know how much is much for sanity 😅 its the first website where i use sanity. I‘m migrating a drupal blog to nextJs and Sanity, the blog has about 14k daily active readers
May 30, 2022, 2:35 PM
It’s not nothing.
May 30, 2022, 2:36 PM
A active visitor reads between 3-5 articles. So it would increment between 40-70k a day
May 30, 2022, 2:37 PM
Right. Then I would definitely do it via backporting analytics data with a periodic background job.
May 30, 2022, 2:38 PM
ok thanks for your help
May 30, 2022, 2:41 PM
Haha. Forgive me for underestimating!
But this is exciting nonetheless. Would love to learn more about how you find that migration and learnings/insights you might have
May 30, 2022, 4:33 PM
So far it went pretty good. Only sometimes i had a hard time finding some tutorials about customizing sanity studio or writing custom plugins. i found some usefull videos from kapehe but not too much about customizing other things than fields for documents or so. but i’m still in middle of the process 😅 hopefully everything will fall into place
May 30, 2022, 4:43 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?