gsc

package
v0.9.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package gsc integrates Google Search Console: the search queries that bring people to your site, next to what they did after arriving — the one report neither GA nor the privacy tools unify well. BYO Google OAuth client (two env vars), a one-command loopback auth, a small daily poller, and the same three surfaces as everything else: dashboard card, MCP tool, verdict material.

Stdlib only, like the rest of the binary. Google endpoints are package vars so tests run against a fake server.

Index

Constants

This section is empty.

Variables

View Source
var (
	AuthEndpoint  = "https://accounts.google.com/o/oauth2/v2/auth"
	TokenEndpoint = "https://oauth2.googleapis.com/token"
	APIEndpoint   = "https://searchconsole.googleapis.com/webmasters/v3"
)

Endpoints — overridable in tests.

Functions

func AuthURL

func AuthURL(c Creds, redirect string) string

AuthURL builds the consent URL for the loopback flow.

func Exchange

func Exchange(ctx context.Context, c Creds, code, redirect string) (string, error)

Exchange trades the auth code for a refresh token.

func ListSites

func ListSites(ctx context.Context, c Creds, refreshToken string) ([]string, error)

ListSites returns the GSC properties the grant can read.

Types

type Creds

type Creds struct{ ClientID, ClientSecret string }

Creds is the BYO OAuth client (SMOLANALYTICS_GSC_CLIENT_ID / _SECRET).

func CredsFromEnv

func CredsFromEnv() (Creds, bool)

type PageRow added in v0.7.2

type PageRow struct {
	Page        string  `json:"page"`
	Query       string  `json:"query"`
	Clicks      int     `json:"clicks"`
	Impressions int     `json:"impressions"`
	Position    float64 `json:"position"`
}

PageRow is one (page, query) pair's performance — the page-level cut that powers the money-pages report (quick wins, CTR problems, cannibalization).

func FetchPages added in v0.7.2

func FetchPages(ctx context.Context, c Creds, refreshToken, siteURL string, days int) ([]PageRow, error)

FetchPages pulls (page, query) pairs for the trailing `days` — the raw material for the money-pages report.

type Row

type Row struct {
	Query       string  `json:"query"`
	Clicks      int     `json:"clicks"`
	Impressions int     `json:"impressions"`
	CTRPct      float64 `json:"ctr_pct"`
	Position    float64 `json:"position"`
}

Row is one search query's performance.

func FetchQueries

func FetchQueries(ctx context.Context, c Creds, refreshToken, siteURL string, days int) ([]Row, error)

FetchQueries pulls top queries for the trailing `days`.

type Store

type Store struct {
	// contains filtered or unexported fields
}

func Open

func Open(p string) (*Store, error)

func (*Store) Connected

func (s *Store) Connected() bool

func (*Store) PageSnapshot added in v0.7.2

func (s *Store) PageSnapshot() (rows, prev []PageRow, fetchErr string)

PageSnapshot returns the page-level rows plus the last page-fetch error (empty when the last page fetch succeeded — or never ran).

func (*Store) Poll

func (s *Store) Poll(ctx context.Context, c Creds) error

Poll refreshes the cached rows if stale (12h). Safe to call on a timer. Queries land first; the page-level fetch is best-effort — a page failure never discards fresh query rows, it's recorded and surfaced in the report.

func (*Store) SetGrant

func (s *Store) SetGrant(refreshToken, siteURL string) error

func (*Store) SetPageFetchError added in v0.7.2

func (s *Store) SetPageFetchError(msg string) error

SetPageFetchError records a page-level fetch failure without touching the cached rows, so the report can say "pages are stale/missing because X".

func (*Store) SetPageRows added in v0.7.2

func (s *Store) SetPageRows(rows []PageRow) error

SetPageRows stores the latest page-level rows; the prior fetch becomes PrevPageRows (mirroring Rows/PrevRows) and any recorded fetch error clears.

func (*Store) SetRows

func (s *Store) SetRows(rows []Row) error

func (*Store) Snapshot

func (s *Store) Snapshot() (rows, prev []Row, site string, fetched time.Time)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL