"""Aggregate the AI Overviews visibility study. Reads only raw/.json (Actor dataset rows) and prompts.json (design: which 5 brands belong to each category). Writes agregado.json.""" import json, os from collections import Counter, defaultdict D = os.path.dirname(os.path.abspath(__file__)) P = json.load(open(os.path.join(D, "prompts.json"))) # Source-type rule (explicit, by registrable-domain suffix match on the source host): REVIEW_SITES = ["g2.com", "capterra.com", "trustradius.com", "getapp.com", "softwareadvice.com", "trustpilot.com", "gartner.com", "pcmag.com", "techradar.com", "forbes.com", "zapier.com", "nerdwallet.com", "investopedia.com", "cnet.com", "tomsguide.com", "wirecutter.com", "nytimes.com", "zdnet.com"] # reddit.com -> "reddit"; youtube.com / youtu.be -> "youtube"; # domain of the audited brand of THAT category -> "audited-brand-site"; # domain of one of its 4 competitors (when given with a domain) -> "competitor-site"; # REVIEW_SITES -> "review-or-editorial"; everything else -> "other". # Note: forbes/pcmag/techradar/zapier etc. are editorial roundups, grouped with G2/Capterra on purpose. def match(host, dom): host = (host or "").lower() host = host[4:] if host.startswith("www.") else host return host == dom or host.endswith("." + dom) def source_type(host, brand_domains): if match(host, "reddit.com"): return "reddit" if match(host, "youtube.com") or match(host, "youtu.be"): return "youtube" if match(host, brand_domains[0]): return "audited-brand-site" if any(match(host, d) for d in brand_domains[1:]): return "competitor-site" if any(match(host, d) for d in REVIEW_SITES): return "review-or-editorial" return "other" out = {"engine": "ai-overviews", "country": "us", "samples": 1, "categories": {}, "sourceTypeRule": {"reddit": "reddit.com", "youtube": "youtube.com, youtu.be", "audited-brand-site": "domain of the category's audited brand", "competitor-site": "domain of one of the 4 competitors (Google Meet, Microsoft Teams have none)", "review-or-editorial": REVIEW_SITES, "other": "anything else"}} tot = ok_tot = none_tot = 0 dom_cit = Counter(); dom_ans = Counter(); st_cit = Counter(); st_ans = Counter() status_tot = Counter() for c in P["categories"]: f = os.path.join(D, "raw", c["slug"] + ".json") if not os.path.exists(f): out["categories"][c["slug"]] = {"missing": True}; continue rows = json.load(open(f)) names = [c["brand"]] + [x.split("=")[0] for x in c["competitors"]] doms = [c["domain"]] + [x.split("=")[1] for x in c["competitors"] if "=" in x] st = Counter(r.get("status") for r in rows); status_tot.update(st) ok = [r for r in rows if r.get("status") == "ok"] tot += len(rows); ok_tot += len(ok) per = {} for i, n in enumerate(names): m = l = cit = 0 for r in ok: if i == 0: mm, ll = bool(r.get("brandMentioned")), bool(r.get("brandLinked")) else: d = next((x for x in r.get("competitors") or [] if x.get("name") == n), {}) mm, ll = bool(d.get("mentioned")), bool(d.get("linked")) m += mm; l += ll; cit += (mm or ll) per[n] = {"cited": cit, "mentioned": m, "linked": l, "n": len(ok), "role": "brand" if i == 0 else "competitor"} none5 = sum(1 for r in ok if not r.get("brandCited") and not r.get("competitorsMentioned") and not r.get("competitorsLinked")) none_tot += none5 cst = Counter() for r in ok: seen = set(); seen_t = set() for s in r.get("sources") or []: h = (s.get("domain") or "").lower(); h = h[4:] if h.startswith("www.") else h if not h: continue dom_cit[h] += 1; seen.add(h) t = source_type(h, doms); st_cit[t] += 1; cst[t] += 1; seen_t.add(t) for h in seen: dom_ans[h] += 1 for t in seen_t: st_ans[t] += 1 out["categories"][c["slug"]] = {"label": c["label"], "rows": len(rows), "status": dict(st), "ok": len(ok), "brands": per, "okAnswersCitingNoneOfThe5": none5, "sourceTypeCitations": dict(cst), "avgSourcesPerOkAnswer": round(sum(len(r.get("sources") or []) for r in ok) / len(ok), 2) if ok else None} tc = sum(st_cit.values()) out["totals"] = {"prompts": tot, "status": dict(status_tot), "ok": ok_tot, "okRate": round(ok_tot / tot, 4) if tot else None, "okAnswersCitingNoneOfThe5": none_tot, "sourceCitations": tc, "distinctSourceDomains": len(dom_cit)} out["topSourceDomains"] = [{"domain": d, "citations": n, "answers": dom_ans[d]} for d, n in sorted(dom_cit.items(), key=lambda kv: (-kv[1], -dom_ans[kv[0]], kv[0]))[:30]] out["sourceTypeShare"] = {t: {"citations": st_cit[t], "shareOfCitations": round(st_cit[t] / tc, 4) if tc else None, "answersWithType": st_ans[t]} for t in ["reddit", "youtube", "audited-brand-site", "competitor-site", "review-or-editorial", "other"]} json.dump(out, open(os.path.join(D, "agregado.json"), "w"), indent=2, ensure_ascii=False) print(json.dumps(out["totals"], ensure_ascii=False))