Phase 3 part 1: Search Console and what already ranks
PLAN: Phase 3 rewritten around a weekly seven-job SEO loop (what ranks, next keywords, brief, page, approval, morning check, Monday report), with chatseo.app's ideas borrowed and rankings from Search Console only. Google OAuth (PKCE, offline, read-only webmasters scope) from Settings → Search engines, with each website's property. Search Console is synced into SQLite by day (site, page, and page+query; 16 months, then twice a day). SEO → What ranks shows the last 90 days: totals, the share of clicks from hidden queries, best pages, queries shown but not clicked, and the pages to protect and to push, each with its numbers. Schema 12. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,247 @@
|
||||
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, site string) (*Ranks, error) {
|
||||
from, to, ok := period(d, site, RanksDays)
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
r := &Ranks{Site: site, From: from, To: to, Anonymous: -1}
|
||||
var posSum float64
|
||||
if err := d.QueryRow(`SELECT coalesce(sum(clicks), 0), coalesce(sum(impressions), 0), coalesce(sum(pos_sum), 0)
|
||||
FROM gsc_site_days WHERE site = ? AND date BETWEEN ? AND ?`, site, from, to).
|
||||
Scan(&r.Total.Clicks, &r.Total.Impressions, &posSum); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.Total.Position = position(posSum, r.Total.Impressions)
|
||||
|
||||
var named int
|
||||
if err := d.QueryRow(`SELECT coalesce(sum(clicks), 0) FROM gsc_query_days WHERE site = ? AND date BETWEEN ? AND ?`,
|
||||
site, 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, site, 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 site = ? 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`, site, 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, site, 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, site, from, to string) ([]PageRow, error) {
|
||||
rows, err := d.Query(`SELECT page, sum(clicks), sum(impressions), sum(pos_sum) FROM gsc_page_days
|
||||
WHERE site = ? AND date BETWEEN ? AND ? GROUP BY page ORDER BY sum(clicks) DESC, sum(impressions) DESC`, site, 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
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
package seo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/api/gsc"
|
||||
"supermarket/internal/db"
|
||||
)
|
||||
|
||||
func openDB(t *testing.T) *db.DB {
|
||||
t.Helper()
|
||||
d, err := db.Open(filepath.Join(t.TempDir(), "app.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { d.Close() })
|
||||
return d
|
||||
}
|
||||
|
||||
var now = time.Date(2026, 11, 22, 8, 0, 0, 0, time.UTC)
|
||||
|
||||
// fakeGSC answers like Search Console for one property, with data through
|
||||
// 2026-11-19. Every day: the home page gets 10 clicks of 100 impressions at
|
||||
// position 2; /bulking is shown 200 times at position 7.6 for "whey price
|
||||
// comparison" and 20 times for "bulking whey", with no clicks; Google hides
|
||||
// queries worth 2 home clicks a day.
|
||||
func fakeGSC(t *testing.T, calls *[]gsc.Query) *httptest.Server {
|
||||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("Authorization") != "Bearer tok" {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
if r.URL.Path == "/sites" {
|
||||
json.NewEncoder(w).Encode(map[string]any{"siteEntry": []any{
|
||||
map[string]any{"siteUrl": "sc-domain:whey.example", "permissionLevel": "siteOwner"},
|
||||
map[string]any{"siteUrl": "https://other.example/", "permissionLevel": "siteUnverifiedUser"},
|
||||
}})
|
||||
return
|
||||
}
|
||||
if !strings.HasSuffix(r.URL.Path, "/searchAnalytics/query") || !strings.Contains(r.URL.RawPath+r.URL.Path, "sc-domain") {
|
||||
t.Errorf("unexpected %s", r.URL)
|
||||
}
|
||||
var q gsc.Query
|
||||
json.NewDecoder(r.Body).Decode(&q)
|
||||
*calls = append(*calls, q)
|
||||
start, _ := time.Parse(dayFormat, q.StartDate)
|
||||
end, _ := time.Parse(dayFormat, q.EndDate)
|
||||
if last, _ := time.Parse(dayFormat, "2026-11-19"); end.After(last) {
|
||||
end = last
|
||||
}
|
||||
var rows []gsc.Row
|
||||
home, bulk := "https://whey.example/", "https://whey.example/bulking/"
|
||||
for d := start; !d.After(end); d = d.AddDate(0, 0, 1) {
|
||||
day := d.Format(dayFormat)
|
||||
switch strings.Join(q.Dimensions, ",") {
|
||||
case "date":
|
||||
rows = append(rows, gsc.Row{Keys: []string{day}, Clicks: 10, Impressions: 320, Position: 5.5})
|
||||
case "date,page":
|
||||
rows = append(rows, gsc.Row{Keys: []string{day, home}, Clicks: 10, Impressions: 100, Position: 2},
|
||||
gsc.Row{Keys: []string{day, bulk}, Clicks: 0, Impressions: 220, Position: 7.6})
|
||||
case "date,page,query":
|
||||
rows = append(rows, gsc.Row{Keys: []string{day, home, "whey"}, Clicks: 8, Impressions: 90, Position: 2},
|
||||
gsc.Row{Keys: []string{day, bulk, "whey price comparison"}, Clicks: 0, Impressions: 200, Position: 7.6},
|
||||
gsc.Row{Keys: []string{day, bulk, "bulking whey"}, Clicks: 0, Impressions: 20, Position: 12})
|
||||
}
|
||||
}
|
||||
json.NewEncoder(w).Encode(map[string]any{"rows": rows})
|
||||
}))
|
||||
}
|
||||
|
||||
func TestSyncAndWhatRanks(t *testing.T) {
|
||||
d := openDB(t)
|
||||
var calls []gsc.Query
|
||||
srv := fakeGSC(t, &calls)
|
||||
defer srv.Close()
|
||||
c := gsc.New(func(context.Context) (string, error) { return "tok", nil }).WithBase(srv.URL)
|
||||
|
||||
sites, err := c.Sites(context.Background())
|
||||
if err != nil || len(sites) != 1 || sites[0].URL != "sc-domain:whey.example" {
|
||||
t.Fatalf("sites: %+v %v", sites, err)
|
||||
}
|
||||
if r, _ := WhatRanks(d, "sc-domain:whey.example"); r != nil {
|
||||
t.Error("ranks before any data")
|
||||
}
|
||||
res, err := Sync(context.Background(), d, c, "sc-domain:whey.example", now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if res.Through != "2026-11-19" || res.From != "2025-07-30" || len(calls) != 3*17 {
|
||||
t.Fatalf("sync: %+v, %d calls", res, len(calls))
|
||||
}
|
||||
// The next sync reads only the last days again.
|
||||
calls = nil
|
||||
res, _ = Sync(context.Background(), d, c, "sc-domain:whey.example", now.AddDate(0, 0, 1))
|
||||
if res.From != "2026-11-15" || len(calls) != 3 || calls[0].DataState != "final" {
|
||||
t.Errorf("second sync: %+v %+v", res, calls)
|
||||
}
|
||||
if last, _ := LastSync(d, "sc-domain:whey.example"); last == nil || last.Through != "2026-11-19" {
|
||||
t.Errorf("last sync: %+v", last)
|
||||
}
|
||||
|
||||
r, err := WhatRanks(d, "sc-domain:whey.example")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if r.From != "2026-08-22" || r.To != "2026-11-19" || r.Total.Clicks != 900 || r.Total.Impressions != 28800 || r.Total.Position != 5.5 {
|
||||
t.Errorf("totals: %+v", r)
|
||||
}
|
||||
if int(r.Anonymous+0.5) != 20 {
|
||||
t.Errorf("anonymous share: %v", r.Anonymous)
|
||||
}
|
||||
if len(r.Pages) != 2 || r.Pages[0].Page != "https://whey.example/" || r.Pages[0].CTR() != 10 {
|
||||
t.Errorf("pages: %+v", r.Pages)
|
||||
}
|
||||
if len(r.Unclicked) != 2 || r.Unclicked[0].Query != "whey price comparison" || r.Unclicked[0].Impressions != 18000 {
|
||||
t.Errorf("unclicked: %+v", r.Unclicked)
|
||||
}
|
||||
if len(r.Protect) != 1 || !strings.Contains(r.Protect[0].Why, "100.0% of all clicks") {
|
||||
t.Errorf("protect: %+v", r.Protect)
|
||||
}
|
||||
if len(r.Push) != 1 {
|
||||
t.Fatalf("push: %+v", r.Push)
|
||||
}
|
||||
p := r.Push[0]
|
||||
if p.Page != "https://whey.example/bulking/" || p.MainQuery != "whey price comparison" || int(p.MainShare+0.5) != 91 ||
|
||||
!strings.Contains(p.Why, "Shown 19,800 times at position 7.6, 0 clicks. 91% of its impressions come from “whey price comparison”, where it's at 7.6.") {
|
||||
t.Errorf("push: %+v", p)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
// Package seo runs the weekly SEO loop for the company's websites, from
|
||||
// Google Search Console's own numbers: what already ranks, the morning
|
||||
// check, the Monday report, the audit, and the changes the user approves.
|
||||
// Every number is computed here, from the data; the AI only writes words
|
||||
// around them.
|
||||
package seo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"supermarket/internal/api/gsc"
|
||||
"supermarket/internal/db"
|
||||
)
|
||||
|
||||
const dayFormat = "2006-01-02"
|
||||
|
||||
// historyDays is how far back the first sync reads: Search Console keeps
|
||||
// about 16 months.
|
||||
const historyDays = 480
|
||||
|
||||
// refetchDays are read again on every sync: the last days' data can still
|
||||
// change before it's final.
|
||||
const refetchDays = 4
|
||||
|
||||
// chunkDays is how many days one set of requests covers.
|
||||
const chunkDays = 30
|
||||
|
||||
func throughKey(site string) string { return "seo:gsc-through:" + site }
|
||||
|
||||
// SyncResult is what a sync read.
|
||||
type SyncResult struct {
|
||||
Site string `json:"site"`
|
||||
At time.Time `json:"at"`
|
||||
From string `json:"from"`
|
||||
Through string `json:"through"` // the latest day with final data
|
||||
Rows int `json:"rows"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
func lastKey(site string) string { return "seo:gsc-last:" + site }
|
||||
|
||||
// LastSync is a property's last sync, or nil.
|
||||
func LastSync(d *db.DB, site string) (*SyncResult, error) {
|
||||
var r SyncResult
|
||||
ok, err := d.GetJSON(lastKey(site), &r)
|
||||
if err != nil || !ok {
|
||||
return nil, err
|
||||
}
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
// Through is the latest day stored for a property, or "".
|
||||
func Through(d *db.DB, site string) string {
|
||||
var t string
|
||||
_, _ = d.GetJSON(throughKey(site), &t)
|
||||
return t
|
||||
}
|
||||
|
||||
// Sync reads a property's Search Console data, by day: the totals, each
|
||||
// page, and each page and query. The first time it reads 16 months; after
|
||||
// that, from a few days before the last final day.
|
||||
func Sync(ctx context.Context, d *db.DB, c *gsc.Client, site string, now time.Time) (*SyncResult, error) {
|
||||
res := &SyncResult{Site: site, At: now}
|
||||
err := sync(ctx, d, c, site, now, res)
|
||||
if err != nil {
|
||||
res.Error = err.Error()
|
||||
}
|
||||
if perr := d.PutJSON(lastKey(site), res); perr != nil && err == nil {
|
||||
err = perr
|
||||
}
|
||||
return res, err
|
||||
}
|
||||
|
||||
func sync(ctx context.Context, d *db.DB, c *gsc.Client, site string, now time.Time, res *SyncResult) error {
|
||||
today := now.UTC().Truncate(24 * time.Hour)
|
||||
from := today.AddDate(0, 0, -historyDays)
|
||||
if t := Through(d, site); t != "" {
|
||||
if last, err := time.Parse(dayFormat, t); err == nil {
|
||||
from = last.AddDate(0, 0, -refetchDays)
|
||||
}
|
||||
}
|
||||
res.From = from.Format(dayFormat)
|
||||
through := Through(d, site)
|
||||
for start := from; !start.After(today); start = start.AddDate(0, 0, chunkDays) {
|
||||
end := start.AddDate(0, 0, chunkDays-1)
|
||||
if end.After(today) {
|
||||
end = today
|
||||
}
|
||||
n, latest, err := syncRange(ctx, d, c, site, start.Format(dayFormat), end.Format(dayFormat))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res.Rows += n
|
||||
if latest > through {
|
||||
through = latest
|
||||
}
|
||||
}
|
||||
res.Through = through
|
||||
if through == "" {
|
||||
return nil
|
||||
}
|
||||
return d.PutJSON(throughKey(site), through)
|
||||
}
|
||||
|
||||
// syncRange replaces the stored days in [start, end] with Google's. It
|
||||
// returns the rows stored and the latest day that had data.
|
||||
func syncRange(ctx context.Context, d *db.DB, c *gsc.Client, site, start, end string) (int, string, error) {
|
||||
q := func(dims ...string) ([]gsc.Row, error) {
|
||||
return c.Analytics(ctx, site, gsc.Query{StartDate: start, EndDate: end, Dimensions: dims, Type: "web", DataState: "final"})
|
||||
}
|
||||
days, err := q("date")
|
||||
if err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
pages, err := q("date", "page")
|
||||
if err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
queries, err := q("date", "page", "query")
|
||||
if err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
tx, err := d.Begin()
|
||||
if err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
for _, table := range []string{"gsc_site_days", "gsc_page_days", "gsc_query_days"} {
|
||||
if _, err := tx.Exec(`DELETE FROM `+table+` WHERE site = ? AND date BETWEEN ? AND ?`, site, start, end); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
}
|
||||
latest := ""
|
||||
n := 0
|
||||
for _, r := range days {
|
||||
if len(r.Keys) < 1 {
|
||||
continue
|
||||
}
|
||||
if r.Keys[0] > latest {
|
||||
latest = r.Keys[0]
|
||||
}
|
||||
if _, err := tx.Exec(`INSERT INTO gsc_site_days VALUES (?, ?, ?, ?, ?)`,
|
||||
site, r.Keys[0], int(r.Clicks), int(r.Impressions), r.Position*r.Impressions); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
n++
|
||||
}
|
||||
for _, r := range pages {
|
||||
if len(r.Keys) < 2 {
|
||||
continue
|
||||
}
|
||||
if _, err := tx.Exec(`INSERT INTO gsc_page_days VALUES (?, ?, ?, ?, ?, ?)`,
|
||||
site, r.Keys[0], r.Keys[1], int(r.Clicks), int(r.Impressions), r.Position*r.Impressions); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
n++
|
||||
}
|
||||
for _, r := range queries {
|
||||
if len(r.Keys) < 3 {
|
||||
continue
|
||||
}
|
||||
if _, err := tx.Exec(`INSERT INTO gsc_query_days VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT (site, date, page, query) DO NOTHING`,
|
||||
site, r.Keys[0], r.Keys[1], r.Keys[2], int(r.Clicks), int(r.Impressions), r.Position*r.Impressions); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
n++
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
return 0, "", fmt.Errorf("saving Search Console data: %w", err)
|
||||
}
|
||||
return n, latest, nil
|
||||
}
|
||||
Reference in New Issue
Block a user