add /referrers endpoint and show table with referrers in period

This commit is contained in:
Danny van Kooten 2016-12-10 14:31:46 +01:00
parent a68c97e701
commit f3e8731fae
4 changed files with 63 additions and 2 deletions

58
api/referrers.go Normal file
View File

@ -0,0 +1,58 @@
package api
import (
"net/http"
"github.com/dannyvankooten/ana/db"
"encoding/json"
"strings"
)
// URL: /api/referrers
var GetReferrersHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
before, after := getRequestedPeriods(r)
// get total
stmt, err := db.Conn.Prepare(`
SELECT
COUNT(DISTINCT(pv.visitor_id))
FROM pageviews pv
WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ?`)
checkError(err)
defer stmt.Close()
var total float32
stmt.QueryRow(before, after).Scan(&total)
// get rows
stmt, err = db.Conn.Prepare(`
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 ?`)
checkError(err)
defer stmt.Close()
rows, err := stmt.Query(before, after, getRequestedLimit(r))
checkError(err)
defer rows.Close()
results := make([]Datapoint, 0)
for rows.Next() {
var d Datapoint
err = rows.Scan(&d.Label, &d.Count);
d.Label = strings.Replace(d.Label, "http://", "", 1)
d.Label = strings.Replace(d.Label, "https://", "", 1)
d.Label = strings.TrimRight(d.Label, "/")
checkError(err)
d.Percentage = float32(d.Count) / total * 100
results = append(results, d)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(results)
})

View File

@ -29,12 +29,14 @@ var GetScreenResolutionsHandler = http.HandlerFunc(func(w http.ResponseWriter, r
FROM pageviews pv FROM pageviews pv
LEFT JOIN visitors v ON v.id = pv.visitor_id LEFT JOIN visitors v ON v.id = pv.visitor_id
WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ? WHERE UNIX_TIMESTAMP(pv.timestamp) <= ? AND UNIX_TIMESTAMP(pv.timestamp) >= ?
AND v.screen_resolution IS NOT NULL
AND v.screen_resolution != ""
GROUP BY v.screen_resolution GROUP BY v.screen_resolution
ORDER BY count DESC ORDER BY count DESC
LIMIT ?`) LIMIT ?`)
checkError(err) checkError(err)
defer stmt.Close() defer stmt.Close()
rows, err := stmt.Query(before, after, defaultLimit) rows, err := stmt.Query(before, after, getRequestedLimit(r))
checkError(err) checkError(err)
defer rows.Close() defer rows.Close()

View File

@ -64,7 +64,7 @@ class Dashboard extends Component {
<Table period={this.state.period} endpoint="screen-resolutions" title="Screen Resolutions" headers={["#", "Resolution", "Count", "%"]} /> <Table period={this.state.period} endpoint="screen-resolutions" title="Screen Resolutions" headers={["#", "Resolution", "Count", "%"]} />
</div> </div>
<div class="col-2"> <div class="col-2">
<Table period={this.state.period} endpoint="countries" title="Countries" headers={["#", "Country", "Count", "%"]} labelCell={formatCountryLabel} /> <Table period={this.state.period} endpoint="referrers" title="Referrers" headers={["#", "URL", "Count", "%"]} />
</div> </div>
<div class="col-2"> <div class="col-2">
<Table period={this.state.period} endpoint="browsers" title="Browsers" headers={["#", "Browser", "Count", "%"]} onAuthError={this.props.onLogout} /> <Table period={this.state.period} endpoint="browsers" title="Browsers" headers={["#", "Browser", "Count", "%"]} onAuthError={this.props.onLogout} />

View File

@ -22,6 +22,7 @@ func startServer() {
r.Handle("/api/pageviews/count/group/{period}", api.Authorize(api.GetPageviewsPeriodCountHandler)).Methods("GET") r.Handle("/api/pageviews/count/group/{period}", api.Authorize(api.GetPageviewsPeriodCountHandler)).Methods("GET")
r.Handle("/api/pageviews", api.Authorize(api.GetPageviewsHandler)).Methods("GET") r.Handle("/api/pageviews", api.Authorize(api.GetPageviewsHandler)).Methods("GET")
r.Handle("/api/languages", api.Authorize(api.GetLanguagesHandler)).Methods("GET") r.Handle("/api/languages", api.Authorize(api.GetLanguagesHandler)).Methods("GET")
r.Handle("/api/referrers", api.Authorize(api.GetReferrersHandler)).Methods("GET")
r.Handle("/api/screen-resolutions", api.Authorize(api.GetScreenResolutionsHandler)).Methods("GET") r.Handle("/api/screen-resolutions", api.Authorize(api.GetScreenResolutionsHandler)).Methods("GET")
r.Handle("/api/countries", api.Authorize(api.GetCountriesHandler)).Methods("GET") r.Handle("/api/countries", api.Authorize(api.GetCountriesHandler)).Methods("GET")
r.Handle("/api/browsers", api.Authorize(api.GetBrowsersHandler)).Methods("GET") r.Handle("/api/browsers", api.Authorize(api.GetBrowsersHandler)).Methods("GET")