DEV Community

Marc Duiker
Marc Duiker

Posted on • Edited on

Retrieving DEV article analytics via the API

Article statistics

Did you ever check the statistics of your DEV article via the dashboard 📈? There you'll find the number of readers, reactions and comments, and followers, for the last week, month, or all time.

DEV stats

This can be useful information, especially if you want to measure if your articles are performing well, and want to learn about community engagement. Since I'm working in developer relations now, these things are important, so I'm keeping an eye on them!

But I don't want to go to the DEV stats dashboard for every one of my articles every week to see how my articles are performing! I really dislike these manual tasks and try to automate them as much as possible 🤖.

API to the rescue!

I looked in to the DEV/Forem API docs, but I couldn't find the statistics that the dashboard is showing 😢. But there must be an API since the data is shown in the dashboard. I opened up my browser devtools, opened the network tab, reloaded the page, and voila... there's a call to dev.to/api/analytics/historical 🎉.

Image description

Based on this HTTP request, I went to the Forem GitHub repo and searched for anything related to analytics and found these route definitions:

...
get "/analytics/totals", to: "analytics#totals"
get "/analytics/historical", to: "analytics#historical"
get "/analytics/past_day", to: "analytics#past_day"
get "/analytics/referrers", to: "analytics#referrers"
...
Enter fullscreen mode Exit fullscreen mode

Analytics endpoints

Combining the information from the Forem API docs, browser devtools and the source code, I defined these HTTP requests to retrieve analytics for a DEV article or even an entire DEV organization:

@apiKey=DEVTO_APIKEY
@orgId=DEVTO_ORGANIZATION_ID
@articleId=DEVTO_ARTICLE_ID

### Get historical analytics per day for an article since the given start date.
GET https://dev.to/api/analytics/historical?start=2022-05-01&article_id={{articleId}}
api-key: {{apiKey}}

### Get total (aggregate) analytics for an article.
https://dev.to/api/analytics/totals?article_id={{articleId}}
api-key: {{apiKey}}

### Get total (aggregate) analytics for an organization.
https://dev.to/api/analytics/totals?organization_id={{orgId}}
api-key: {{apiKey}}

### Get analytics for the past day for an article.
GET https://dev.to/api/analytics/past_day?article_id={{articleId}}
api-key: {{apiKey}}

### Get the referrers for an article
GET https://dev.to/api/analytics/referrers?article_id={{articleId}}
api-key: {{apiKey}}
Enter fullscreen mode Exit fullscreen mode

The endpoints are also available in this gist.

The next step is to use this in a script to get all the stats for my articles automatically 🧑‍💻.

Let me know if this is useful to you. I'd ❤️ to learn how others are using DEV analytics.

Top comments (0)