import os, requests, time
G2 = os.environ["G2_API_KEY"]
MV = os.environ["MAVERA_API_KEY"]
G2_BASE = "https://data.g2.com/api/v1"
G2_H = {"Authorization": f"Token token={G2}", "Content-Type": "application/vnd.api+json"}
MV_H = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
# 1. Pull product scores (replace with your product ID)
PRODUCT_ID = "your-product-id"
product = requests.get(f"{G2_BASE}/products/{PRODUCT_ID}",
headers=G2_H).json().get("data", {})
attrs = product.get("attributes", {})
scores = {
"satisfaction": attrs.get("satisfaction_score", 0),
"market_presence": attrs.get("market_presence_score", 0),
"implementation": attrs.get("implementation_score", 0),
"results": attrs.get("results_score", 0),
"review_count": attrs.get("review_count", 0),
"avg_rating": attrs.get("average_rating", 0),
}
# 2. Pull competitor scores for comparison
COMPETITOR_IDS = ["comp-a-id", "comp-b-id", "comp-c-id"]
comp_scores = []
for cid in COMPETITOR_IDS:
r = requests.get(f"{G2_BASE}/products/{cid}", headers=G2_H)
if r.status_code == 200:
ca = r.json().get("data", {}).get("attributes", {})
comp_scores.append({
"name": ca.get("name", "Unknown"),
"satisfaction": ca.get("satisfaction_score", 0),
"market_presence": ca.get("market_presence_score", 0),
"avg_rating": ca.get("average_rating", 0),
"review_count": ca.get("review_count", 0),
})
time.sleep(0.1)
# 3. Simulate historical trend (in production, store and compare)
previous_scores = {
"satisfaction": scores["satisfaction"] - 3,
"market_presence": scores["market_presence"] + 2,
"implementation": scores["implementation"] - 1,
}
# 4. Mave positioning analysis
score_block = "\n".join(f"- {k}: {v}" for k, v in scores.items())
comp_block = "\n".join(
f"- {c['name']}: Satisfaction={c['satisfaction']}, Presence={c['market_presence']}, Rating={c['avg_rating']}, Reviews={c['review_count']}"
for c in comp_scores
)
trend_block = "\n".join(
f"- {k}: {previous_scores[k]} → {scores[k]} ({'+' if scores[k]>previous_scores[k] else ''}{scores[k]-previous_scores[k]})"
for k in previous_scores
)
analysis = requests.post("https://app.mavera.io/api/v1/mave/chat",
headers=MV_H,
json={"message": f"""Analyze our G2 market position and recommend messaging adjustments.
OUR SCORES:
{score_block}
COMPETITOR COMPARISON:
{comp_block}
SCORE TRENDS (previous → current):
{trend_block}
Produce:
1. Position summary (leader/contender/niche/high performer quadrant)
2. Strengths to amplify in messaging (where we beat competitors)
3. Weaknesses to address (where competitors beat us)
4. Specific messaging updates (what to say differently this quarter)
5. Review generation strategy (how to improve review volume and scores)
6. Competitive talking points for sales"""}).json()
print("=== G2 Market Positioning Analysis ===")
print(analysis.get("content", "")[:2000])