fathom/api/referrers.go

32 lines
792 B
Go
Raw Normal View History

package api
import (
2016-12-11 13:50:01 +00:00
"encoding/json"
"github.com/dannyvankooten/ana/count"
"net/http"
)
// URL: /api/referrers
var GetReferrersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2016-12-11 13:50:01 +00:00
before, after := getRequestedPeriods(r)
2016-12-11 13:50:01 +00:00
// get total
total := count.Visitors(before, after)
2016-12-11 13:50:01 +00:00
// get rows
results := count.Custom(`
SELECT
pv.referrer_url,
COUNT(DISTINCT(pv.visitor_id)) AS count
FROM pageviews pv
WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ?
AND pv.referrer_url IS NOT NULL
AND pv.referrer_url != ""
GROUP BY pv.referrer_url
ORDER BY count DESC
LIMIT ?`, before, after, getRequestedLimit(r), total)
2016-12-11 13:50:01 +00:00
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(results)
})