import os, requests, time
KL_KEY = os.environ["KLAVIYO_API_KEY"]
MV = os.environ["MAVERA_API_KEY"]
MB = "https://app.mavera.io/api/v1"
MH = {"Authorization": f"Bearer {MV}", "Content-Type": "application/json"}
KH = {
"Authorization": f"Klaviyo-API-Key {KL_KEY}",
"Content-Type": "application/json",
"revision": "2024-10-15",
}
CAMPAIGN_DATA = [
{
"name": "Flash Sale — 24hr",
"email": {"open_rate": 0.22, "click_rate": 0.035, "conversion": 0.012, "sent": 15000},
"sms": {"click_rate": 0.11, "conversion": 0.028, "sent": 8000},
},
{
"name": "New Product Launch",
"email": {"open_rate": 0.31, "click_rate": 0.048, "conversion": 0.018, "sent": 15000},
"sms": {"click_rate": 0.09, "conversion": 0.022, "sent": 8000},
},
{
"name": "Restock Alert",
"email": {"open_rate": 0.28, "click_rate": 0.062, "conversion": 0.034, "sent": 5000},
"sms": {"click_rate": 0.15, "conversion": 0.048, "sent": 3000},
},
{
"name": "Weekly Newsletter",
"email": {"open_rate": 0.18, "click_rate": 0.025, "conversion": 0.005, "sent": 15000},
"sms": {"click_rate": 0.04, "conversion": 0.008, "sent": 8000},
},
{
"name": "Abandoned Cart",
"email": {"open_rate": 0.35, "click_rate": 0.072, "conversion": 0.042, "sent": 3000},
"sms": {"click_rate": 0.18, "conversion": 0.065, "sent": 2000},
},
]
performance_block = []
for camp in CAMPAIGN_DATA:
email = camp["email"]
sms = camp["sms"]
winner_click = "SMS" if sms["click_rate"] > email["click_rate"] else "Email"
winner_conv = "SMS" if sms["conversion"] > email["conversion"] else "Email"
performance_block.append(
f"**{camp['name']}**\n"
f" Email: {email['open_rate']:.0%} open, {email['click_rate']:.0%} click, {email['conversion']:.1%} conv ({email['sent']:,} sent)\n"
f" SMS: {sms['click_rate']:.0%} click, {sms['conversion']:.1%} conv ({sms['sent']:,} sent)\n"
f" Winner: Click={winner_click}, Conversion={winner_conv}"
)
perf_summary = "\n\n".join(performance_block)
personas = []
for name, desc in [
("Mobile-First Shopper", "Does 90% of shopping on phone. Prefers quick interactions. Opens texts within 3 minutes. Age 22-30."),
("Desktop Researcher", "Researches on desktop, buys on desktop. Reads full emails. Finds SMS marketing intrusive. Age 35-45."),
("Omnichannel Buyer", "Uses phone, tablet, and desktop. Appreciates consistent messaging. Doesn't mind either channel if relevant. Age 28-38."),
("Deal Hunter", "Only buys on sale. Has notifications on for deals. Speed matters — first to know = first to buy. Age 25-40."),
]:
p = requests.post(f"{MB}/personas", headers=MH, json={"name": name, "description": desc}).json()
personas.append(p)
time.sleep(0.3)
fg = requests.post(f"{MB}/focus-groups", headers=MH, json={
"name": "Klaviyo: SMS vs Email Channel Study",
"persona_ids": [p["id"] for p in personas],
"questions": [
"For each of these message types, would you prefer to receive it as an SMS or email? Explain for each:\n 1. Flash sale (24hr only)\n 2. New product launch\n 3. Restock alert for a product you wanted\n 4. Weekly newsletter/content\n 5. Abandoned cart reminder",
"When you receive a promotional SMS, what's your immediate reaction? Be honest — annoyance, curiosity, or something else?",
"What's the MAXIMUM number of promotional texts per month you'd accept from a brand? What makes you unsubscribe from SMS?",
"If a brand sent you the same promotion via BOTH email and SMS, how would that make you feel? Redundant? Helpful? Aggressive?",
"Describe the perfect use of SMS by a brand you buy from. What message type, timing, and frequency would make you glad you opted in?",
],
"context": f"""Channel performance study comparing SMS vs. email for the same campaigns.
CAMPAIGN PERFORMANCE DATA:
{perf_summary}
Key insight: SMS consistently outperforms email on conversion rate (avg 2.8x higher), but has lower reach (smaller opted-in audience). The question is WHEN to use each channel — not which is globally better.""",
"responses_per_persona": 2,
}).json()
for _ in range(30):
time.sleep(5)
data = requests.get(f"{MB}/focus-groups/{fg['id']}", headers=MH).json()
if data.get("status") == "completed":
break
print(f"Focus Group: {data.get('id')} — {data.get('status')}\n")
for resp in data.get("responses", []):
print(f"[{resp.get('persona_id','?')}] {resp.get('question','')[:80]}")
print(f" → {resp.get('answer','')[:350]}\n")