# Tracking topic views history

**URL:** https://meta.discourse.org/t/tracking-topic-views-history/280759
**Category:** Data & reporting
**Created:** [October 2, 2023, 4:01pm UTC](https://meta.discourse.org/t/tracking-topic-views-history/280759 "2023-10-02T16:01:51Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Ayelen\_Rives](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ayelen_rives/32/226978_2.png) [@Ayelen\_Rives](https://meta.discourse.org/u/Ayelen_Rives)
#### Post date: [October 2, 2023, 4:01pm UTC](https://meta.discourse.org/t/tracking-topic-views-history/280759/1 "2023-10-02T16:01:51Z")

</div>

Hi!  
I’ve been searching for a while but couldn’t find anything similar, not even in the [cool Data Explorer queries](https://meta.discourse.org/t/superceded-what-cool-data-explorer-queries-have-you-come-up-with/43516).

I want to see the history of views/visits that a specific topic has received for a period of time.  
Ideally, I’d like to have something like this, but for each individual topic:

 ![image](https://global.discourse-cdn.com/meta/original/4X/7/9/4/794f15f4b4e52eb004dc3776825d6715602eb079.png)

I don’t mind if it’s just a table day by day or if it can include a graph (I can build that outside Discourse). I’m also really interested in having all visits (user & anon).

This would help me track and analyze topics usage within certain periods of time (in where I expect more or less activity).

Do you know if is there some [data explorer](https://meta.discourse.org/t/32566?silent=true) query for this?  
Thank you in advance! 🙌

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [October 2, 2023, 4:10pm UTC](https://meta.discourse.org/t/tracking-topic-views-history/280759/2 "2023-10-02T16:10:18Z")

</div>

Hi @Ayelen_Rives 👋

Maybe try this:

```sql
-- [params]
-- int :topic_id = 
-- date :start_date = 
-- date :end_date = 

SELECT COUNT(*) AS views
FROM topic_views
WHERE topic_id = :topic_id
AND viewed_at BETWEEN :start_date AND :end_date

```

you will need to specify the topic id (from the topic URL) and the date range parameters in YYYY-MM-DD format.

---

<div class="post-metadata">

### Author: ![Ayelen\_Rives](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ayelen_rives/32/226978_2.png) [@Ayelen\_Rives](https://meta.discourse.org/u/Ayelen_Rives)
#### Post date: [October 2, 2023, 4:27pm UTC](https://meta.discourse.org/t/tracking-topic-views-history/280759/3 "2023-10-02T16:27:26Z")

</div>

Hi Lilly!

It’s useful! I’ll make some research in order to separate data day by day, because it sums up all views during period.

Now I know where to begin 🙌 🔎  
Thanks!!

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [October 2, 2023, 4:30pm UTC](https://meta.discourse.org/t/tracking-topic-views-history/280759/4 "2023-10-02T16:30:41Z")

</div>

Glad that helps you. Let us know if you need more specific queries or more help with it 🙂

---

<div class="post-metadata">

### Author: ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)
#### Post date: [October 2, 2023, 4:40pm UTC](https://meta.discourse.org/t/tracking-topic-views-history/280759/5 "2023-10-02T16:40:12Z")

</div>

I’m afraid I have recently found out that the `topic_views` table only records the very first view of a topic by a user and **not** one topic view per user/ip per day.

Unfortunately this makes doing a time-based breakdown on views a little skewed, and can only be done on ‘viewed for the first time’.

---

<div class="post-metadata">

### Author: ![Lilly](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/lilly/32/575047_2.png) [@Lilly](https://meta.discourse.org/u/Lilly)
#### Post date: [October 2, 2023, 4:42pm UTC](https://meta.discourse.org/t/tracking-topic-views-history/280759/6 "2023-10-02T16:42:05Z")

</div>

Well then the table should be called `topic_first_views` 😅 🙃

Sorry @Ayelen_Rives , I tried 🤷🏻‍♀️ 😿

edit: Found a relevant discussion:

> [@Topic\_views vs Topic.views](https://meta.discourse.org/t/topic-views-vs-topic-views/97175):
>
> When helping a client with a [data explorer](https://meta.discourse.org/t/32566?silent=true) query, they noticed a difference between the topic views returned by the query, and the number on the category topic list. Where does the difference between SELECT COUNT(\*) FROM topic\_views WHERE topic\_id = X and Topic.find(X).views come from?

---

<div class="post-metadata">

### Author: ![Ayelen\_Rives](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ayelen_rives/32/226978_2.png) [@Ayelen\_Rives](https://meta.discourse.org/u/Ayelen_Rives)
#### Post date: [October 3, 2023, 12:40pm UTC](https://meta.discourse.org/t/tracking-topic-views-history/280759/7 "2023-10-03T12:40:22Z")

</div>

What a pity! Agree with Lilly that the table should be called topic\_first\_views for disambiguation.

Thanks for the info and thanks Lilly for the try! 🙌  
I’ll keep looking on that discussion.
