Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show number of mobile browsers and screen sizes #51

Merged
merged 1 commit into from Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions cron/browser_stat.go
Expand Up @@ -81,13 +81,14 @@ func updateBrowserStats(ctx context.Context, site goatcounter.Site) error {
// Group properly.
type gt struct {
count int
mobile bool
day string
browser string
version string
}
grouped := map[string]gt{}
for _, s := range stats {
browser, version := getBrowser(s.Browser)
browser, version, mobile := getBrowser(s.Browser)
if browser == "" {
continue
}
Expand All @@ -97,27 +98,28 @@ func updateBrowserStats(ctx context.Context, site goatcounter.Site) error {
v.day = s.CreatedAt.Format("2006-01-02")
v.browser = browser
v.version = version
v.mobile = mobile
}
v.count += s.Count
grouped[k] = v
}

insBrowser := bulk.NewInsert(ctx, zdb.MustGet(ctx).(*sqlx.DB),
"browser_stats", []string{"site", "day", "browser", "version", "count"})
"browser_stats", []string{"site", "day", "browser", "version", "count", "mobile"})
for _, v := range grouped {
insBrowser.Values(site.ID, v.day, v.browser, v.version, v.count)
insBrowser.Values(site.ID, v.day, v.browser, v.version, v.count, v.mobile)
}

return insBrowser.Finish()
}

func getBrowser(uaHeader string) (string, string) {
func getBrowser(uaHeader string) (string, string, bool) {
ua := user_agent.New(uaHeader)
browser, version := ua.Browser()

// A lot of this is wrong, so just skip for now.
if browser == "Android" {
return "", ""
return "", "", false
}

if browser == "Chromium" {
Expand Down Expand Up @@ -149,5 +151,6 @@ func getBrowser(uaHeader string) (string, string) {
}
}

return browser, version
mobile := ua.Mobile()
return browser, version, mobile
}
8 changes: 4 additions & 4 deletions cron/browser_stat_test.go
Expand Up @@ -35,13 +35,13 @@ func TestBrowserStats(t *testing.T) {
}

var stats goatcounter.BrowserStats
total, err := stats.List(ctx, now, now)
total, totalMobile, err := stats.List(ctx, now, now)
if err != nil {
t.Fatal(err)
}

want := `3 -> [{Firefox 2} {Chrome 1}]`
out := fmt.Sprintf("%d -> %v", total, stats)
want := `3 -> 0 -> [{Firefox false 2} {Chrome false 1}]`
out := fmt.Sprintf("%d -> %d -> %v", total, totalMobile, stats)
if want != out {
t.Errorf("\nwant: %s\nout: %s", want, out)
}
Expand All @@ -52,7 +52,7 @@ func TestBrowserStats(t *testing.T) {
t.Fatal(err)
}

want = `2 -> [{68.0 1} {69.0 1}]`
want = `2 -> [{68.0 false 1} {69.0 false 1}]`
out = fmt.Sprintf("%d -> %v", total, stats)
if want != out {
t.Errorf("\nwant: %s\nout: %s", want, out)
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/2019-09-19-1-mobile.sql
@@ -0,0 +1,10 @@
begin;
alter table browser_stats
add column mobile boolean default false not null;

update sites set last_stat=null;
delete from hit_stats;
delete from browser_stats;

insert into version values ('2019-09-19-1-mobile');
commit;
53 changes: 35 additions & 18 deletions db/pack.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions db/schema.pgsql
Expand Up @@ -84,6 +84,7 @@ create table browser_stats (
browser varchar not null,
version varchar not null,
count int not null,
mobile int default 0 not null,

foreign key (site) references sites(id) on delete restrict on update restrict
);
Expand Down
3 changes: 2 additions & 1 deletion db/schema.sql
Expand Up @@ -53,8 +53,8 @@ create table hits (
ref_original varchar,
ref_params varchar,
ref_scheme varchar null check(ref_scheme in ('h', 'g', 'o')),
size varchar not null default '',
browser varchar not null,
size varchar not null default '',

created_at timestamp not null check(created_at = strftime('%Y-%m-%d %H:%M:%S', created_at))
);
Expand Down Expand Up @@ -84,6 +84,7 @@ create table browser_stats (
browser varchar not null,
version varchar not null,
count int not null,
mobile int default 0 not null,

foreign key (site) references sites(id) on delete restrict on update restrict
);
Expand Down
31 changes: 23 additions & 8 deletions handlers/backend.go
Expand Up @@ -193,12 +193,22 @@ func (h backend) index(w http.ResponseWriter, r *http.Request) error {
if err != nil {
return err
}
l = l.Since("pages.List")

var browsers goatcounter.BrowserStats
totalBrowsers, err := browsers.List(r.Context(), start, end)
totalBrowsers, totalMobile, err := browsers.List(r.Context(), start, end)
if err != nil {
return err
}
l = l.Since("browsers.List")

var sizeStat goatcounter.BrowserStats
err = sizeStat.ListSize(r.Context(), start, end)
if err != nil {
return err
}

l = l.Since("sizeStat.ListSize")

// Add refers.
sr := r.URL.Query().Get("showrefs")
Expand All @@ -209,14 +219,14 @@ func (h backend) index(w http.ResponseWriter, r *http.Request) error {
if err != nil {
return err
}
l = l.Since("refs.ListRefs")
}

subs, err := goatcounter.MustGetSite(r.Context()).ListSubs(r.Context())
if err != nil {
return err
}

l = l.Since("fetch data")
x := zhttp.Template(w, "backend.gohtml", struct {
Globals
ShowRefs string
Expand All @@ -229,11 +239,16 @@ func (h backend) index(w http.ResponseWriter, r *http.Request) error {
TotalHits int
TotalHitsDisplay int
Browsers goatcounter.BrowserStats
TotalBrowsers uint64
TotalBrowsers int
TotalMobile string
SubSites []string
SizeStat goatcounter.BrowserStats
}{newGlobals(w, r), sr, r.URL.Query().Get("hl-period"), start, end, pages,
refs, moreRefs, total, totalDisplay, browsers, totalBrowsers, subs})
l = l.Since("exec template")
refs, moreRefs, total, totalDisplay, browsers, totalBrowsers,
fmt.Sprintf("%.1f", float32(totalMobile)/float32(totalBrowsers)*100), subs,
sizeStat})
l = l.Since("zhttp.Template")
l.FieldsSince().Print("")
return x
}

Expand Down Expand Up @@ -308,9 +323,9 @@ func (h backend) browsers(w http.ResponseWriter, r *http.Request) error {
return err
}

f := zhttp.FuncMap["hbar_chart"].(func(goatcounter.BrowserStats, uint64, uint64) template.HTML)
t, _ := strconv.ParseUint(r.URL.Query().Get("total"), 10, 64)
tpl := f(browsers, total, t)
f := zhttp.FuncMap["hbar_chart"].(func(goatcounter.BrowserStats, int, int, float32) template.HTML)
t, _ := strconv.ParseInt(r.URL.Query().Get("total"), 10, 64)
tpl := f(browsers, total, int(t), .5)

return zhttp.JSON(w, map[string]interface{}{
"html": string(tpl),
Expand Down