Top traffic sources by topic

Adding a slightly different variation of the Topic External Traffic Sources query above for reference here as well.

This version uses an int_list parameter for the topic_ids, so if you wanted to specify multiple topic_ids to run the query for, you can use this query.

-- [params]
-- int_list :topic_ids = 12345

SELECT 
  ind.name AS domain,                   -- External domain referring traffic
  COUNT(*) AS clicks                    -- Total clicks from this source
FROM incoming_links il
INNER JOIN posts p 
  ON p.deleted_at IS NULL 
  AND p.id = il.post_id
INNER JOIN topics t 
  ON t.deleted_at IS NULL 
  AND t.id = p.topic_id
INNER JOIN incoming_referers ir 
  ON ir.id = il.incoming_referer_id
INNER JOIN incoming_domains ind 
  ON ind.id = ir.incoming_domain_id
WHERE t.id IN (:topic_ids)              -- Filter for the specified list of topics
  AND ind.name != '127.0.0.1'
GROUP BY ind.name
ORDER BY clicks DESC
2 Likes