Commit 0ff8a38
Antonio Mika
·
2026-10-25 13:43:36 -0400 EDT
parent 06a75d5
fix(analytics): stop counting mobile visitors as desktop
2 files changed,
+44,
-1
+1,
-1
| ... | ... | @@ -10,7 +10,7 @@ import ( | |
| 10 | 10 | ) | |
| 11 | 11 | ||
| 12 | 12 | // mobileUserAgentExpr is a SQL expression to detect mobile user agents. | |
| 13 | - | const mobileUserAgentExpr = `user_agent ILIKE '%mobile%' OR user_agent ILIKE '%android%' OR user_agent ILIKE '%iphone%' OR user_agent ILIKE '%ipad%' OR user_agent ILIKE '%ipod%' OR user_agent ILIKE '%blackberry%' OR user_agent ILIKE '%windows phone%'` | |
| 13 | + | const mobileUserAgentExpr = `(user_agent ILIKE '%mobile%' OR user_agent ILIKE '%android%' OR user_agent ILIKE '%iphone%' OR user_agent ILIKE '%ipad%' OR user_agent ILIKE '%ipod%' OR user_agent ILIKE '%blackberry%' OR user_agent ILIKE '%windows phone%')` | |
| 14 | 14 | ||
| 15 | 15 | func (me *PsqlDB) InsertVisit(visit *db.AnalyticsVisits) error { | |
| 16 | 16 | _, err := me.Db.Exec( |
+43,
-0
| ... | ... | @@ -781,6 +781,49 @@ func TestVisitSummary(t *testing.T) { | |
| 781 | 781 | } | |
| 782 | 782 | } | |
| 783 | 783 | ||
| 784 | + | func TestVisitSummaryDeviceSplit(t *testing.T) { | |
| 785 | + | cleanupTestData(t) | |
| 786 | + | ||
| 787 | + | user, _ := testDB.RegisterUser("deviceowner", "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI deviceowner", "comment", "") | |
| 788 | + | ||
| 789 | + | // A phone user agent matches several mobile keywords at once. | |
| 790 | + | _ = testDB.InsertVisit(&db.AnalyticsVisits{ | |
| 791 | + | UserID: user.ID, | |
| 792 | + | Host: "devices.com", | |
| 793 | + | Path: "/", | |
| 794 | + | IpAddress: "192.168.1.10", | |
| 795 | + | UserAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 18_0 like Mac OS X) Mobile/15E148", | |
| 796 | + | Status: 200, | |
| 797 | + | }) | |
| 798 | + | _ = testDB.InsertVisit(&db.AnalyticsVisits{ | |
| 799 | + | UserID: user.ID, | |
| 800 | + | Host: "devices.com", | |
| 801 | + | Path: "/", | |
| 802 | + | IpAddress: "192.168.1.11", | |
| 803 | + | UserAgent: "Mozilla/5.0 (X11; Linux x86_64) Gecko/20100101 Firefox/140.0", | |
| 804 | + | Status: 200, | |
| 805 | + | }) | |
| 806 | + | ||
| 807 | + | summary, err := testDB.VisitSummary(&db.SummaryOpts{ | |
| 808 | + | Interval: "day", | |
| 809 | + | Origin: time.Now().Add(-24 * time.Hour), | |
| 810 | + | Host: "devices.com", | |
| 811 | + | UserID: user.ID, | |
| 812 | + | }) | |
| 813 | + | if err != nil { | |
| 814 | + | t.Fatalf("VisitSummary failed: %v", err) | |
| 815 | + | } | |
| 816 | + | ||
| 817 | + | var mobile, desktop int | |
| 818 | + | for _, iv := range summary.Intervals { | |
| 819 | + | mobile += iv.MobileVisitors | |
| 820 | + | desktop += iv.DesktopVisitors | |
| 821 | + | } | |
| 822 | + | if mobile != 1 || desktop != 1 { | |
| 823 | + | t.Errorf("got %d mobile and %d desktop visitors, want 1 and 1", mobile, desktop) | |
| 824 | + | } | |
| 825 | + | } | |
| 826 | + | ||
| 784 | 827 | func TestFindVisitSiteList(t *testing.T) { | |
| 785 | 828 | cleanupTestData(t) | |
| 786 | 829 |