# Finding out how many users are watching or tracking?

**URL:** https://meta.discourse.org/t/finding-out-how-many-users-are-watching-or-tracking/56900
**Category:** Development
**Created:** [February 4, 2017, 10:21am UTC](https://meta.discourse.org/t/finding-out-how-many-users-are-watching-or-tracking/56900 "2017-02-04T10:21:52Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)
#### Post date: [February 4, 2017, 10:21am UTC](https://meta.discourse.org/t/finding-out-how-many-users-are-watching-or-tracking/56900/1 "2017-02-04T10:21:52Z")

</div>

Where Can we find how many users are watching or tracking a topic or a category? Does it save any where in discourse?

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [February 4, 2017, 8:55pm UTC](https://meta.discourse.org/t/finding-out-how-many-users-are-watching-or-tracking/56900/2 "2017-02-04T20:55:01Z")

</div>

There are “\_user” tables.  
`category_users`, `topic_users`, `group_users` and `tag_users`  
Each has a `notification_level` field where the integer indicates a members preference - 0 Muted, 1 Normal, 2 Tracked, 3 Watched

So a query something like this should work

```plaintext
SELECT COUNT(id) 
FROM category_users 
WHERE notification_level = 3 
GROUP BY category_id 

```

to give you how many are Watching each category.

---

<div class="post-metadata">

### Author: ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)
#### Post date: [March 31, 2017, 1:33pm UTC](https://meta.discourse.org/t/finding-out-how-many-users-are-watching-or-tracking/56900/3 "2017-03-31T13:33:58Z")

</div>

How can I define a function in plugin.rb for using this code?  
I want to use a function in my JavaScript code to get the tracking but I don’t know how to configure the plugin.rb file to create a function like that?  
Can any body tell me what should I add to plugin.rb to access a function like `trakingNumbers(categoryID)` in my widget’s javascript code?

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [March 31, 2017, 1:44pm UTC](https://meta.discourse.org/t/finding-out-how-many-users-are-watching-or-tracking/56900/4 "2017-03-31T13:44:49Z")

</div>

If you post a link to your GitHub repo others could look and offer suggestions / PRs

---

<div class="post-metadata">

### Author: ![Alavi1412](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/alavi1412/32/67721_2.png) [@Alavi1412](https://meta.discourse.org/u/Alavi1412)
#### Post date: [March 31, 2017, 1:50pm UTC](https://meta.discourse.org/t/finding-out-how-many-users-are-watching-or-tracking/56900/5 "2017-03-31T13:50:07Z")

</div>

[Here](https://github.com/Alavi1412/nova-widget) is my repo on github.  
It is a widget for @angus sidebar widget plugin. but I have stock in finding tracking number. I want to use this number in [This](https://github.com/Alavi1412/nova-widget/blob/master/assets/javascripts/discourse/widgets/profile.js.es6) file.

---

<div class="post-metadata">

### Author: ![Mittineague](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/mittineague/32/114259_2.png) [@Mittineague](https://meta.discourse.org/u/Mittineague)
#### Post date: [March 31, 2017, 2:05pm UTC](https://meta.discourse.org/t/finding-out-how-many-users-are-watching-or-tracking/56900/6 "2017-03-31T14:05:59Z")

</div>

I think others will know better than I as to what the best approach might be. My take is that certain values are already available by making an XHR to get the JSON and when they aren’t a controller can be put together to return JSON eg.

> <https://github.com/Mittineague/discourse-newpage/blob/master/app/controllers/discourse_newpage/newpages_controller.rb#L5-L9>

I have seen plugins that use custom queries involving JOINs etc. but I have not experimented doing similar yet.
