Phase 3 part 3: the audit, ranked by traffic, with AI visibility
A polite crawl of each website (Search Console's busiest pages, the sitemap, then links; up to 300 pages; robots.txt honoured) checks titles and descriptions (characters and pixels), headings, canonical, noindex, status and redirects, broken links (outside ones twice), alt text, Open Graph, JSON-LD, text without JavaScript, and whether the title says what the page's main search asks. Issues are ranked by severity, then the page's clicks and impressions. AI visibility: AI crawlers in robots.txt, llms.txt, structured data, Organization data. Optional PageSpeed checks. Weekly in the background, or Audit now. Schema 14. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+160
-1
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -112,6 +113,8 @@ func (s *Server) searchSettingsPage(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *Server) renderSearchSettings(w http.ResponseWriter, r *http.Request, errMsg string) {
|
||||
data := map[string]any{
|
||||
"Fields": googleFields,
|
||||
"Speed": pageSpeedKey,
|
||||
"SpeedSet": s.Secrets.Has(secrets.PageSpeedAPIKey),
|
||||
"Set": s.secretState(secrets.GoogleClientID, secrets.GoogleClientSecret, secrets.GoogleRefreshToken),
|
||||
"Redirect": s.callbackURL("google"),
|
||||
"Sites": s.seoSites(),
|
||||
@@ -136,7 +139,7 @@ func (s *Server) searchSettingsSave(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
if err := s.saveSecrets(r, googleFields); err != nil {
|
||||
if err := s.saveSecrets(r, append(googleFields, pageSpeedKey)); err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
@@ -220,9 +223,13 @@ func (s *Server) syncSearchConsole(ctx context.Context) error {
|
||||
|
||||
// RunSEO syncs Search Console twice a day.
|
||||
func (s *Server) RunSEO(ctx context.Context) {
|
||||
if err := seo.RecoverAudits(s.DB); err != nil {
|
||||
s.Log.Error("seo: recovering audits", "err", err)
|
||||
}
|
||||
tick := time.NewTicker(30 * time.Minute)
|
||||
defer tick.Stop()
|
||||
for {
|
||||
s.weeklyAudits(time.Now())
|
||||
if s.gsc() != nil {
|
||||
due := false
|
||||
for _, site := range s.seoSites() {
|
||||
@@ -418,3 +425,155 @@ func (s *Server) weeklyReports(ctx context.Context, now time.Time) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- The audit ---
|
||||
|
||||
var pageSpeedKey = secretField{"pagespeed", secrets.PageSpeedAPIKey, "PageSpeed Insights API key (optional)",
|
||||
"Free. In Google Cloud, enable the PageSpeed Insights API and create an API key under Credentials. Without one, Google allows only a few speed checks.", googleCreds}
|
||||
|
||||
// auditing marks the sites being audited now.
|
||||
var auditing sync.Map
|
||||
|
||||
// auditSites are every website in the profile (an audit doesn't need
|
||||
// Search Console; with it, issues are ranked by traffic).
|
||||
func (s *Server) auditSites(r *http.Request) ([]seoSite, *seoSite) {
|
||||
sites := s.seoSites()
|
||||
if i, err := strconv.Atoi(r.URL.Query().Get("site")); err == nil {
|
||||
for j := range sites {
|
||||
if sites[j].Index == i {
|
||||
return sites, &sites[j]
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(sites) > 0 {
|
||||
return sites, &sites[0]
|
||||
}
|
||||
return sites, nil
|
||||
}
|
||||
|
||||
func (s *Server) seoAuditPage(w http.ResponseWriter, r *http.Request) {
|
||||
sites, cur := s.auditSites(r)
|
||||
data := map[string]any{"Sites": sites, "Current": cur, "Path": "/seo/audit", "Connected": true,
|
||||
"MaxPages": seo.MaxAuditPages, "Severity": r.URL.Query().Get("severity")}
|
||||
if cur != nil {
|
||||
a, err := seo.LatestAudit(s.DB, cur.URL)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
data["Audit"] = a
|
||||
if a != nil && a.Status == "done" {
|
||||
sev := r.URL.Query().Get("severity")
|
||||
var shown []seo.Issue
|
||||
for _, is := range a.Issues {
|
||||
if sev == "" || is.Severity == sev {
|
||||
shown = append(shown, is)
|
||||
}
|
||||
}
|
||||
if len(shown) > 300 {
|
||||
data["More"] = len(shown) - 300
|
||||
shown = shown[:300]
|
||||
}
|
||||
data["Shown"] = shown
|
||||
}
|
||||
}
|
||||
s.render(w, r, "seo-audit", page{Title: "Audit", Nav: "seo/audit", Data: data})
|
||||
}
|
||||
|
||||
// seoAuditRun starts an audit in the background.
|
||||
func (s *Server) seoAuditRun(w http.ResponseWriter, r *http.Request) {
|
||||
_, cur := s.auditSites(r)
|
||||
if cur == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if err := r.ParseForm(); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
s.startAudit(*cur, r.PostFormValue("speed") == "on")
|
||||
redirect(w, r, "/seo/audit?site="+strconv.Itoa(cur.Index), "auditing")
|
||||
}
|
||||
|
||||
// startAudit runs one in the background, unless one is running for the
|
||||
// site.
|
||||
func (s *Server) startAudit(site seoSite, speed bool) {
|
||||
if _, busy := auditing.LoadOrStore(site.URL, true); busy {
|
||||
return
|
||||
}
|
||||
id, err := seo.StartAudit(s.DB, site.URL, time.Now())
|
||||
if err != nil {
|
||||
auditing.Delete(site.URL)
|
||||
s.Log.Error("seo: starting the audit", "err", err)
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
defer auditing.Delete(site.URL)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 45*time.Minute)
|
||||
defer cancel()
|
||||
a, err := seo.RunAudit(ctx, s.DB, seo.NewFetcher("Supermarket/"+s.Version+" (SEO audit)"), site.URL, site.Property)
|
||||
if err == nil && speed {
|
||||
for _, p := range busiest(a, 5) {
|
||||
a.Speed = append(a.Speed, seo.CheckSpeed(ctx, p, s.Secrets.Get(secrets.PageSpeedAPIKey)))
|
||||
}
|
||||
}
|
||||
if ferr := seo.FinishAudit(s.DB, id, a, err, time.Now()); ferr != nil {
|
||||
s.Log.Error("seo: saving the audit", "err", ferr)
|
||||
}
|
||||
if err != nil {
|
||||
s.Log.Warn("seo: audit failed", "site", site.URL, "err", err)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// busiest are an audit's pages with the most impressions (the home page
|
||||
// first when there's no traffic data).
|
||||
func busiest(a *seo.Audit, n int) []string {
|
||||
traffic := map[string]int{}
|
||||
for _, is := range a.Issues {
|
||||
traffic[is.Page] = is.Traffic.Impressions
|
||||
}
|
||||
var pages []string
|
||||
for _, p := range a.Pages {
|
||||
if p.Status == 200 {
|
||||
pages = append(pages, p.URL)
|
||||
}
|
||||
}
|
||||
sort.SliceStable(pages, func(i, j int) bool { return traffic[pages[i]] > traffic[pages[j]] })
|
||||
if len(pages) > n {
|
||||
pages = pages[:n]
|
||||
}
|
||||
return pages
|
||||
}
|
||||
|
||||
// seoAuditStatus is polled while an audit runs; it reloads the page when
|
||||
// the audit is done.
|
||||
func (s *Server) seoAuditStatus(w http.ResponseWriter, r *http.Request) {
|
||||
_, cur := s.auditSites(r)
|
||||
if cur == nil {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
a, err := seo.LatestAudit(s.DB, cur.URL)
|
||||
if err != nil {
|
||||
s.serverError(w, err)
|
||||
return
|
||||
}
|
||||
if a == nil || a.Status != "running" {
|
||||
w.Header().Set("HX-Refresh", "true")
|
||||
}
|
||||
w.Write([]byte(""))
|
||||
}
|
||||
|
||||
// weeklyAudits audits each website once a week.
|
||||
func (s *Server) weeklyAudits(now time.Time) {
|
||||
for _, site := range s.seoSites() {
|
||||
if site.URL != "" && now.Sub(seo.LastDoneAudit(s.DB, site.URL)) > 7*24*time.Hour {
|
||||
if a, _ := seo.LatestAudit(s.DB, site.URL); a != nil && a.Status == "failed" && now.Sub(a.StartedAt) < 24*time.Hour {
|
||||
continue // don't retry a failing site more than daily
|
||||
}
|
||||
s.startAudit(site, false)
|
||||
return // one at a time
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user