Побудова lookalike-аудиторій на основі даних веб-сайту
Lookalike-аудиторії — рекламні сегменти, які схожі на ваших найкращих клієнтів. Будуються на основі даних про конверсії з сайту та завантажуються в рекламні системи (Meta, VK, Яндекс) для пошуку схожих користувачів.
Джерела даних для lookalike
Якісні seed-аудиторії дають кращий результат:
| Сегмент | Розмір (min) | Якість |
|---|---|---|
| Покупці за 90 днів | 500–1000 | Дуже висока |
| Користувачі з LTV > N | 500 | Висока |
| Trial → платний | 300 | Висока |
| Переглянули ключові сторінки | 1000+ | Середня |
| Всі зареєстровані | 5000+ | Низька |
Підготовка даних і хеширування
Перед завантаженням в рекламні системи email та телефони повинні бути захешовані (SHA-256):
class AudienceExporter
{
public function exportHighValueCustomers(): array
{
return User::query()
->join('orders', 'orders.user_id', '=', 'users.id')
->where('orders.status', 'completed')
->where('orders.created_at', '>=', now()->subDays(90))
->groupBy('users.id', 'users.email', 'users.phone')
->havingRaw('SUM(orders.total) >= ?', [5000])
->get(['users.email', 'users.phone'])
->map(fn($user) => [
'email' => hash('sha256', strtolower(trim($user->email))),
'phone' => $user->phone ? hash('sha256', $this->normalizePhone($user->phone)) : null,
])
->toArray();
}
private function normalizePhone(string $phone): string
{
// Приводимо до формату E.164: +79991234567
$digits = preg_replace('/\D/', '', $phone);
if (strlen($digits) === 10) $digits = '7' . $digits;
return '+' . $digits;
}
public function exportToCsv(array $data, string $filename): string
{
$path = storage_path("app/audiences/{$filename}.csv");
$fp = fopen($path, 'w');
fputcsv($fp, ['email', 'phone']);
foreach ($data as $row) {
fputcsv($fp, [$row['email'], $row['phone'] ?? '']);
}
fclose($fp);
return $path;
}
}
Завантаження в Meta через API
class MetaAudienceService
{
private const API_VERSION = 'v19.0';
private string $accessToken;
private string $adAccountId;
public function createCustomAudience(string $name, string $description): string
{
$response = Http::post(
"https://graph.facebook.com/{$this->API_VERSION}/act_{$this->adAccountId}/customaudiences",
[
'name' => $name,
'subtype' => 'CUSTOM',
'description' => $description,
'customer_file_source' => 'USER_PROVIDED_ONLY',
'access_token' => $this->accessToken,
]
);
return $response->json('id');
}
public function uploadUsers(string $audienceId, array $hashedEmails): void
{
// Meta приймає партії по 10000 записів
foreach (array_chunk($hashedEmails, 10000) as $chunk) {
$payload = [
'schema' => ['EMAIL_SHA256'],
'data' => array_map(fn($email) => [$email], $chunk),
];
Http::post(
"https://graph.facebook.com/{$this->API_VERSION}/{$audienceId}/users",
[
'payload' => json_encode($payload),
'access_token' => $this->accessToken,
]
);
}
}
public function createLookalike(string $sourceAudienceId, string $country, float $ratio = 0.01): string
{
// ratio: 0.01 = 1% (найбільш схожі), 0.10 = 10% (менш схожі, але більше)
$response = Http::post(
"https://graph.facebook.com/{$this->API_VERSION}/act_{$this->adAccountId}/customaudiences",
[
'name' => "Lookalike {$country} {$ratio}",
'subtype' => 'LOOKALIKE',
'origin_audience_id' => $sourceAudienceId,
'lookalike_spec' => json_encode([
'type' => 'similarity',
'country' => $country,
'ratio' => $ratio,
]),
'access_token' => $this->accessToken,
]
);
return $response->json('id');
}
}
Завантаження в VK Ads
class VkAudienceService
{
public function uploadToRetargeting(string $name, array $hashedEmails): int
{
// VK приймає email або телефони в відкритому виді (хешує сам)
// або SHA-256 з префіксом
$content = implode("\n", $hashedEmails);
$response = Http::withToken($this->token)
->post('https://api.vk.com/method/ads.createTargetGroup', [
'account_id' => $this->accountId,
'name' => $name,
'v' => '5.199',
]);
$groupId = $response->json('response.id');
// Завантажуємо дані
Http::withToken($this->token)
->post('https://api.vk.com/method/ads.importTargetContacts', [
'account_id' => $this->accountId,
'target_group_id' => $groupId,
'contacts' => $content,
'v' => '5.199',
]);
return $groupId;
}
}
Автоматичне оновлення аудиторій
// Тижневе оновлення через Scheduler
class UpdateLookalikeAudiences implements ShouldQueue
{
public function handle(): void
{
$exporter = app(AudienceExporter::class);
$data = $exporter->exportHighValueCustomers();
$emails = array_column($data, 'email');
// Оновлюємо в Meta
app(MetaAudienceService::class)->uploadUsers(
config('ads.meta.buyer_audience_id'),
$emails
);
Log::info("Lookalike audience updated", ['count' => count($emails)]);
}
}
Графік
Система експорту аудиторій з хешуванням та автоматичним оновленням у Meta/VK: 4-6 робочих днів.







