Files
supermarket/internal/tracking/referrers_test.go
T
davidandClaude Opus 5.5 392980aff6 Phase 8 part 1: links that bring visitors
The daily Plausible import also reads the visitors by referring site,
linking page and entry page, with their sign-ups and leads (a new
referrer_days table). Search engines, social networks, AI assistants,
email and mail filters, work apps, the company's own sites and tagged
links are told apart from links on other sites. SEO → Links lists the
sites that sent visitors in 90 days, the pages they landed on and the
sign-ups they brought, new ones first. A linking page Plausible names
in full is kept as a known backlink (a new backlinks table, for parts 2
and 3); browsers mostly send only the site, so most aren't.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-24 08:23:47 -07:00

101 lines
3.5 KiB
Go

package tracking
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"supermarket/internal/api/plausible"
"supermarket/internal/profile"
)
func TestRefKind(t *testing.T) {
for in, want := range map[[2]string]string{
{"Google", ""}: RefSearch,
{"t.co", ""}: RefSocial,
{"reddit.com", "https://www.reddit.com/r/selfhosted/x"}: RefSocial,
{"ChatGPT", ""}: RefAI,
{"perplexity.ai", ""}: RefAI,
{"wdtprs.com", "https://wdtprs.com/2026/11/post/"}: RefLink,
{"gitea.example.com", ""}: RefOwn,
{"linkedin", ""}: RefSocial,
{"newsletter", ""}: RefEmail,
{"partner-promo", ""}: RefTagged,
{"link.edgepilot.com", ""}: RefEmail,
{"rho.lightning.force.com", ""}: RefApp,
{"matrix.org", ""}: RefApp,
} {
if got := RefKind(in[0], in[1], "example.com"); got != want {
t.Errorf("%v: %s, want %s", in, got, want)
}
}
}
func TestImportReferrers(t *testing.T) {
d := openDB(t)
SaveSignups(d, []string{"/signup"})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var q plausible.Query
json.NewDecoder(r.Body).Decode(&q)
goal := ""
for _, f := range q.Filters {
if f[1] == "event:goal" {
goal = fmt.Sprint(f[2])
}
}
rows := []plausible.Row{}
switch goal {
case "":
rows = []plausible.Row{
{Dimensions: []string{"2026-11-20", "wdtprs.com", "https://wdtprs.com/2026/11/post/#c", "/"}, Metrics: []float64{9}},
{Dimensions: []string{"2026-11-20", "tomwoods.com", "tomwoods.com", "/"}, Metrics: []float64{1}},
{Dimensions: []string{"2026-11-21", "Google", "", "/"}, Metrics: []float64{35}},
{Dimensions: []string{"2026-11-21", "selfhosted.blog", "https://selfhosted.blog/hosts/", "/headscale"}, Metrics: []float64{3}},
}
case "[" + OutboundGoal + "]":
rows = []plausible.Row{{Dimensions: []string{"2026-11-21", "selfhosted.blog", "https://selfhosted.blog/hosts/", "/headscale"}, Metrics: []float64{1}}}
}
json.NewEncoder(w).Encode(map[string]any{"results": rows})
}))
defer srv.Close()
c := plausible.New(srv.URL, "k")
w := profile.Website{URL: "https://example.com/"}
res, err := ImportReferrers(context.Background(), d, c, []profile.Website{w}, now)
if err != nil {
t.Fatal(err)
}
if res.Detail != "Referring sites read; 2 new linking pages found." {
t.Errorf("detail: %s", res.Detail)
}
list, ok, err := Referrers(d, w, 90)
if err != nil || !ok || len(list) != 4 {
t.Fatalf("referrers: %+v %v %v", list, ok, err)
}
var blog *Referrer
for i := range list {
if list[i].Source == "selfhosted.blog" {
blog = &list[i]
}
}
if blog == nil || blog.Kind != RefLink || blog.Visitors != 3 || blog.Signups != 1 || blog.Pages[0] != "/headscale/" || !blog.New {
t.Errorf("blog: %+v", blog)
}
var n int
var src string
d.QueryRow(`SELECT count(*), min(source_url) FROM backlinks WHERE site = ?`, w.URL).Scan(&n, &src)
if n != 2 || src != "https://selfhosted.blog/hosts/" {
t.Errorf("backlinks: %d %s", n, src)
}
// Read again: nothing new, nothing doubled.
d.PutJSON(refStateKey("example.com"), "")
if res, _ := ImportReferrers(context.Background(), d, c, []profile.Website{w}, now); res.Detail != "Referring sites read; 0 new linking pages found." {
t.Errorf("again: %s", res.Detail)
}
if list, _, _ := Referrers(d, w, 90); list[len(list)-1].Visitors+list[0].Visitors < 12 {
t.Errorf("after again: %+v", list)
}
}