Sites → Sections and pages → Add a section, on any page built from sections: choose one of the site's section types and where it goes; the AI writes it from the page, its searches, the profile and the Inventory, with the same rules as a new page. It's reviewed like a page (claims to confirm, to-dos), and the pull request inserts it into the page's file as text, so only that section is added; the build checks it. What to write next suggests a FAQ for a section page found with questions, and a comparison for one searched against something else. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
236 lines
6.6 KiB
Go
236 lines
6.6 KiB
Go
package seo
|
|
|
|
import (
|
|
"fmt"
|
|
"math"
|
|
"net/url"
|
|
"slices"
|
|
"strings"
|
|
|
|
"supermarket/internal/db"
|
|
)
|
|
|
|
// What the website's pages turn visits into, from Plausible (the tracking
|
|
// package reads it), so suggestions can rank by sign-ups, not by
|
|
// impressions alone.
|
|
|
|
// PageConv is a page's visits, and the visits that went on to sign up or
|
|
// send a form, over the same 90 days What ranks covers.
|
|
type PageConv struct {
|
|
Visitors int
|
|
Conversions int
|
|
}
|
|
|
|
// Thresholds for the page rates and flags.
|
|
const (
|
|
minVisitsForRate = 30 // fewer visits than this: the site's rate is used
|
|
noSignupsVisits = 100 // this many visits and no sign-up: flagged
|
|
hiddenMaxImpr = 300 // converts well, but shown less than this: flagged
|
|
hiddenMinVisits = 10
|
|
)
|
|
|
|
// Flags for pages.
|
|
const (
|
|
FlagNoSignups = "traffic, no sign-ups"
|
|
FlagHidden = "converts, hardly found"
|
|
)
|
|
|
|
// PathKey is the key Conv uses for a page's address: its path in lower
|
|
// case, with a trailing slash. Same as the tracking package's.
|
|
func PathKey(page string) string {
|
|
p := page
|
|
if u, err := url.Parse(page); err == nil && u.Path != "" {
|
|
p = u.Path
|
|
} else if err == nil && u.Host != "" {
|
|
p = "/"
|
|
}
|
|
p = strings.ToLower(strings.TrimSuffix(p, "index.html"))
|
|
if !strings.HasSuffix(p, "/") {
|
|
p += "/"
|
|
}
|
|
if !strings.HasPrefix(p, "/") {
|
|
p = "/" + p
|
|
}
|
|
return p
|
|
}
|
|
|
|
// HasConv says whether the scope knows what pages convert.
|
|
func (s Scope) HasConv() bool {
|
|
t := s.siteConv()
|
|
return t.Visitors > 0
|
|
}
|
|
|
|
func (s Scope) siteConv() PageConv {
|
|
var t PageConv
|
|
for _, c := range s.Conv {
|
|
t.Visitors += c.Visitors
|
|
t.Conversions += c.Conversions
|
|
}
|
|
return t
|
|
}
|
|
|
|
// Rate is the share of a page's visits that sign up: its own with enough
|
|
// visits, else the site's. own says which.
|
|
func (s Scope) Rate(page string) (rate float64, own bool) {
|
|
if c, ok := s.Conv[PathKey(page)]; ok && c.Visitors >= minVisitsForRate {
|
|
return float64(c.Conversions) / float64(c.Visitors), true
|
|
}
|
|
if t := s.siteConv(); t.Visitors > 0 {
|
|
return float64(t.Conversions) / float64(t.Visitors), false
|
|
}
|
|
return 0, false
|
|
}
|
|
|
|
// ctrAt is the share of searchers who click a result at a position, on
|
|
// page one: the usual curve, rounded. Past page one, 1%.
|
|
func ctrAt(pos float64) float64 {
|
|
curve := []float64{0.28, 0.15, 0.11, 0.08, 0.07, 0.05, 0.04, 0.03, 0.025, 0.02}
|
|
i := int(math.Round(pos)) - 1
|
|
switch {
|
|
case i < 0:
|
|
return curve[0]
|
|
case i < len(curve):
|
|
return curve[i]
|
|
}
|
|
return 0.01
|
|
}
|
|
|
|
// Expected is how many more sign-ups a page would bring in the period if
|
|
// it were clicked as often as a result at its position usually is: the
|
|
// clicks it misses, times its sign-up rate. 0 without conversion data.
|
|
func (s Scope) Expected(n Numbers, page string) float64 {
|
|
rate, _ := s.Rate(page)
|
|
if rate == 0 || n.Impressions == 0 {
|
|
return 0
|
|
}
|
|
missed := float64(n.Impressions)*ctrAt(n.Position) - float64(n.Clicks)
|
|
return math.Max(0, missed) * rate
|
|
}
|
|
|
|
// Flags marks the pages worth a look for what they convert: visited a lot
|
|
// with no sign-up, or converting well but hardly shown by Google (by
|
|
// impressions per path over the same period).
|
|
func (s Scope) Flags(impressions map[string]int) map[string]string {
|
|
out := map[string]string{}
|
|
var rates []float64
|
|
for _, c := range s.Conv {
|
|
if c.Visitors >= hiddenMinVisits && c.Conversions > 0 {
|
|
rates = append(rates, float64(c.Conversions)/float64(c.Visitors))
|
|
}
|
|
}
|
|
slices.Sort(rates)
|
|
top := math.Inf(1)
|
|
if len(rates) > 0 {
|
|
top = rates[len(rates)*3/4] // the top quarter starts here
|
|
}
|
|
for p, c := range s.Conv {
|
|
switch {
|
|
case c.Visitors >= noSignupsVisits && c.Conversions == 0:
|
|
out[p] = FlagNoSignups
|
|
case c.Visitors >= hiddenMinVisits && c.Conversions > 0 && float64(c.Conversions)/float64(c.Visitors) >= top && impressions[p] < hiddenMaxImpr:
|
|
out[p] = FlagHidden
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Impressions sums a period's impressions by path (a page on www. and off
|
|
// it counts once).
|
|
func Impressions(rows []PageRow) map[string]int {
|
|
out := map[string]int{}
|
|
for _, r := range rows {
|
|
out[PathKey(r.Page)] += r.Impressions
|
|
}
|
|
return out
|
|
}
|
|
|
|
// expectedWhy says what a page is worth in sign-ups, and on what rate.
|
|
func expectedWhy(sc Scope, page string, expected float64) string {
|
|
rate, own := sc.Rate(page)
|
|
whose := "the site's"
|
|
if own {
|
|
whose = "its own"
|
|
}
|
|
if rate == 0 {
|
|
return "Nobody signed up from the site in this period, so its worth in sign-ups is unknown."
|
|
}
|
|
return fmt.Sprintf("Clicked as usual for its position, it would bring %s in 90 days (at %s rate of %.1f%% of visits).",
|
|
moreSignups(expected), whose, 100*rate)
|
|
}
|
|
|
|
// moreSignups rounds an estimate for reading: "less than one more
|
|
// sign-up", "about 1 more sign-up", "about 12 more sign-ups".
|
|
func moreSignups(x float64) string {
|
|
if x < 0.5 {
|
|
return "less than one more sign-up"
|
|
}
|
|
n := int(math.Round(x))
|
|
return fmt.Sprintf("about %d more %s", n, plural(n, "sign-up", "sign-ups"))
|
|
}
|
|
|
|
// SearchesByPath are the searches Google showed each page for in the last
|
|
// 90 days of data (any host of the scope, by PathKey), with their
|
|
// impressions, most first.
|
|
func SearchesByPath(d *db.DB, sc Scope) (map[string][]QueryRow, error) {
|
|
from, to, ok := period(d, sc.Property, RanksDays)
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
cond, args := sc.where()
|
|
rows, err := d.Query(`SELECT page, query, sum(clicks), sum(impressions), sum(pos_sum) FROM gsc_query_days
|
|
WHERE `+cond+` AND date BETWEEN ? AND ? GROUP BY page, query`, append(args, from, to)...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
byPath := map[string]map[string]*QueryRow{}
|
|
for rows.Next() {
|
|
var page string
|
|
var q QueryRow
|
|
var ps float64
|
|
if err := rows.Scan(&page, &q.Query, &q.Clicks, &q.Impressions, &ps); err != nil {
|
|
return nil, err
|
|
}
|
|
k := PathKey(page)
|
|
if byPath[k] == nil {
|
|
byPath[k] = map[string]*QueryRow{}
|
|
}
|
|
if have := byPath[k][q.Query]; have != nil {
|
|
have.Clicks += q.Clicks
|
|
have.Impressions += q.Impressions
|
|
continue
|
|
}
|
|
q.Position = position(ps, q.Impressions)
|
|
byPath[k][q.Query] = &q
|
|
}
|
|
out := map[string][]QueryRow{}
|
|
for k, qs := range byPath {
|
|
for _, q := range qs {
|
|
out[k] = append(out[k], *q)
|
|
}
|
|
slices.SortFunc(out[k], func(a, b QueryRow) int {
|
|
if a.Impressions != b.Impressions {
|
|
return b.Impressions - a.Impressions
|
|
}
|
|
return strings.Compare(a.Query, b.Query)
|
|
})
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// PageSearches are a page's searches, most impressions first.
|
|
func PageSearches(d *db.DB, sc Scope, path string, n int) ([]string, error) {
|
|
all, err := SearchesByPath(d, sc)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var out []string
|
|
for i, q := range all[PathKey(path)] {
|
|
if i == n {
|
|
break
|
|
}
|
|
out = append(out, q.Query)
|
|
}
|
|
return out, nil
|
|
}
|