Phase 7 part 4: SEO → What to write next

One list per website of everything worth writing: new wording for pages
(Push pages and the audit's title and description issues), landing
pages for signals that get replies, pages for apps people name in their
replies (their own words, without quoted mail or signatures), and
keywords or briefs that stalled. Ranked by the sign-ups each is expected
to bring, at most 10 shown, each with its reason in numbers and the
button that starts its flow; Not now hides one for 90 days. The SEO
module still opens on the Morning check; the Monday report links here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-24 07:17:42 -07:00
co-authored by Claude Opus 5.5
parent 855615ae7f
commit 5705a9b58b
10 changed files with 264 additions and 4 deletions
+154
View File
@@ -0,0 +1,154 @@
package web
import (
"net/http"
"slices"
"strconv"
"strings"
"time"
"supermarket/internal/next"
"supermarket/internal/profile"
"supermarket/internal/seo"
)
// What to write next (SEO → What to write next): SEO's suggestions and the
// funnel's in one list, ranked by the sign-ups they're expected to bring.
const (
maxNext = 10
nextDismissed = "next:dismissed" // suggestion key → when it was dismissed
dismissFor = 90 * 24 * time.Hour
)
// nextFor works out a website's list, without the dismissed suggestions;
// more is how many fell below the first maxNext.
func (s *Server) nextFor(cur seoSite, now time.Time) (list []next.Suggestion, more int, err error) {
var all []next.Suggestion
site := strconv.Itoa(cur.Index)
// SEO: the pages that would gain most from new wording (Push pages and
// the audit's title and description issues), by expected sign-ups.
if cur.Property != "" {
conv := s.seoConv(cur.URL)
cands, err := seo.Candidates(s.DB, auditKey(cur), cur.Property, conv, now)
if err != nil {
return nil, 0, err
}
for _, c := range cands {
what, kind := "", "New title and description"
switch {
case c.Title && !c.Desc:
what, kind = "title", "New title"
case c.Desc && !c.Title:
what, kind = "description", "New description"
}
score := c.Expected
if conv == nil || score == 0 {
// Without conversion data, by how often Google shows it.
score = float64(c.Traffic.Impressions) / 20000
}
all = append(all, next.Suggestion{
Key: "change:" + seo.PathKey(c.Page), Kind: kind, Title: pagePath(c.Page), Why: c.Why, Score: score,
Action: next.Action{Label: "Propose new wording", Method: "POST", URL: "/seo/changes/page?site=" + site, Busy: "Writing…",
Fields: map[string]string{"page": c.Page, "why": c.Why, "what": what}},
})
}
}
// The funnel: signals without a landing page, apps people name.
sigs, err := next.SignalsWithoutPages(s.DB, now)
if err != nil {
return nil, 0, err
}
all = append(all, sigs...)
if p, _ := profile.Load(s.DB); p != nil {
var paths, titles []string
if a, _ := seo.LatestAudit(s.DB, cur.URL); a != nil {
for _, pg := range a.Pages {
paths = append(paths, seo.PathKey(pg.URL))
titles = append(titles, pg.Title)
}
}
if len(paths) > 0 { // without the site's pages, any app would look missing
ms, err := next.AppsInReplies(s.DB, p, paths, titles)
if err != nil {
return nil, 0, err
}
all = append(all, next.MentionSuggestions(ms, p)...)
}
}
stalled, err := next.Stalled(s.DB, cur.URL, cur.Index, now)
if err != nil {
return nil, 0, err
}
all = append(all, stalled...)
// Not the dismissed ones; the most promising first.
var dismissed map[string]time.Time
_, _ = s.DB.GetJSON(nextDismissed, &dismissed)
seen := map[string]bool{}
for _, sg := range all {
if at, ok := dismissed[sg.Key]; (ok && now.Sub(at) < dismissFor) || seen[sg.Key] {
continue
}
seen[sg.Key] = true
list = append(list, sg)
}
slices.SortStableFunc(list, func(a, b next.Suggestion) int {
switch {
case a.Score > b.Score:
return -1
case a.Score < b.Score:
return 1
}
return strings.Compare(a.Key, b.Key)
})
if len(list) > maxNext {
more = len(list) - maxNext
list = list[:maxNext]
}
return list, more, nil
}
func (s *Server) seoNextPage(w http.ResponseWriter, r *http.Request) {
data, cur := s.seoBase(r)
data["Path"] = "/seo/next"
if cur != nil {
list, more, err := s.nextFor(*cur, time.Now())
if err != nil {
s.serverError(w, err)
return
}
data["Next"], data["More"] = list, more
data["HasConv"] = s.seoConv(cur.URL) != nil
}
s.render(w, r, "seo-next", page{Title: "What to write next", Nav: "seo/next", Data: data})
}
// seoNextDismiss hides a suggestion for 90 days.
func (s *Server) seoNextDismiss(w http.ResponseWriter, r *http.Request) {
key := strings.TrimSpace(r.PostFormValue("key"))
if key == "" || len(key) > 300 {
http.Error(w, "no suggestion", http.StatusBadRequest)
return
}
var dismissed map[string]time.Time
_, _ = s.DB.GetJSON(nextDismissed, &dismissed)
if dismissed == nil {
dismissed = map[string]time.Time{}
}
now := time.Now()
for k, at := range dismissed { // forget the ones that came back anyway
if now.Sub(at) > dismissFor {
delete(dismissed, k)
}
}
dismissed[key] = now
if err := s.DB.PutJSON(nextDismissed, dismissed); err != nil {
s.serverError(w, err)
return
}
idx, _ := strconv.Atoi(r.URL.Query().Get("site"))
redirect(w, r, "/seo/next?site="+strconv.Itoa(idx), "next-dismissed")
}
+31
View File
@@ -5,12 +5,15 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"
"time"
"supermarket/internal/oauth"
"supermarket/internal/profile"
"supermarket/internal/secrets"
"supermarket/internal/signals"
)
func TestSearchConsole(t *testing.T) {
@@ -140,3 +143,31 @@ func TestSignInHostsAllowedAsFormTargets(t *testing.T) {
}
}
}
func TestWhatToWriteNext(t *testing.T) {
s, h := newTestServer(t, nil)
p := &profile.Profile{Name: "Acme", Websites: []profile.Website{{URL: "https://acme.example", SearchConsoleProperty: "sc-domain:acme.example"}}}
if err := profile.Save(s.DB, p); err != nil {
t.Fatal(err)
}
sig := &signals.Signal{Name: "Leaving M365", Status: "testing"}
signals.Save(s.DB, sig)
signals.Record(s.DB, signals.Event{SignalID: sig.ID, Stage: signals.StageReplied, Channel: "email", Source: "manual", Subject: "contact:1", ExternalID: "r1", At: time.Now().AddDate(0, 0, -2)})
body := do(h, http.MethodGet, "/seo/next", nil, nil).Body.String()
if !strings.Contains(body, "Leaving M365") || !strings.Contains(body, "kind=landing&amp;signal=") || !strings.Contains(body, "Ranked by impressions for now") {
t.Fatalf("next: %s", body)
}
rec := postForm(h, "/seo/next/dismiss?site=0", url.Values{"key": {"signal-page:" + strconv.FormatInt(sig.ID, 10)}})
if rec.Code != http.StatusSeeOther {
t.Fatalf("dismiss: %d", rec.Code)
}
if body := do(h, http.MethodGet, "/seo/next", nil, nil).Body.String(); strings.Contains(body, "Leaving M365") {
t.Error("a dismissed suggestion still shows")
}
// The New pages form takes the suggestion's kind and signal.
if body := do(h, http.MethodGet, "/sites/pages?kind=landing&signal="+strconv.FormatInt(sig.ID, 10), nil, nil).Body.String(); strings.Contains(body, "<form method=\"post\" action=\"/sites/pages\"") &&
!strings.Contains(body, `value="landing" selected`) {
t.Error("New pages wasn't filled in")
}
}
+4
View File
@@ -222,6 +222,8 @@ func (s *Server) Handler() (http.Handler, error) {
mux.HandleFunc("POST /seo/audit", s.seoAuditRun)
mux.HandleFunc("GET /seo/audit/status", s.seoAuditStatus)
mux.HandleFunc("GET /seo/report", s.seoReportPage)
mux.HandleFunc("GET /seo/next", s.seoNextPage)
mux.HandleFunc("POST /seo/next/dismiss", s.seoNextDismiss)
mux.HandleFunc("POST /seo/report", s.seoReportWrite)
mux.HandleFunc("GET /settings/search", s.searchSettingsPage)
mux.HandleFunc("POST /settings/search", s.searchSettingsSave)
@@ -396,6 +398,7 @@ func (s *Server) loadTemplates() error {
"dec1": func(f float64) string { return strconv.FormatFloat(f, 'f', 1, 64) },
"pagePath": pagePath,
"change": seo.Percent,
"inc": func(i int) int { return i + 1 },
"flagOf": func(m map[string]string, page string) string { return m[seo.PathKey(page)] },
"conv": func(m map[string]tracking.PageNumbers, page string) tracking.PageNumbers {
return m[tracking.PathOf(page)]
@@ -539,6 +542,7 @@ var flashes = map[string]string{
"keyword-dismissed": "Dismissed: it's under Dismissed if you change your mind.",
"keyword-back": "Back among the candidates.",
"signups-saved": "Saved. The pages' sign-ups were read again with it.",
"next-dismissed": "Hidden for 90 days.",
"audited": "The audit is done.",
"auditing": "Auditing. It reads up to 300 pages politely, so it takes a few minutes; this page updates when it's done.",
"no-data": "There isn't two weeks of Search Console data yet.",
+1 -1
View File
@@ -100,7 +100,7 @@ func TestEveryPageRenders(t *testing.T) {
"/settings/ai", "/settings/accounts", "/settings/core", "/static/app.css", "/favicon.ico", "/static/favicon.png", "/static/logo.png", "/static/apple-touch-icon.png", "/search", "/crm", "/search?q=rival", "/search?q=nothing+here", "/static/htmx.min.js", "/healthz",
"/content", "/content/1", "/content/1?dismissed=1", "/posts", "/posts?msg=drafted",
"/content/scheduled", "/content/published",
"/prospects", "/prospects?status=accepted", "/prospects?status=rejected", "/prospects/new", "/prospects/suppressed", "/outreach", "/outreach?tab=queued", "/outreach?tab=sent", "/outreach?tab=replied", "/outreach?tab=failed", "/settings/email", "/settings/tracking", "/settings/search", "/seo", "/seo/check", "/seo/report", "/seo/audit", "/seo/changes", "/seo/keywords", "/seo/briefs", "/seo/engines", "/metrics", "/metrics/customers", "/metrics/customers?show=all", "/metrics/spend", "/metrics/clv", "/metrics/clv?view=profit", "/metrics/publish", "/sites", "/sites?site=0", "/sites/previews", "/sites/pages", "/sites/publishing", "/sites/forms", "/sites/new", "/settings/backups",
"/prospects", "/prospects?status=accepted", "/prospects?status=rejected", "/prospects/new", "/prospects/suppressed", "/outreach", "/outreach?tab=queued", "/outreach?tab=sent", "/outreach?tab=replied", "/outreach?tab=failed", "/settings/email", "/settings/tracking", "/settings/search", "/seo", "/seo/check", "/seo/report", "/seo/next", "/seo/audit", "/seo/changes", "/seo/keywords", "/seo/briefs", "/seo/engines", "/metrics", "/metrics/customers", "/metrics/customers?show=all", "/metrics/spend", "/metrics/clv", "/metrics/clv?view=profit", "/metrics/publish", "/sites", "/sites?site=0", "/sites/previews", "/sites/pages", "/sites/publishing", "/sites/forms", "/sites/new", "/settings/backups",
"/signals", "/signals?period=30d&channel=email", "/signals/new", "/settings/sources"} {
rec := do(h, http.MethodGet, path, nil, nil)
if rec.Code != http.StatusOK {
+6
View File
@@ -475,3 +475,9 @@ svg.line-chart line.axis { stroke: hsl(var(--border)); }
.click-chart { margin: 0.5rem 0 0.75rem; }
.click-chart .key { display: inline-block; width: 1.25rem; height: 0; vertical-align: middle; border-top: 2px solid hsl(var(--primary)); }
.click-chart .key.prev { border-top: 2px dashed hsl(var(--muted-foreground)); }
/* What to write next. */
.next-item .next-head { display: flex; gap: 0.85rem; align-items: flex-start; }
.next-item .next-head h3 { margin: 0; }
.next-item .next-head p { margin: 0 0 0.15rem; }
.next-rank { flex: none; width: 1.9rem; height: 1.9rem; border-radius: 999px; display: grid; place-items: center; font-weight: 600; background: hsl(var(--accent)); color: hsl(var(--accent-foreground)); }
+1
View File
@@ -45,6 +45,7 @@
<div class="subnav" id="subnav-seo" data-subnav="seo"{{if hasPrefix $nav "seo"}} data-current{{end}}>
<a href="/seo/check" {{if eq $nav "seo/check"}}aria-current="page"{{end}}>Morning check</a>
<a href="/seo/report" {{if eq $nav "seo/report"}}aria-current="page"{{end}}>Monday report</a>
<a href="/seo/next" {{if eq $nav "seo/next"}}aria-current="page"{{end}}>What to write next</a>
<a href="/seo" {{if eq $nav "seo/ranks"}}aria-current="page"{{end}}>What ranks</a>
<a href="/seo/changes" {{if eq $nav "seo/changes"}}aria-current="page"{{end}}>Changes</a>
<a href="/seo/keywords" {{if eq $nav "seo/keywords"}}aria-current="page"{{end}}>Next keywords</a>
+31
View File
@@ -0,0 +1,31 @@
{{define "content"}}{{with .Data}}{{$d := .}}
<div class="heading">
<h1>What to write next</h1>
</div>
<p class="lede">Everything worth writing, in one list: new wording for pages Google already shows, landing pages for signals that get replies, pages for apps people name, and keywords or briefs that stalled. The ones expected to bring most sign-ups come first. Nothing is written until you press its button, and a brief still asks its questions first.</p>
{{template "seo-sites" .}}
{{if .Current}}
{{if not .HasConv}}<p class="help">Ranked by impressions for now: say what counts as a sign-up under <a href="/settings/tracking#signups">Settings → Website &amp; sales</a> to rank by sign-ups.</p>{{end}}
{{range $i, $s := .Next}}
<section class="card next-item">
<div class="next-head">
<span class="next-rank">{{inc $i}}</span>
<div>
<p class="small muted">{{.Kind}}</p>
<h3>{{.Title}}</h3>
</div>
</div>
<p class="small">{{.Why}}</p>
<div class="row-actions">
{{with .Action}}{{if eq .Method "GET"}}<a class="button small" href="{{.URL}}">{{.Label}}</a>
{{else}}<form method="post" action="{{.URL}}"{{with .Busy}} data-busy="{{.}}"{{end}}>{{range $k, $v := .Fields}}<input type="hidden" name="{{$k}}" value="{{$v}}">{{end}}<button type="submit" class="small">{{.Label}}</button></form>{{end}}{{end}}
<form method="post" action="/seo/next/dismiss?site={{$d.Current.Index}}"><input type="hidden" name="key" value="{{.Key}}"><button type="submit" class="secondary small">Not now</button></form>
</div>
</section>
{{else}}
<section class="card empty"><p>Nothing to suggest yet: sync Search Console, run an audit, and let the funnel bring replies in.</p></section>
{{end}}
{{with .More}}<p class="help">And {{.}} more, below these. <em>Not now</em> hides one for 90 days.</p>{{end}}
{{end}}
{{end}}{{end}}
+1
View File
@@ -37,6 +37,7 @@
<p><a href="{{.Page}}" target="_blank" rel="noopener noreferrer">{{pagePath .Page}}</a><br><span class="small">{{.Why}}</span></p>
{{else}}<p class="help">Nothing is waiting: no change has been proposed.</p>{{end}}
{{if .Waiting}}<p class="help">Say yes or no to each on <a href="/seo/changes?site={{$d.Current.Index}}">Changes</a>: a yes opens a pull request.</p>{{end}}
<p class="help">Everything worth writing, ranked, is on <a href="/seo/next?site={{$d.Current.Index}}">What to write next</a>.</p>
{{with .Candidates}}
<h3>Worth a proposal</h3>
{{range .}}<p><a href="{{.Page}}" target="_blank" rel="noopener noreferrer">{{pagePath .Page}}</a><br><span class="small">{{.Why}}</span></p>{{end}}