implement referrers table

This commit is contained in:
Danny van Kooten 2018-05-07 19:33:52 +02:00
parent 9a9ea1687c
commit 95cd0e4053
4 changed files with 77 additions and 9 deletions

View File

@ -54,7 +54,7 @@ class Table extends Component {
render(props, state) {
const tableRows = state.records !== null ? state.records.map((p, i) => {
let ahref = document.createElement('a');
ahref.href = p.Pathname;
ahref.href = p.Pathname || p.URL;
let classes = "table-row w"; // TODO: Add percentage of total back in
let label = ahref.pathname + ahref.search;
if( props.showHostname ) {
@ -64,8 +64,8 @@ class Table extends Component {
return(
<div class={classes}>
<div class="cell main-col"><a href={ahref.href}>{label}</a></div>
<div class="cell">{p.Views}</div>
<div class="cell">{p.UniqueViews||"-"}</div>
<div class="cell">{p.Views||p.Pageviews}</div>
<div class="cell">{p.UniqueViews||p.Visitors||"-"}</div>
</div>
)}) : <div class="table-row">Nothing here, yet.</div>;

View File

@ -30,6 +30,7 @@ func Aggregate() error {
}
pages := map[string]*models.PageStats{}
referrers := map[string]*models.ReferrerStats{}
for _, p := range pageviews {
// site stats
@ -77,7 +78,31 @@ func Aggregate() error {
pageStats.Entries += 1
}
// TODO: referrer stats
if p.Referrer != "" {
var referrerStats *models.ReferrerStats
var ok bool
if referrerStats, ok = referrers[p.Referrer]; !ok {
referrerStats, err = getReferrerStats(now, p.Referrer)
if err != nil {
log.Error(err)
continue
}
referrers[p.Referrer] = referrerStats
}
referrerStats.Pageviews += 1
if p.IsNewVisitor {
referrerStats.Visitors += 1
}
}
}
err = datastore.UpdateSiteStats(siteStats)
if err != nil {
log.Error(err)
return err
}
for _, pageStats := range pages {
@ -88,10 +113,12 @@ func Aggregate() error {
}
}
err = datastore.UpdateSiteStats(siteStats)
if err != nil {
log.Error(err)
return err
for _, referrerStats := range referrers {
err = datastore.UpdateReferrerStats(referrerStats)
if err != nil {
log.Error(err)
return err
}
}
// TODO: delete data
@ -132,3 +159,21 @@ func getPageStats(date time.Time, pathname string) (*models.PageStats, error) {
err = datastore.InsertPageStats(stats)
return stats, err
}
func getReferrerStats(date time.Time, url string) (*models.ReferrerStats, error) {
stats, err := datastore.GetReferrerStats(date, url)
if err != nil && err != datastore.ErrNoResults {
return nil, err
}
if stats != nil {
return stats, nil
}
stats = &models.ReferrerStats{
URL: url,
Date: date,
}
err = datastore.InsertReferrerStats(stats)
return stats, err
}

View File

@ -1,11 +1,34 @@
package datastore
import (
"database/sql"
"time"
"github.com/usefathom/fathom/pkg/models"
)
func GetReferrerStats(date time.Time, url string) (*models.ReferrerStats, error) {
stats := &models.ReferrerStats{}
query := dbx.Rebind(`SELECT * FROM daily_referrer_stats WHERE url = ? AND date = ? LIMIT 1`)
err := dbx.Get(stats, query, url, date.Format("2006-01-02"))
if err != nil && err == sql.ErrNoRows {
return nil, ErrNoResults
}
return stats, err
}
func InsertReferrerStats(s *models.ReferrerStats) error {
query := dbx.Rebind(`INSERT INTO daily_referrer_stats(visitors, pageviews, bounces, avg_duration, url, date) VALUES(?, ?, ?, ?, ?, ?)`)
_, err := dbx.Exec(query, s.Visitors, s.Pageviews, s.Bounces, s.AvgDuration, s.URL, s.Date.Format("2006-01-02"))
return err
}
func UpdateReferrerStats(s *models.ReferrerStats) error {
query := dbx.Rebind(`UPDATE daily_referrer_stats SET visitors = ?, pageviews = ?, bounces = ?, avg_duration = ? WHERE url = ? AND date = ?`)
_, err := dbx.Exec(query, s.Visitors, s.Pageviews, s.Bounces, s.AvgDuration, s.URL, s.Date.Format("2006-01-02"))
return err
}
func GetAggregatedReferrerStats(startDate time.Time, endDate time.Time, limit int) ([]*models.ReferrerStats, error) {
var result []*models.ReferrerStats
query := dbx.Rebind(`SELECT url, SUM(visitors) AS visitors, SUM(pageviews) AS pageviews FROM daily_referrer_stats WHERE date >= ? AND date <= ? GROUP BY url, visitors, pageviews ORDER BY pageviews DESC LIMIT ?`)

View File

@ -10,5 +10,5 @@ type ReferrerStats struct {
Pageviews int64 `db:"pageviews"`
Bounces int64 `db:"bounces"`
AvgDuration int64 `db:"avg_duration"`
Date time.Time `db:"date"`
Date time.Time `db:"date" json:"omitempty"`
}