Appends last week's top search queries, clicks, impressions and average position to a Google Sheet every Monday.
Uses
Google Sheets
Weekly search performance export to Sheets
Every Monday at 08:00, last week's search performance for your site is pulled from your search analytics endpoint and appended to a Google Sheet, one row per query, so the trend builds up week after week without anyone opening a console.
Each row records the week it covers, the query, clicks, impressions, CTR and average position, written as real numbers and dates so you can chart any column straight from the sheet. It reads the seven full days ending three days before the run, because search consoles finalise a day's figures on a lag. The first run creates the tab and writes the header; every run after that only appends, so history is never overwritten. A week with no data stops cleanly and leaves the sheet alone, and a result with no query text is logged and skipped rather than ending the run.
You need an access token for your search analytics endpoint and a Google Sheet to hold the history.
searchDataUrl to blank.Set searchDataUrl to blank.accessToken to blank.Set accessToken to blank.spreadsheetUrl to blank.Set spreadsheetUrl to blank.sheetTab to Search performance.Set sheetTab to Search performance.maxQueries to 50.Set maxQueries to 50.days split by T and save it as weekEnd.Get the first item of today's date shifted by -3 days split by T and save it as weekEnd.days split by T and save it as weekStart.Get the first item of today's date shifted by -9 days split by T and save it as weekStart.searchDataUrl and save it as report.Send data to searchDataUrl and save it as report.queryRows to report.body.rows.Set queryRows to report.body.rows.rowsFound to 0.Set rowsFound to 0.noRowsErrorIf a step below fails, save the error as noRowsErrorqueryRows and save it as rowsFound.Count the items in queryRows and save it as rowsFound.The search analytics endpoint returned no rows for this week..Show in the output: The search analytics endpoint returned no rows for this week..rowsFound is 0If rowsFound is 0weekStart and weekEnd. Nothing was added to the sheet.Finish the run: No search data between weekStart and weekEnd. Nothing was added to the sheet.tabIsNew to false.Set tabIsNew to false.tabErrorIf a step below fails, save the error as tabErrorsheetTab in spreadsheetUrl.Create the tab sheetTab in spreadsheetUrl.tabIsNew to true.Change tabIsNew to true.tabIsNew is yesIf tabIsNew is yessheetTab in spreadsheetUrl.Add a row to sheetTab in spreadsheetUrl.rowsAdded to 0.Set rowsAdded to 0.searchRow in queryRows:For each searchRow in queryRows:queryText to blank.Set queryText to blank.queryErrorIf a step below fails, save the error as queryErrorsearchRow.keys and save it as queryText.Trim the first item of searchRow.keys and save it as queryText.Skipped a search result that carried no query text..Show in the output: Skipped a search result that carried no query text..queryText isn't emptyIf queryText isn't emptysheetTab in spreadsheetUrl.Add a row to sheetTab in spreadsheetUrl.rowsAdded.Add 1 to rowsAdded.rowsAdded query rows for weekStart to weekEnd to the '@sheetTab' tab.Show in the output: Added rowsAdded query rows for weekStart to weekEnd to the '@sheetTab' tab.