Retrieving DEV article analytics via the API
Tags: programming, analytics, api, meta
Published: 2022-06-29 08:44:27


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/la2pz053w3e9xlgjkxrb.png)

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](https://developers.forem.com/api), 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s8gxnmci81pasbm1gbk0.png)

Based on this HTTP request, I went to the [Forem GitHub repo](https://github.com/forem/forem) and searched for anything related to **analytics** and found [these route definitions](![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5y6zv0lcdpugl7158v2.png)
https://github.com/forem/forem/blob/1eafd7388e970cdd1de4f439cf8d3319f7948def/config/routes/api.rb):

```rb
...
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"
...
```

## 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:

```http
@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}}
```

The endpoints are also available in [this gist](https://gist.github.com/marcduiker/0fd7e686d4e519ce1deb530dd26cd1e0). 

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 :heart: to learn how others are using DEV analytics.