Local rank grid with Google Maps

Measure a business's Google Maps position across a grid of map points around its location, using location_coordinate and the standard queue.
Open as Markdown

A local rank grid runs the same Maps search from many points around a business and records its position at each one. This recipe builds a 7 × 7 grid, queues one Google Maps task per point, and prints a position map.

Note:

Beta: Google Maps results come from parsers that haven't been checked against live responses yet.

Cost: one task per point, $0.00054 each in the standard queue. A 7 × 7 grid is 49 tasks, $0.02646 per keyword. Maps tasks don't fall back to a live source, so a point that fails costs $0.

You need: Python 3.9+, requests, DATAFORGAIO_API_KEY, and the business's CID (from a Maps result's cid, or my_business_info).

rank_grid.py
import math
import os
import time
 
import requests
 
BASE = "https://api.dataforgaio.com/v1"
AUTH = ("api", os.environ["DATAFORGAIO_API_KEY"])
 
KEYWORD = "coffee shop"
TARGET_CID = "1234567890123456789"   # the business you track
CENTER = (30.2672, -97.7431)         # lat, lng
SIZE = 7                             # 7 x 7 points
SPACING_KM = 1.0
ZOOM = "15z"
DEPTH = 20
 
 
def grid(center, size, spacing_km):
    lat0, lng0 = center
    half = size // 2
    for row in range(size):
        for col in range(size):
            dy = (half - row) * spacing_km
            dx = (col - half) * spacing_km
            lat = lat0 + dy / 111.32
            lng = lng0 + dx / (111.32 * math.cos(math.radians(lat0)))
            yield row, col, round(lat, 6), round(lng, 6)
 
 
points = list(grid(CENTER, SIZE, SPACING_KM))
tasks = [{
    "keyword": KEYWORD,
    "location_coordinate": f"{lat},{lng},{ZOOM}",
    "language_code": "en",
    "depth": DEPTH,
    "tag": f"{row},{col}",
} for row, col, lat, lng in points]
 
body = requests.post(f"{BASE}/serp/google/maps/task_post", auth=AUTH, json=tasks, timeout=60).json()
if body["status_code"] != 20000:
    raise SystemExit(f'{body["status_code"]}: {body["status_message"]}')
pending = {t["id"]: t["data"]["tag"] for t in body["tasks"] if t["status_code"] == 20100}
 
positions = {}
deadline = time.time() + 1800
while pending and time.time() < deadline:
    for task_id in list(pending):
        task = requests.get(f"{BASE}/serp/google/maps/task_get/advanced/{task_id}",
                            auth=AUTH, timeout=60).json()["tasks"][0]
        if task["status_code"] in (40601, 40602):
            continue  # not finished yet
        tag = pending.pop(task_id)
        items = (task.get("result") or [{}])[0].get("items") or []
        hit = next((i for i in items if i.get("cid") == TARGET_CID), None)
        positions[tag] = hit["rank_absolute"] if hit else None
    if pending:
        time.sleep(30)  # 49 reads per 30 s stays under 120 requests per minute
 
for row in range(SIZE):
    cells = []
    for col in range(SIZE):
        pos = positions.get(f"{row},{col}")
        cells.append(f"{pos:>3}" if pos else "  -")
    print(" ".join(cells))
 
found = [p for p in positions.values() if p]
print(f"found at {len(found)}/{len(points)} points; average position {sum(found) / len(found):.1f}" if found
      else "not found in the top results at any point")

Example output (- means not in the top 20 at that point):

  -   -  14  11  12   -   -
  -   9   6   4   5   8   -
 13   5   3   2   2   4  10
  9   4   2   1   1   3   7
 12   6   3   2   2   5  11
  -  10   7   5   6   9   -
  -   -  15  12  13   -   -

Notes

  • location_coordinate is "latitude,longitude,zoom". Zoom defaults to 14z when you leave it out. You don't need a location_code alongside it.
  • Depth. Google returns about 20 Maps results per request. Depth up to 100 is served, at one unit per task.
  • Matching. cid is the most reliable key. place_id and title work too.
  • Caching. The standard queue reuses a Maps result up to 6 hours old for an identical keyword, coordinate, language and depth. Add "max_age": 0 for a fresh grid every time.
  • Live. For an on-demand grid in a UI, post the same tasks to /v1/serp/google/maps/live/advanced in arrays of 10 or so ($0.0018 each).
  • This recipe polls task_get per task, which is fine for 49 tasks. For many keywords, collect with tasks_ready or a postback_url instead, and mind the 120 requests per minute per key.

Reference: POSTGoogle Maps SERP: Task Post, GETGoogle Maps SERP: Task Get Advanced.