So, something like this? So, would the IP address be null for crawlers?
I want to include:
- anonymous viewers
- logged in users
I want to exclude:
- crawlers
- staff users
-- [params]
-- string :date = '2020-01-01'
-- boolean :staff = false
WITH t AS (
SELECT date(:date) AS START
),
f AS (
SELECT id as user_id
FROM users u
WHERE u.moderator = 'True' or u.admin = 'True'
)
SELECT
user_id,
-- date_part('year', viewed_at) as year,
-- date_part('week', viewed_at) as week,
COUNT(topic_id) AS topics_viewed
FROM topic_views, t
WHERE viewed_at > t.START
AND ((ip_address is not null AND user_id is NULL) OR
user_id not in (select f.user_id from f))
GROUP BY user_id
ORDER BY topics_viewed DESC
-- GROUP BY year, week
-- ORDER BY year, week