gsc

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 12 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gsc is a small client for the Google Search Console Search Analytics API.

Index

Constants

View Source
const (
	ScopeReadOnly = "https://www.googleapis.com/auth/webmasters.readonly"
	ScopeFull     = "https://www.googleapis.com/auth/webmasters"
)

OAuth scopes supported by Search Console.

View Source
const DefaultBaseURL = "https://www.googleapis.com/webmasters/v3"

DefaultBaseURL is the Search Console webmasters v3 endpoint.

View Source
const DefaultInspectionBaseURL = "https://searchconsole.googleapis.com/v1"

DefaultInspectionBaseURL is the URL Inspection v1 endpoint.

View Source
const MaxRowLimit = 25000

MaxRowLimit is the largest rowLimit the API accepts.

Variables

This section is empty.

Functions

func DefaultRange

func DefaultRange(now time.Time) (start, end string)

DefaultRange returns the last 28 days ending 3 days ago (finalized data lag), computed in Pacific Time like the API.

Types

type APIError

type APIError struct {
	HTTPStatus int
	Code       int    `json:"code"`
	Message    string `json:"message"`
	Status     string `json:"status"`
}

APIError is a Google API error response.

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) IsQuotaExceeded

func (e *APIError) IsQuotaExceeded() bool

IsQuotaExceeded reports quota and rate-limit errors.

type AggregationType

type AggregationType string

AggregationType controls how rows are aggregated.

const (
	AggAuto                AggregationType = "auto"
	AggByPage              AggregationType = "byPage"
	AggByProperty          AggregationType = "byProperty"
	AggByNewsShowcasePanel AggregationType = "byNewsShowcasePanel"
)

type AmpInspectionResult

type AmpInspectionResult struct {
	Issues                []AmpIssue `json:"issues,omitempty"`
	Verdict               string     `json:"verdict,omitempty"`
	AmpURL                string     `json:"ampUrl,omitempty"`
	RobotsTxtState        string     `json:"robotsTxtState,omitempty"`
	IndexingState         string     `json:"indexingState,omitempty"`
	AmpIndexStatusVerdict string     `json:"ampIndexStatusVerdict,omitempty"`
	LastCrawlTime         string     `json:"lastCrawlTime,omitempty"`
	PageFetchState        string     `json:"pageFetchState,omitempty"`
}

AmpInspectionResult mirrors the Search Console API resource. Enum values are API strings.

type AmpIssue

type AmpIssue struct {
	IssueMessage string `json:"issueMessage,omitempty"`
	Severity     string `json:"severity,omitempty"`
}

AmpIssue mirrors the Search Console API resource. Enum values are API strings.

type Client

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

Client calls the Search Console API. The HTTP client must add auth (for example an oauth2.Client).

func New

func New(httpClient *http.Client, opts ...Option) *Client

New returns a Client using an authenticated HTTP client.

func (*Client) AddSite

func (c *Client) AddSite(ctx context.Context, site string) error

AddSite adds a property to the user's sites.

func (*Client) Dates

func (c *Client) Dates(ctx context.Context, siteURL, start, end string, t SearchType) ([]Row, error)

Dates returns the days with data in the range, the recommended first check.

func (*Client) DeleteSite

func (c *Client) DeleteSite(ctx context.Context, site string) error

DeleteSite removes a property from the user's sites.

func (*Client) DeleteSitemap

func (c *Client) DeleteSitemap(ctx context.Context, site, feed string) error

DeleteSitemap deletes a sitemap submission.

func (*Client) GetSite

func (c *Client) GetSite(ctx context.Context, site string) (*Site, error)

GetSite retrieves a property.

func (*Client) GetSitemap

func (c *Client) GetSitemap(ctx context.Context, site, feed string) (*Sitemap, error)

GetSitemap retrieves a sitemap.

func (*Client) InspectURL

func (c *Client) InspectURL(ctx context.Context, req InspectRequest) (*InspectionResult, error)

InspectURL inspects Google's indexed version of a URL (not a live test).

func (*Client) ListSitemaps

func (c *Client) ListSitemaps(ctx context.Context, site, sitemapIndex string) ([]Sitemap, error)

ListSitemaps lists submitted sitemaps, optionally within an index.

func (*Client) Query

func (c *Client) Query(ctx context.Context, siteURL string, req QueryRequest) (*QueryResponse, error)

Query runs one searchAnalytics.query call.

func (*Client) QueryAll

func (c *Client) QueryAll(ctx context.Context, siteURL string, req QueryRequest, maxRows int) (*QueryResponse, error)

QueryAll pages through results with startRow in MaxRowLimit batches. It stops on a short or empty page, or once maxRows rows are collected (maxRows <= 0 means no cap). Metadata comes from the first page.

func (*Client) Sites

func (c *Client) Sites(ctx context.Context) ([]Site, error)

Sites lists the properties visible to the user.

func (*Client) SubmitSitemap

func (c *Client) SubmitSitemap(ctx context.Context, site, feed string) error

SubmitSitemap submits a sitemap.

type DataState

type DataState string

DataState selects finalized, fresh, or hourly data.

const (
	DataFinal     DataState = "final"
	DataAll       DataState = "all"
	DataHourlyAll DataState = "hourly_all"
)

type DetectedItems

type DetectedItems struct {
	Items          []Item `json:"items,omitempty"`
	RichResultType string `json:"richResultType,omitempty"`
}

DetectedItems mirrors the Search Console API resource. Enum values are API strings.

type Dimension

type Dimension string

Dimension groups or filters Search Analytics rows.

const (
	DimQuery            Dimension = "query"
	DimPage             Dimension = "page"
	DimCountry          Dimension = "country"
	DimDevice           Dimension = "device"
	DimSearchAppearance Dimension = "searchAppearance"
	DimDate             Dimension = "date"
	DimHour             Dimension = "hour"
)

type Filter

type Filter struct {
	Dimension  Dimension `json:"dimension"`
	Operator   Operator  `json:"operator,omitempty"`
	Expression string    `json:"expression"`
}

Filter matches one dimension. Operator defaults to equals.

func ParseFilter

func ParseFilter(s string) (Filter, error)

ParseFilter parses "dim op expr" or "dim=expr". The expression may contain spaces, e.g. "query contains seo tools".

func (Filter) Validate

func (f Filter) Validate() error

Validate checks one filter.

type FilterGroup

type FilterGroup struct {
	GroupType string   `json:"groupType,omitempty"`
	Filters   []Filter `json:"filters"`
}

FilterGroup is a set of filters that must all match ("and").

func AndGroup

func AndGroup(filters ...Filter) []FilterGroup

AndGroup builds one "and" group, or nil when filters is empty.

type IndexStatusInspectionResult

type IndexStatusInspectionResult struct {
	Sitemap         []string `json:"sitemap,omitempty"`
	ReferringURLs   []string `json:"referringUrls,omitempty"`
	Verdict         string   `json:"verdict,omitempty"`
	CoverageState   string   `json:"coverageState,omitempty"`
	RobotsTxtState  string   `json:"robotsTxtState,omitempty"`
	IndexingState   string   `json:"indexingState,omitempty"`
	LastCrawlTime   string   `json:"lastCrawlTime,omitempty"`
	PageFetchState  string   `json:"pageFetchState,omitempty"`
	GoogleCanonical string   `json:"googleCanonical,omitempty"`
	UserCanonical   string   `json:"userCanonical,omitempty"`
	CrawledAs       string   `json:"crawledAs,omitempty"`
}

IndexStatusInspectionResult mirrors the Search Console API resource. Enum values are API strings.

type InspectRequest

type InspectRequest struct {
	InspectionURL string `json:"inspectionUrl,omitempty"`
	SiteURL       string `json:"siteUrl,omitempty"`
	LanguageCode  string `json:"languageCode,omitempty"`
}

InspectRequest mirrors the Search Console API resource. Enum values are API strings.

type InspectionResult

type InspectionResult struct {
	InspectionResultLink  string                           `json:"inspectionResultLink,omitempty"`
	IndexStatusResult     *IndexStatusInspectionResult     `json:"indexStatusResult,omitempty"`
	AmpResult             *AmpInspectionResult             `json:"ampResult,omitempty"`
	MobileUsabilityResult *MobileUsabilityInspectionResult `json:"mobileUsabilityResult,omitempty"`
	RichResultsResult     *RichResultsInspectionResult     `json:"richResultsResult,omitempty"`
}

InspectionResult mirrors the Search Console API resource. Enum values are API strings.

type Item

type Item struct {
	Issues []RichResultsIssue `json:"issues,omitempty"`
	Name   string             `json:"name,omitempty"`
}

Item mirrors the Search Console API resource. Enum values are API strings.

type Metadata

type Metadata struct {
	FirstIncompleteDate string `json:"first_incomplete_date,omitempty"`
	FirstIncompleteHour string `json:"first_incomplete_hour,omitempty"`
}

Metadata marks where fresh data is still incomplete (America/Los_Angeles).

type MobileUsabilityInspectionResult

type MobileUsabilityInspectionResult struct {
	Issues  []MobileUsabilityIssue `json:"issues,omitempty"`
	Verdict string                 `json:"verdict,omitempty"`
}

MobileUsabilityInspectionResult mirrors the Search Console API resource. Enum values are API strings.

type MobileUsabilityIssue

type MobileUsabilityIssue struct {
	IssueType string `json:"issueType,omitempty"`
	Severity  string `json:"severity,omitempty"`
	Message   string `json:"message,omitempty"`
}

MobileUsabilityIssue mirrors the Search Console API resource. Enum values are API strings.

type Operator

type Operator string

Operator compares a filter expression with a dimension value.

const (
	OpEquals         Operator = "equals"
	OpNotEquals      Operator = "notEquals"
	OpContains       Operator = "contains"
	OpNotContains    Operator = "notContains"
	OpIncludingRegex Operator = "includingRegex"
	OpExcludingRegex Operator = "excludingRegex"
)

type Option

type Option func(*Client)

Option configures a Client.

func WithBaseURL

func WithBaseURL(u string) Option

WithBaseURL overrides the API base URL (useful for tests).

func WithInspectionBaseURL

func WithInspectionBaseURL(u string) Option

WithInspectionBaseURL overrides the URL Inspection endpoint.

type QueryRequest

type QueryRequest struct {
	StartDate             string          `json:"startDate"`
	EndDate               string          `json:"endDate"`
	Dimensions            []Dimension     `json:"dimensions,omitempty"`
	Type                  SearchType      `json:"type,omitempty"`
	DimensionFilterGroups []FilterGroup   `json:"dimensionFilterGroups,omitempty"`
	AggregationType       AggregationType `json:"aggregationType,omitempty"`
	RowLimit              int             `json:"rowLimit,omitempty"`
	StartRow              int             `json:"startRow,omitempty"`
	DataState             DataState       `json:"dataState,omitempty"`
}

QueryRequest is the searchAnalytics.query request body. Dates use YYYY-MM-DD in Pacific Time and are inclusive.

func (QueryRequest) Validate

func (r QueryRequest) Validate() error

Validate checks the request before it is sent.

type QueryResponse

type QueryResponse struct {
	Rows                    []Row     `json:"rows"`
	ResponseAggregationType string    `json:"responseAggregationType,omitempty"`
	Metadata                *Metadata `json:"metadata,omitempty"`
}

QueryResponse is the searchAnalytics.query response body.

type RichResultsInspectionResult

type RichResultsInspectionResult struct {
	DetectedItems []DetectedItems `json:"detectedItems,omitempty"`
	Verdict       string          `json:"verdict,omitempty"`
}

RichResultsInspectionResult mirrors the Search Console API resource. Enum values are API strings.

type RichResultsIssue

type RichResultsIssue struct {
	IssueMessage string `json:"issueMessage,omitempty"`
	Severity     string `json:"severity,omitempty"`
}

RichResultsIssue mirrors the Search Console API resource. Enum values are API strings.

type Row

type Row struct {
	Keys        []string `json:"keys"`
	Clicks      float64  `json:"clicks"`
	Impressions float64  `json:"impressions"`
	CTR         float64  `json:"ctr"`
	Position    float64  `json:"position"`
}

Row is one result row. Keys follow the requested dimension order.

type SearchType

type SearchType string

SearchType selects the Google Search surface.

const (
	TypeWeb        SearchType = "web"
	TypeImage      SearchType = "image"
	TypeVideo      SearchType = "video"
	TypeNews       SearchType = "news"
	TypeDiscover   SearchType = "discover"
	TypeGoogleNews SearchType = "googleNews"
)

type Site

type Site struct {
	SiteURL         string `json:"siteUrl"`
	PermissionLevel string `json:"permissionLevel"`
}

Site is a Search Console property visible to the user.

type Sitemap

type Sitemap struct {
	Path            string           `json:"path,omitempty"`
	LastSubmitted   string           `json:"lastSubmitted,omitempty"`
	IsPending       bool             `json:"isPending"`
	IsSitemapsIndex bool             `json:"isSitemapsIndex"`
	Type            string           `json:"type,omitempty"`
	LastDownloaded  string           `json:"lastDownloaded,omitempty"`
	Warnings        string           `json:"warnings,omitempty"`
	Errors          string           `json:"errors,omitempty"`
	Contents        []SitemapContent `json:"contents,omitempty"`
}

Sitemap mirrors the Search Console API resource. Counters use plain strings to preserve the API int64 JSON representation.

type SitemapContent

type SitemapContent struct {
	Type      string `json:"type,omitempty"`
	Submitted string `json:"submitted,omitempty"`
	Indexed   string `json:"indexed,omitempty"`
}

SitemapContent mirrors the Search Console API resource. Enum values are API strings.

Source Files

  • client.go
  • endpoints.go
  • filter.go
  • resources.go
  • types.go
  • validate.go

Jump to

Keyboard shortcuts

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