AI-optimized social advertising targeting
Programmatic optimization in Facebook/Instagram Ads and TikTok Ads is the automated selection of audiences, creatives, and bids using machine learning. Proper setup reduces CPA by 30-50% and improves ROAS by 1.5-2x compared to manual management.
Automatic campaign optimization
import numpy as np
import pandas as pd
from sklearn.ensemble import GradientBoostingClassifier
from anthropic import Anthropic
import json
class AudienceOptimizer:
"""Оптимизация lookalike и interest-based аудиторий"""
def build_seed_audience(self, customers: pd.DataFrame,
customer_value_col: str = 'ltv_90d',
top_pct: float = 0.20) -> pd.DataFrame:
"""
Seed аудитория для lookalike: топ клиентов по LTV.
Чем лучше seed, тем точнее lookalike.
"""
top_customers = customers.nlargest(
int(len(customers) * top_pct), customer_value_col
)
# Профиль seed аудитории для понимания характеристик
profile = {
'size': len(top_customers),
'avg_ltv': top_customers[customer_value_col].mean(),
'age_distribution': top_customers.get('age_group', pd.Series(['25-34'])).value_counts(normalize=True).to_dict(),
'top_interests': top_customers.get('interests', pd.Series([[]])).explode().value_counts().head(10).to_dict(),
}
return top_customers, profile
def score_audience_segments(self, segment_performance: pd.DataFrame) -> pd.DataFrame:
"""Ранжирование аудиторных сегментов по эффективности"""
df = segment_performance.copy()
# Нормализованный скор: ROAS + CTR - CPA
df['roas_norm'] = (df['roas'] - df['roas'].min()) / (df['roas'].max() - df['roas'].min() + 1e-9)
df['ctr_norm'] = (df['ctr'] - df['ctr'].min()) / (df['ctr'].max() - df['ctr'].min() + 1e-9)
df['cpa_norm'] = 1 - (df['cpa'] - df['cpa'].min()) / (df['cpa'].max() - df['cpa'].min() + 1e-9)
df['segment_score'] = (
df['roas_norm'] * 0.50 +
df['cpa_norm'] * 0.30 +
df['ctr_norm'] * 0.20
)
return df.sort_values('segment_score', ascending=False)
class CreativeOptimizer:
"""Оптимизация рекламных креативов"""
def __init__(self):
self.llm = Anthropic()
def analyze_creative_performance(self,
creative_data: pd.DataFrame) -> dict:
"""Анализ элементов, влияющих на CTR и конверсию"""
# Предположим, что у нас есть теги для каждого креатива
if creative_data.empty:
return {}
tag_performance = {}
tag_cols = [c for c in creative_data.columns if c.startswith('has_')]
for tag_col in tag_cols:
tag = tag_col.replace('has_', '')
with_tag = creative_data[creative_data[tag_col] == 1]
without_tag = creative_data[creative_data[tag_col] == 0]
if len(with_tag) > 5 and len(without_tag) > 5:
lift = with_tag['ctr'].mean() / max(without_tag['ctr'].mean(), 1e-9) - 1
tag_performance[tag] = {
'avg_ctr': round(with_tag['ctr'].mean(), 4),
'ctr_lift': round(lift, 3),
'sample_size': len(with_tag)
}
return dict(sorted(tag_performance.items(), key=lambda x: -x[1]['ctr_lift']))
def generate_creative_variants(self, product: dict,
top_performing_elements: list[str],
target_audience: dict) -> list[dict]:
"""Генерация вариантов текстов для A/B теста"""
response = self.llm.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=500,
messages=[{
"role": "user",
"content": f"""Generate 4 ad copy variants for social media in Russian.
Product: {product.get('name')}
Key benefit: {product.get('main_benefit')}
Price point: {product.get('price', '')}
High-performing creative elements to incorporate: {top_performing_elements[:5]}
Target audience: {target_audience}
For each variant, return JSON:
{{"headline": "max 25 chars", "body": "max 125 chars", "cta": "button text", "angle": "urgency|social_proof|benefit|curiosity"}}
Return JSON array of 4 variants."""
}]
)
try:
return json.loads(response.content[0].text)
except Exception:
return []
class BidOptimizer:
"""Оптимизация ставок в рекламных аукционах"""
def compute_optimal_bid(self, target_cpa: float,
predicted_cvr: float,
competition_level: float = 1.0) -> float:
"""
Оптимальная ставка = target_CPA × CVR.
Конкурентная поправка для hot аукционов.
"""
base_bid = target_cpa * predicted_cvr
adjusted_bid = base_bid * competition_level
return round(adjusted_bid, 2)
def dayparting_multipliers(self, hourly_performance: pd.DataFrame) -> dict:
"""Мультипликаторы ставок по часам суток"""
# Нормализуем относительно среднего ROAS
avg_roas = hourly_performance['roas'].mean()
multipliers = {}
for _, row in hourly_performance.iterrows():
hour = row['hour']
roas = row['roas']
multiplier = roas / avg_roas if avg_roas > 0 else 1.0
multipliers[hour] = round(float(np.clip(multiplier, 0.5, 2.0)), 2)
return multipliers
def portfolio_budget_allocation(self, campaigns: pd.DataFrame,
total_budget: float) -> dict:
"""Распределение бюджета между кампаниями по эффективности"""
# Аллоцируем пропорционально ROAS × объём конверсий
campaigns = campaigns.copy()
campaigns['weight'] = campaigns['roas'] * np.sqrt(campaigns['conversions'].clip(1))
campaigns['weight'] = campaigns['weight'] / campaigns['weight'].sum()
campaigns['allocated_budget'] = (campaigns['weight'] * total_budget).round(2)
return campaigns.set_index('campaign_id')['allocated_budget'].to_dict()
A typical result of AI-based social ad optimization: CPA decreases by 25-40% during the first 2-3 weeks of algorithm training. Key rules: don't change audiences and budgets during the learning phase (Facebook's learning phase: 50 conversions in 7 days), and regularly update the audience seed as your base grows.







