Settings → Website & sales says which clicked links count as a sign-up: Supermarket suggests the sign-up, trial and checkout links visitors clicked, and the user ticks, unticks or adds their own. The Plausible import reads, per page a visit began on, the visitors, sign-ups and form leads (a new page_days table); What ranks, the audit and Sites show visits and sign-ups next to each page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
105 lines
3.4 KiB
Go
105 lines
3.4 KiB
Go
package tracking
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"supermarket/internal/api/plausible"
|
|
)
|
|
|
|
// fakePlausible answers the page import's queries: clicked links, and
|
|
// visitors by day and entry page, for all visits, sign-ups and leads.
|
|
func fakePlausible(t *testing.T, queries *[]plausible.Query) *plausible.Client {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
var q plausible.Query
|
|
json.NewDecoder(r.Body).Decode(&q)
|
|
*queries = append(*queries, q)
|
|
rows := []plausible.Row{}
|
|
goal := ""
|
|
for _, f := range q.Filters {
|
|
if f[1] == "event:goal" {
|
|
goal = fmt.Sprint(f[2])
|
|
}
|
|
}
|
|
switch {
|
|
case len(q.Dimensions) == 1 && q.Dimensions[0] == "event:props:url":
|
|
rows = []plausible.Row{{Dimensions: []string{"https://cloud.example/login"}, Metrics: []float64{6}},
|
|
{Dimensions: []string{"https://enterprise.example/signup?plan=x"}, Metrics: []float64{5}},
|
|
{Dimensions: []string{"https://docs.example/"}, Metrics: []float64{2}}}
|
|
case goal == "[Lead]":
|
|
rows = []plausible.Row{{Dimensions: []string{"2026-11-20", "/partners"}, Metrics: []float64{1}}}
|
|
case goal != "":
|
|
rows = []plausible.Row{{Dimensions: []string{"2026-11-20", "/freescout/"}, Metrics: []float64{2}}}
|
|
default:
|
|
rows = []plausible.Row{{Dimensions: []string{"2026-11-20", "/freescout/"}, Metrics: []float64{40}},
|
|
{Dimensions: []string{"2026-11-21", "/freescout/index.html"}, Metrics: []float64{10}},
|
|
{Dimensions: []string{"2026-11-21", "/partners/"}, Metrics: []float64{30}}}
|
|
}
|
|
json.NewEncoder(w).Encode(map[string]any{"results": rows})
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return plausible.New(srv.URL, "k")
|
|
}
|
|
|
|
func TestImportPages(t *testing.T) {
|
|
d := openDB(t)
|
|
var queries []plausible.Query
|
|
c := fakePlausible(t, &queries)
|
|
|
|
links, err := OutboundLinks(context.Background(), c, "example.com")
|
|
if err != nil || len(links) != 3 {
|
|
t.Fatal(links, err)
|
|
}
|
|
var urls []string
|
|
for _, l := range links {
|
|
urls = append(urls, l.URL)
|
|
}
|
|
if s := LoadSignups(d, urls); s.Saved || len(s.Patterns) != 1 || s.Patterns[0] != "enterprise.example/signup" {
|
|
t.Errorf("suggested: %+v", s)
|
|
}
|
|
|
|
if _, err := ImportPages(context.Background(), d, c, []string{"example.com"}, now); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pages, ok, err := Pages(d, "example.com", 90)
|
|
if err != nil || !ok {
|
|
t.Fatal(ok, err)
|
|
}
|
|
if f := pages["/freescout/"]; f.Visitors != 50 || f.Signups != 2 || f.Leads != 0 {
|
|
t.Errorf("freescout: %+v", f)
|
|
}
|
|
if p := pages["/partners/"]; p.Visitors != 30 || p.Leads != 1 || p.Rate() < 0.03 {
|
|
t.Errorf("partners: %+v", p)
|
|
}
|
|
// The suggestion was used for the sign-ups query.
|
|
found := false
|
|
for _, q := range queries {
|
|
for _, f := range q.Filters {
|
|
if f[1] == "event:props:url" && fmt.Sprint(f[2]) == "[enterprise.example/signup]" {
|
|
found = true
|
|
}
|
|
}
|
|
}
|
|
if !found {
|
|
t.Errorf("no sign-up filter in %+v", queries)
|
|
}
|
|
|
|
// The user's own definition replaces it, and the pages are read again.
|
|
if err := SaveSignups(d, []string{" Cloud.example/login ", "", "cloud.example/login"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if s := LoadSignups(d, urls); !s.Saved || len(s.Patterns) != 1 || !s.Matches("https://CLOUD.example/login?x") || s.Matches("https://enterprise.example/signup") {
|
|
t.Errorf("saved: %+v", s)
|
|
}
|
|
if _, ok, _ := Pages(d, "example.com", 90); ok {
|
|
t.Error("the pages weren't reset")
|
|
}
|
|
if PathOf("https://www.example.com/FreeScout") != "/freescout/" {
|
|
t.Error("PathOf")
|
|
}
|
|
}
|