The query looks like it should work.
Yes, as it is written it is counting anonymous views. For anonymous topic views, the user_id will be NULL and the ip_address will be NOT NULL. You could exclude anonymous views by adding the following condition to the query:
AND user_id IS NOT NULL
That could be done through a plugin. The Discourse Solved plugin adds a report to the admin dashboard. There could be some code in that plugin that’s useful as an example of how to add a report: discourse-solved/plugin.rb at main · discourse/discourse-solved · GitHub.