Check your Google positions every day with Python and a SERP position API
One of the first rank trackers I built stored a single number per keyword. It worked until someone asked why a #1 ranking lost clicks. The page was #1 in the organic list, but it sat below an AI Overview and a People Also Ask block. The tracker was not broken. It was measuring the wrong number. Google's results page is not a list anymore. A page can be first in the organic list and fourth on the…
In the world of online search rankings, it's crucial to monitor not just where a website appears in the organic search results, but also where it appears on the entire search page. This is especially important when a site ranks at the top of the organic results but is pushed down due to additional features like AI Overview, People Also Ask blocks, or video and local packs. To address this, one can utilize Python and a SERP position API to store two key metrics: rank and position.
The rank metric indicates the order of a result within the organic search list on a particular search page. On the other hand, the position metric represents the result's location on the whole search page, taking into account each module as a single block. For instance, a site could be ranked #1 in the organic list but have a position of #4 on the page if additional modules take up space.
To achieve this, a script needs to be built using Python 3.8 or later, and it requires just one dependency: requests. The API key should be stored as an environment variable (SERPBASE_API_KEY) to avoid hardcoding sensitive information. The script will make a POST request to the SERP position API, passing the keyword, domain, and optional parameters such as the country (gl) and language (hl).
Once the request is made, the script checks for an HTTP 200 status code, which signifies success. However, the real signal of success is a status code of 0 within the API response. If the status is not 0, the script returns a dictionary with None values for both rank and position, along with the error message from the API response.
If the status is 0, the script iterates through the organic search results, extracting the host from each item's link. If the host matches the specified domain or the domain is present at the end of the host, the script returns a dictionary containing the rank, position, URL, and None for the error field. This dictionary is then appended to a list of rows, which includes the current date, keyword, rank, position, URL, and error (if any).
Finally, the script prints the keyword along with its rank and position, and then sleeps for a second to avoid overloading the API server. The function returns the list of rows, which can be used to store the data in a CSV file or any other format for further analysis.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.