SEO: a domain property (sc-domain:) covers every subdomain, so the shop, Gitea and forum were counted as the website. What ranks, the morning check, the Monday report, keywords, change candidates and the audit's traffic now read only the website's own host (with or without www). The report keeps pages worth a proposal apart from "Waiting for your yes", explains only moves past the thresholds, and says "places up/down". Also: - Feed summaries drop the posts' scripts and styles. - Spend you log can't be negative. - Test and check buttons can't be clicked twice while they run. - Previews show the site's web fonts and icon sprites. - Backups are read without leaving -wal/-shm files; deleting one removes them, and leftovers are swept at start. - The Overview's setup flags a CRM that Hermes can't reach. - Import now says what came in; adding a prospect that exists says so; prospects can be deleted. - Signal tags and brief addresses keep accented letters (Ü → u). - Page errors start with a capital; clearer messages for bad uploads, CRM retry and cancel, keywords, and the finished audit. - The module you're in stays unfolded in the sidebar. - A picked keyword can be unpicked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
246 lines
6.6 KiB
Go
246 lines
6.6 KiB
Go
package seo
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"slices"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"supermarket/internal/db"
|
|
)
|
|
|
|
// Numbers are a page's, a query's or a site's numbers over a period.
|
|
type Numbers struct {
|
|
Clicks int
|
|
Impressions int
|
|
Position float64 // average, weighted by impressions; 0 without impressions
|
|
}
|
|
|
|
// CTR is the click rate, in percent.
|
|
func (n Numbers) CTR() float64 {
|
|
if n.Impressions == 0 {
|
|
return 0
|
|
}
|
|
return 100 * float64(n.Clicks) / float64(n.Impressions)
|
|
}
|
|
|
|
func position(posSum float64, impressions int) float64 {
|
|
if impressions == 0 {
|
|
return 0
|
|
}
|
|
return posSum / float64(impressions)
|
|
}
|
|
|
|
// PageRow is a page and its numbers.
|
|
type PageRow struct {
|
|
Page string
|
|
Numbers
|
|
}
|
|
|
|
// QueryRow is a query and its numbers.
|
|
type QueryRow struct {
|
|
Query string
|
|
Numbers
|
|
}
|
|
|
|
// Pick is a page to protect or to push, with the numbers that justify it.
|
|
type Pick struct {
|
|
Page string
|
|
Numbers Numbers
|
|
MainQuery string // the query with the most impressions for the page
|
|
MainShare float64 // that query's share of the page's impressions, in percent
|
|
MainPos float64 // the page's position for it
|
|
Why string
|
|
}
|
|
|
|
// Ranks is job 1: what already ranks, over a period.
|
|
type Ranks struct {
|
|
Site string
|
|
From, To string
|
|
Total Numbers
|
|
Anonymous float64 // share of clicks from queries Google doesn't name, in percent; -1 if unknown
|
|
Pages []PageRow
|
|
Unclicked []QueryRow // shown a lot, almost never clicked
|
|
Protect []Pick
|
|
Push []Pick
|
|
}
|
|
|
|
// RanksDays is the period job 1 looks at.
|
|
const RanksDays = 90
|
|
|
|
// Thresholds for "pushing" a page: on page one, shown enough, rarely clicked.
|
|
const (
|
|
pushMaxPosition = 10.5
|
|
pushMinImpr = 100
|
|
lowCTR = 1.0 // percent
|
|
)
|
|
|
|
// period is the last n days up to the latest stored day.
|
|
func period(d *db.DB, site string, days int) (from, to string, ok bool) {
|
|
to = Through(d, site)
|
|
if to == "" {
|
|
return "", "", false
|
|
}
|
|
t, err := time.Parse(dayFormat, to)
|
|
if err != nil {
|
|
return "", "", false
|
|
}
|
|
return t.AddDate(0, 0, -(days - 1)).Format(dayFormat), to, true
|
|
}
|
|
|
|
// WhatRanks works out job 1 for a property, over the last RanksDays days
|
|
// of final data. It returns nil if nothing is stored yet.
|
|
func WhatRanks(d *db.DB, sc Scope) (*Ranks, error) {
|
|
from, to, ok := period(d, sc.Property, RanksDays)
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
r := &Ranks{Site: sc.Property, From: from, To: to, Anonymous: -1}
|
|
var err error
|
|
if r.Total, err = siteNumbers(d, sc, from, to); err != nil {
|
|
return nil, err
|
|
}
|
|
cond, args := sc.where()
|
|
var named int
|
|
if err := d.QueryRow(`SELECT coalesce(sum(clicks), 0) FROM gsc_query_days WHERE `+cond+` AND date BETWEEN ? AND ?`,
|
|
append(args, from, to)...).Scan(&named); err != nil {
|
|
return nil, err
|
|
}
|
|
if r.Total.Clicks > 0 {
|
|
r.Anonymous = 100 * float64(r.Total.Clicks-min(named, r.Total.Clicks)) / float64(r.Total.Clicks)
|
|
}
|
|
|
|
pages, err := pageNumbers(d, sc, from, to)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for i, p := range pages {
|
|
if i == 10 {
|
|
break
|
|
}
|
|
r.Pages = append(r.Pages, p)
|
|
}
|
|
|
|
rows, err := d.Query(`SELECT query, sum(clicks), sum(impressions), sum(pos_sum) FROM gsc_query_days
|
|
WHERE `+cond+` AND date BETWEEN ? AND ? GROUP BY query
|
|
HAVING sum(impressions) > 0 AND 100.0 * sum(clicks) / sum(impressions) < ?
|
|
ORDER BY sum(impressions) DESC LIMIT 10`, append(args, from, to, lowCTR)...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for rows.Next() {
|
|
var q QueryRow
|
|
var ps float64
|
|
if err := rows.Scan(&q.Query, &q.Clicks, &q.Impressions, &ps); err != nil {
|
|
rows.Close()
|
|
return nil, err
|
|
}
|
|
q.Position = position(ps, q.Impressions)
|
|
r.Unclicked = append(r.Unclicked, q)
|
|
}
|
|
rows.Close()
|
|
|
|
// Protect: the pages that carry the clicks.
|
|
for _, p := range pages {
|
|
if len(r.Protect) == 3 || p.Clicks == 0 {
|
|
break
|
|
}
|
|
pick := Pick{Page: p.Page, Numbers: p.Numbers}
|
|
pick.Why = fmt.Sprintf("%s clicks, %.1f%% of all clicks, at position %.1f.", Thousands(p.Clicks), share(p.Clicks, r.Total.Clicks), p.Position)
|
|
r.Protect = append(r.Protect, pick)
|
|
}
|
|
// Push: on page one, shown a lot, hardly clicked.
|
|
var cands []PageRow
|
|
for _, p := range pages {
|
|
if p.Position > 0 && p.Position <= pushMaxPosition && p.Impressions >= pushMinImpr && p.CTR() < lowCTR {
|
|
cands = append(cands, p)
|
|
}
|
|
}
|
|
slices.SortStableFunc(cands, func(a, b PageRow) int { return b.Impressions - a.Impressions })
|
|
for _, p := range cands {
|
|
if len(r.Push) == 3 {
|
|
break
|
|
}
|
|
pick := Pick{Page: p.Page, Numbers: p.Numbers}
|
|
if err := mainQuery(d, sc.Property, from, to, &pick); err != nil {
|
|
return nil, err
|
|
}
|
|
pick.Why = fmt.Sprintf("Shown %s times at position %.1f, %s %s.", Thousands(p.Impressions), p.Position, Thousands(p.Clicks), plural(p.Clicks, "click", "clicks"))
|
|
if pick.MainQuery != "" {
|
|
pick.Why += fmt.Sprintf(" %.0f%% of its impressions come from “%s”, where it's at %.1f.", pick.MainShare, pick.MainQuery, pick.MainPos)
|
|
}
|
|
r.Push = append(r.Push, pick)
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
// pageNumbers are every page's numbers over a period, most clicks first.
|
|
func pageNumbers(d *db.DB, sc Scope, from, to string) ([]PageRow, error) {
|
|
cond, args := sc.where()
|
|
rows, err := d.Query(`SELECT page, sum(clicks), sum(impressions), sum(pos_sum) FROM gsc_page_days
|
|
WHERE `+cond+` AND date BETWEEN ? AND ? GROUP BY page ORDER BY sum(clicks) DESC, sum(impressions) DESC`, append(args, from, to)...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []PageRow
|
|
for rows.Next() {
|
|
var p PageRow
|
|
var ps float64
|
|
if err := rows.Scan(&p.Page, &p.Clicks, &p.Impressions, &ps); err != nil {
|
|
return nil, err
|
|
}
|
|
p.Position = position(ps, p.Impressions)
|
|
out = append(out, p)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// mainQuery fills in the query a page is shown for most.
|
|
func mainQuery(d *db.DB, site, from, to string, p *Pick) error {
|
|
var q string
|
|
var impr int
|
|
var ps float64
|
|
err := d.QueryRow(`SELECT query, sum(impressions), sum(pos_sum) FROM gsc_query_days
|
|
WHERE site = ? AND page = ? AND date BETWEEN ? AND ? GROUP BY query ORDER BY sum(impressions) DESC LIMIT 1`,
|
|
site, p.Page, from, to).Scan(&q, &impr, &ps)
|
|
if err == sql.ErrNoRows {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p.MainQuery, p.MainShare, p.MainPos = q, share(impr, p.Numbers.Impressions), position(ps, impr)
|
|
return nil
|
|
}
|
|
|
|
func share(part, whole int) float64 {
|
|
if whole == 0 {
|
|
return 0
|
|
}
|
|
return 100 * float64(part) / float64(whole)
|
|
}
|
|
|
|
func plural(n int, one, many string) string {
|
|
if n == 1 {
|
|
return one
|
|
}
|
|
return many
|
|
}
|
|
|
|
// Thousands writes an integer with thousands separators: 63,201.
|
|
func Thousands(n int) string {
|
|
s := strconv.Itoa(n)
|
|
neg := strings.HasPrefix(s, "-")
|
|
s = strings.TrimPrefix(s, "-")
|
|
for i := len(s) - 3; i > 0; i -= 3 {
|
|
s = s[:i] + "," + s[i:]
|
|
}
|
|
if neg {
|
|
return "-" + s
|
|
}
|
|
return s
|
|
}
|