Phase 3 part 5: the next keywords

Candidates a site has no page for, from Search Console searches it ranks
weakly for, the profile's keywords and customers' problems, the signals'
search lines, and competitors' sitemaps; each checked against our own
pages. Buyer searches first. On request, Claude's web search reads the
live top 3 and types each result (big brand, marketplace, forum, small
site), and keywords are ranked by who has to be beaten. Pick, dismiss or
bring back. Schema 16.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 08:11:27 -07:00
co-authored by Claude Opus 5.5
parent 438192faba
commit c0a5ddf77f
13 changed files with 846 additions and 2 deletions
+89
View File
@@ -290,3 +290,92 @@ func (s *Server) testGitea(ctx context.Context) map[string]any {
}
return res
}
// --- The next keywords ---
func (s *Server) seoKeywordsPage(w http.ResponseWriter, r *http.Request) {
s.renderKeywords(w, r, "")
}
func (s *Server) renderKeywords(w http.ResponseWriter, r *http.Request, errMsg string) {
sites, cur := s.auditSites(r)
data := map[string]any{"Sites": sites, "Current": cur}
if cur != nil {
for _, st := range []string{seo.KeywordNew, seo.KeywordPicked, seo.KeywordDismissed} {
ks, err := seo.Keywords(s.DB, auditKey(*cur), st)
if err != nil {
s.serverError(w, err)
return
}
data[st] = ks
}
if _, err := s.researcher(); err != nil {
data["NoSearch"] = err.Error()
}
}
s.render(w, r, "seo-keywords", page{Title: "Next keywords", Nav: "seo/keywords", Error: errMsg, Data: data})
}
func (s *Server) seoKeywordsFind(w http.ResponseWriter, r *http.Request) {
_, cur := s.auditSites(r)
if cur == nil {
http.NotFound(w, r)
return
}
p, _ := profile.Load(s.DB)
ctx, cancel := context.WithTimeout(r.Context(), 5*time.Minute)
defer cancel()
if _, err := seo.FindKeywords(ctx, s.DB, auditKey(*cur), cur.Property, p, "Supermarket/"+s.Version+" (SEO)", time.Now()); err != nil {
s.renderKeywords(w, r, "Couldn't look for keywords: "+err.Error())
return
}
redirect(w, r, "/seo/keywords?site="+strconv.Itoa(cur.Index), "keywords-found")
}
func (s *Server) seoKeywordAction(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
http.NotFound(w, r)
return
}
k, err := seo.GetKeyword(s.DB, id)
if err != nil {
http.NotFound(w, r)
return
}
site, ok := s.siteFor(k.Site)
if !ok {
http.NotFound(w, r)
return
}
back := "/seo/keywords?site=" + strconv.Itoa(site.Index) + "#k-" + strconv.FormatInt(id, 10)
now := time.Now()
switch r.PathValue("action") {
case "top":
rs, rerr := s.researcher()
if rerr == nil {
ctx, cancel := context.WithTimeout(r.Context(), 4*time.Minute)
defer cancel()
_, rerr = seo.ReadTop(ctx, s.DB, rs, id, site.URL, now)
}
if rerr != nil {
s.renderKeywords(w, r, "Couldn't read the top results: "+rerr.Error())
return
}
redirect(w, r, back, "top-read")
case "pick":
err = seo.SetKeywordStatus(s.DB, id, seo.KeywordPicked, now)
case "dismiss":
err = seo.SetKeywordStatus(s.DB, id, seo.KeywordDismissed, now)
case "restore":
err = seo.SetKeywordStatus(s.DB, id, seo.KeywordNew, now)
default:
http.NotFound(w, r)
return
}
if err != nil {
s.renderKeywords(w, r, err.Error())
return
}
redirect(w, r, back, "saved")
}
+62
View File
@@ -0,0 +1,62 @@
package web
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"supermarket/internal/ai"
"supermarket/internal/profile"
"supermarket/internal/secrets"
"supermarket/internal/seo"
)
type cannedResearcher struct{ cannedAI }
func (cannedResearcher) Research(context.Context, ai.ResearchRequest) (*ai.ResearchResult, error) {
return &ai.ResearchResult{Text: `{"results":[{"url":"https://forum.example/1","title":"A thread","kind":"forum","note":"old"}]}`,
Sources: []ai.Source{{URL: "https://forum.example/1"}}, Searches: 1}, nil
}
func TestKeywordsPage(t *testing.T) {
newAIClient = func(ai.Config, *secrets.Store) (ai.Client, error) { return cannedResearcher{}, nil }
defer func() { newAIClient = ai.New }()
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `<urlset></urlset>`)
}))
defer srv.Close()
s, h := newTestServer(t, nil)
profile.Save(s.DB, &profile.Profile{Name: "Us", Keywords: []string{"private cloud pricing"}, Websites: []profile.Website{{URL: srv.URL}}})
if body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String(); !strings.Contains(body, "No candidates yet") {
t.Fatal("no empty state")
}
if rec := postForm(h, "/seo/keywords?site=0", nil); rec.Code != http.StatusSeeOther {
t.Fatalf("find: %d %s", rec.Code, rec.Body)
}
body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String()
for _, want := range []string{"private cloud pricing", "buyer", "From profile keyword", "Read the live top 3"} {
if !strings.Contains(body, want) {
t.Errorf("page lacks %q", want)
}
}
ks, _ := seo.Keywords(s.DB, srv.URL, seo.KeywordNew)
id := fmt.Sprint(ks[0].ID)
if rec := postForm(h, "/seo/keywords/"+id+"/top", nil); rec.Code != http.StatusSeeOther {
t.Fatalf("top: %d %s", rec.Code, rec.Body)
}
if body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String(); !strings.Contains(body, "A thread") || !strings.Contains(body, "winnable for a small site") {
t.Error("the top results aren't shown")
}
postForm(h, "/seo/keywords/"+id+"/pick", nil)
if body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String(); !strings.Contains(body, "<h2>Picked</h2>") {
t.Error("the pick isn't shown")
}
postForm(h, "/seo/keywords/"+id+"/dismiss", nil)
if body := do(h, http.MethodGet, "/seo/keywords", nil, nil).Body.String(); !strings.Contains(body, "Dismissed (1)") {
t.Error("dismissed isn't shown")
}
}
+5
View File
@@ -161,6 +161,9 @@ func (s *Server) Handler() (http.Handler, error) {
mux.HandleFunc("POST /settings/email/send-test", s.emailSendTest)
mux.HandleFunc("GET /seo", s.seoPage)
mux.HandleFunc("GET /seo/check", s.seoCheckPage)
mux.HandleFunc("GET /seo/keywords", s.seoKeywordsPage)
mux.HandleFunc("POST /seo/keywords", s.seoKeywordsFind)
mux.HandleFunc("POST /seo/keywords/{id}/{action}", s.seoKeywordAction)
mux.HandleFunc("GET /seo/changes", s.seoChangesPage)
mux.HandleFunc("POST /seo/changes/suggest", s.seoChangesSuggest)
mux.HandleFunc("POST /seo/changes/page", s.seoChangeForPage)
@@ -418,6 +421,8 @@ var flashes = map[string]string{
"google-connected": "Search Console is connected. Choose each website's property below, then sync.",
"synced": "Synced from Search Console.",
"report-written": "This week's report is written.",
"keywords-found": "Looked for keywords: the candidates are below.",
"top-read": "Read the live top results.",
"proposed": "Proposed: read each one, edit it if you like, then say yes or no.",
"approved": "Approved: Supermarket is making the pull request; it shows here in a minute.",
"declined": "Declined.",
+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",
"/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",
"/signals", "/signals?period=30d&channel=email", "/signals/new", "/settings/sources"} {
rec := do(h, http.MethodGet, path, nil, nil)
if rec.Code != http.StatusOK {
+1 -1
View File
@@ -506,7 +506,7 @@ func (s *Server) researcher() (ai.Researcher, error) {
if err != nil {
return nil, err
}
c, err := ai.New(cfg, s.Secrets)
c, err := newAIClient(cfg, s.Secrets)
if err != nil {
return nil, err
}
+1
View File
@@ -44,6 +44,7 @@
<a href="/seo/report" {{if eq $nav "seo/report"}}aria-current="page"{{end}}>Monday report</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>
<a href="/seo/audit" {{if eq $nav "seo/audit"}}aria-current="page"{{end}}>Audit</a>
</div>
{{end}}
+54
View File
@@ -0,0 +1,54 @@
{{define "content"}}{{with .Data}}{{$d := .}}
<div class="heading">
<h1>Next keywords</h1>
{{if .Current}}<div class="buttons">
<form method="post" action="/seo/keywords?site={{.Current.Index}}" data-busy="Looking…"><button type="submit" class="secondary">Look for keywords</button></form>
</div>{{end}}
</div>
<p class="lede">Searches your site has no page for yet, from four places: what Google already shows you for but weakly (below page two), your profile's keywords and your customers' problems, your signals' search lines, and the topics your competitors' sites cover. Each is checked against your own pages, so “no page” means no page. Buyer searches (best, price, vs, alternative, hosting…) come first. Search volume and difficulty aren't available; what counts is who you'd have to beat, so read a keyword's live top 3 before you pick it. Then pick one: not ten.</p>
{{if not .Sites}}
<section class="card empty"><p>Add your websites to the <a href="/settings/profile">profile</a> first.</p></section>
{{else}}
{{if gt (len .Sites) 1}}<nav class="tabs">{{range .Sites}}<a href="/seo/keywords?site={{.Index}}" {{if eq .Index $d.Current.Index}}aria-current="page"{{end}}>{{.URL}}</a>{{end}}</nav>{{end}}
{{with .picked}}
<h2>Picked</h2>
{{range .}}{{template "keyword" .}}{{end}}
{{end}}
<h2>Candidates</h2>
{{with .NoSearch}}<p class="help">Reading the top results needs Claude with web search: {{.}}</p>{{end}}
{{range .new}}{{template "keyword" .}}{{else}}
<section class="card empty"><p>No candidates yet. <em>Look for keywords</em> gathers them; it reads your competitors' sitemaps, so it can take a minute.</p></section>
{{end}}
{{with .dismissed}}
<details><summary>Dismissed ({{len .}})</summary>{{range .}}{{template "keyword" .}}{{end}}</details>
{{end}}
{{end}}
{{end}}{{end}}
{{define "keyword"}}
<section class="card keyword" id="k-{{.ID}}">
<div class="prospect-head">
<div>
<h3>{{.Keyword}} {{if eq .Intent "buyer"}}<span class="badge published">buyer</span>{{end}}</h3>
<p class="small muted">From {{range $i, $s := .Sources}}{{if $i}}, {{end}}{{$s}}{{end}}.
{{if .Impressions}}Google already shows you for it: {{num .Impressions}} impressions in 90 days at position {{dec1 .Position}}{{with .ShownOn}}, on <a href="{{.}}" target="_blank" rel="noopener noreferrer">{{pagePath .}}</a>{{end}}.{{end}}
Volume and difficulty: not available.</p>
</div>
<div class="row-actions">
{{if ne .Status "picked"}}<form method="post" action="/seo/keywords/{{.ID}}/pick"><button type="submit" class="small">Pick this one</button></form>{{end}}
{{if eq .Status "dismissed"}}<form method="post" action="/seo/keywords/{{.ID}}/restore"><button type="submit" class="secondary small">Bring back</button></form>
{{else}}<form method="post" action="/seo/keywords/{{.ID}}/dismiss"><button type="submit" class="secondary small">Dismiss</button></form>{{end}}
</div>
</div>
{{if .Top}}
<p class="small"><strong>The live top 3</strong> ({{ago .TopAt}}){{if ge .Hard 0}}: {{if le .Hard 3}}winnable for a small site{{else if le .Hard 6}}a fight{{else}}held by the big ones{{end}}{{end}}</p>
<ol class="small">{{range .Top}}<li><a href="{{.URL}}" target="_blank" rel="noopener noreferrer">{{.Title}}</a> <span class="badge">{{.Kind}}</span> {{.Note}}</li>{{end}}</ol>
{{else}}
<form method="post" action="/seo/keywords/{{.ID}}/top" data-busy="Searching…" class="inline"><button type="submit" class="link small">Read the live top 3 (up to 3 web searches)</button></form>
{{end}}
</section>
{{end}}