import os, requests, time
META = os.environ["META_ACCESS_TOKEN"]
ACCT = os.environ["META_AD_ACCOUNT_ID"]
MV = os.environ["MAVERA_API_KEY"]
GRAPH = "https://graph.facebook.com/v24.0"
MB = "https://app.mavera.io/api/v1"
MH = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
# 1. Pull Custom Audiences
audiences = requests.get(
f"{GRAPH}/{ACCT}/customaudiences",
params={
"access_token": META,
"fields": "id,name,description,subtype,approximate_count,data_source,delivery_status",
"limit": 25,
},
).json().get("data", [])
active_audiences = [a for a in audiences
if a.get("delivery_status", {}).get("status") != "deleted"
and a.get("approximate_count", 0) > 100]
print(f"Active audiences: {len(active_audiences)}")
# 2. Create mirrored personas
persona_map = []
for aud in active_audiences[:6]:
subtype = aud.get("subtype", "CUSTOM")
data_source = aud.get("data_source", {})
source_type = data_source.get("type", "unknown")
source_desc = data_source.get("sub_type", source_type)
if subtype == "LOOKALIKE":
persona_desc = (
f"Lookalike audience: {aud['name']}. ~{aud.get('approximate_count',0):,} users. "
f"Mirrors users similar to your seed audience. Likely shares demographic and "
f"behavioral traits with your best customers but hasn't engaged with your brand yet."
)
elif subtype == "WEBSITE":
persona_desc = (
f"Website visitor audience: {aud['name']}. ~{aud.get('approximate_count',0):,} users. "
f"Has visited your website — already aware of your product. "
f"Evaluating or comparing options. Warm prospect mindset."
)
elif subtype == "ENGAGEMENT":
persona_desc = (
f"Engagement audience: {aud['name']}. ~{aud.get('approximate_count',0):,} users. "
f"Has interacted with your social content. Aware and interested but not yet converted. "
f"Responds to social proof and community-oriented messaging."
)
else:
persona_desc = (
f"Custom audience: {aud['name']}. Source: {source_desc}. "
f"~{aud.get('approximate_count',0):,} users. {aud.get('description','No description.')}"
)
persona = requests.post(f"{MB}/personas", headers=MH, json={
"name": f"Meta Audience: {aud['name'][:50]}",
"description": persona_desc,
"psychographic": {
"audience_type": subtype.lower(),
"awareness_level": "warm" if subtype in ("WEBSITE", "ENGAGEMENT") else "cold",
"source": source_desc,
},
}).json()
persona_map.append({
"audience_id": aud["id"],
"audience_name": aud["name"],
"persona_id": persona["id"],
"subtype": subtype,
"size": aud.get("approximate_count", 0),
})
time.sleep(0.3)
# 3. Run Focus Group with ad creative stimulus
AD_CREATIVE = """
Headline: "Your Competitors Already Know This"
Body: "Teams using centralized analytics make decisions 40% faster. See how in 2 minutes."
CTA: Learn More
Visual: Split-screen showing cluttered dashboards vs clean single-pane view.
"""
fg = requests.post(f"{MB}/focus-groups", headers=MH, json={
"name": "Custom Audience Creative Test",
"persona_ids": [p["persona_id"] for p in persona_map],
"questions": [
f"You see this ad in your Instagram feed:\n\n{AD_CREATIVE}\n\nWhat's your first reaction?",
"Does the headline create curiosity or feel clickbait-y? Why?",
"Rate your likelihood to click (1-10). What would change your rating?",
"What would this ad need to say to make you stop scrolling?",
"If you clicked, what would you expect on the landing page?",
],
"context": "Testing ad creative against different audience segments defined by Meta targeting.",
"responses_per_persona": 2,
}).json()
# 4. Poll and display
for _ in range(24):
time.sleep(5)
data = requests.get(f"{MB}/focus-groups/{fg['id']}", headers=MH).json()
if data.get("status") == "completed":
break
print("\n=== Audience-Mirrored Focus Group ===")
for pm in persona_map:
print(f"\n[{pm['audience_name']}] ({pm['subtype']}, {pm['size']:,} users)")
audience_responses = [r for r in data.get("responses", [])
if r.get("persona_id") == pm["persona_id"]]
for resp in audience_responses[:3]:
print(f" Q: {resp.get('question','')[:60]}...")
print(f" A: {resp.get('answer','')[:200]}")