Why the GSC API Changes Your Workflow
The GSC web interface is useful for exploration, but it has real limitations: 16 months of data maximum, 1,000 rows per export, no automation, and no integration with other data sources. The Search Analytics API removes all of these constraints.
With the API, you can: store unlimited historical data in your own database, pull 25,000 rows per request, schedule weekly reports automatically, combine GSC data with GA4 for full funnel analysis, and set up alerts when traffic drops unexpectedly.
Python Setup
Install the required packages:
pip install google-auth google-auth-oauthlib google-api-python-client gspread pandas
You need OAuth 2.0 credentials from Google Cloud Console:
- Create a project at console.cloud.google.com
- Enable "Google Search Console API"
- Create OAuth 2.0 credentials (Desktop application)
- Download
client_secrets.json
Authentication and Basic Query
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import json
SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']
def authenticate():
flow = InstalledAppFlow.from_client_secrets_file('client_secrets.json', SCOPES)
credentials = flow.run_local_server(port=0)
return build('searchconsole', 'v1', credentials=credentials)
service = authenticate()
site_url = 'sc-domain:yourdomain.com' # or 'https://yourdomain.com/'
# Fetch top queries with impressions > 100
response = service.searchanalytics().query(
siteUrl=site_url,
body={
'startDate': '2026-01-01',
'endDate': '2026-05-01',
'dimensions': ['query'],
'dimensionFilterGroups': [{
'filters': [{
'dimension': 'query',
'operator': 'notContains',
'expression': 'brand-name' # exclude branded queries
}]
}],
'rowLimit': 25000,
'startRow': 0
}
).execute()
rows = response.get('rows', [])
for row in rows:
if row['impressions'] > 100:
print(f"Query: {row['keys'][0]}, Clicks: {row['clicks']}, Position: {row['position']:.1f}")
Automated Weekly Report to Google Sheets
import gspread
from google.oauth2.service_account import Credentials
from datetime import datetime, timedelta
def get_weekly_data(service, site_url):
end_date = datetime.now().strftime('%Y-%m-%d')
start_date = (datetime.now() - timedelta(days=7)).strftime('%Y-%m-%d')
response = service.searchanalytics().query(
siteUrl=site_url,
body={
'startDate': start_date,
'endDate': end_date,
'dimensions': ['page', 'query'],
'rowLimit': 5000
}
).execute()
return response.get('rows', [])
def write_to_sheets(rows, sheet_name='GSC Weekly Report'):
gc = gspread.service_account(filename='service_account.json')
sh = gc.open(sheet_name)
ws = sh.get_worksheet(0)
headers = ['Page', 'Query', 'Clicks', 'Impressions', 'CTR', 'Position', 'Date']
data = [headers]
today = datetime.now().strftime('%Y-%m-%d')
for row in rows:
data.append([
row['keys'][0],
row['keys'][1],
row['clicks'],
row['impressions'],
f"{row['ctr']:.2%}",
f"{row['position']:.1f}",
today
])
ws.append_rows(data[1:]) # skip header if sheet already has it
Traffic Drop Alert
def check_traffic_drop(service, site_url, threshold=0.20):
"""Alert if clicks dropped more than threshold% week over week."""
def get_clicks(start, end):
r = service.searchanalytics().query(
siteUrl=site_url,
body={'startDate': start, 'endDate': end, 'dimensions': ['date'], 'rowLimit': 7}
).execute()
return sum(row['clicks'] for row in r.get('rows', []))
this_week_end = datetime.now().strftime('%Y-%m-%d')
this_week_start = (datetime.now() - timedelta(days=7)).strftime('%Y-%m-%d')
last_week_end = (datetime.now() - timedelta(days=7)).strftime('%Y-%m-%d')
last_week_start = (datetime.now() - timedelta(days=14)).strftime('%Y-%m-%d')
this_week = get_clicks(this_week_start, this_week_end)
last_week = get_clicks(last_week_start, last_week_end)
if last_week > 0:
change = (this_week - last_week) / last_week
if change < -threshold:
send_alert(f"Traffic dropped {abs(change):.0%} week over week ({last_week} -> {this_week} clicks)")
Schedule this script with a cron job or GitHub Actions to run every Monday morning. When traffic drops more than 20% week over week, you get an immediate alert — rather than discovering the problem two weeks later during a manual review.
Links: GSC API reference | Python quickstart | google-auth library