210 lines
7.0 KiB
PHP
Executable File
210 lines
7.0 KiB
PHP
Executable File
|
|
<?php
|
|
/**
|
|
* DATA ANALYTICS API
|
|
* Client list analysis and targeting intelligence
|
|
*/
|
|
header('Content-Type: application/json');
|
|
$pdo = new PDO("pgsql:host=localhost;dbname=adx_system", "admin", "admin123", [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
|
|
|
$action = $_GET['action'] ?? $_POST['action'] ?? '';
|
|
|
|
class DataAnalytics {
|
|
private $pdo;
|
|
|
|
public function __construct($pdo) {
|
|
$this->pdo = $pdo;
|
|
}
|
|
|
|
// Analyze list by country
|
|
public function analyzeByCountry() {
|
|
// Get from leads table
|
|
$result = $this->pdo->query("
|
|
SELECT
|
|
COALESCE(country, 'Unknown') as country,
|
|
COUNT(*) as total,
|
|
COUNT(CASE WHEN segment = 'hot' THEN 1 END) as hot,
|
|
COUNT(CASE WHEN segment = 'warm' THEN 1 END) as warm,
|
|
COUNT(CASE WHEN segment = 'cold' OR segment IS NULL THEN 1 END) as cold
|
|
FROM admin.leads
|
|
GROUP BY country
|
|
ORDER BY total DESC
|
|
")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
return $result;
|
|
}
|
|
|
|
// Analyze by ISP (detect from email domain)
|
|
public function analyzeByISP() {
|
|
$ispMapping = [
|
|
'gmail.com' => 'Gmail',
|
|
'googlemail.com' => 'Gmail',
|
|
'outlook.com' => 'Outlook',
|
|
'hotmail.com' => 'Outlook',
|
|
'live.com' => 'Outlook',
|
|
'yahoo.com' => 'Yahoo',
|
|
'yahoo.fr' => 'Yahoo',
|
|
'gmx.de' => 'GMX',
|
|
'gmx.net' => 'GMX',
|
|
'web.de' => 'Web.de',
|
|
't-online.de' => 'T-Online',
|
|
'orange.fr' => 'Orange',
|
|
'wanadoo.fr' => 'Orange',
|
|
'sfr.fr' => 'SFR',
|
|
'free.fr' => 'Free',
|
|
'laposte.net' => 'LaPoste'
|
|
];
|
|
|
|
$result = [];
|
|
foreach ($ispMapping as $domain => $isp) {
|
|
$count = $this->pdo->query("SELECT COUNT(*) FROM admin.leads WHERE email LIKE '%@$domain'")->fetchColumn();
|
|
if ($count > 0) {
|
|
if (!isset($result[$isp])) {
|
|
$result[$isp] = 0;
|
|
}
|
|
$result[$isp] += $count;
|
|
}
|
|
}
|
|
|
|
// Sort by count
|
|
arsort($result);
|
|
|
|
$formatted = [];
|
|
foreach ($result as $isp => $count) {
|
|
$formatted[] = ['isp' => $isp, 'count' => $count];
|
|
}
|
|
|
|
return $formatted;
|
|
}
|
|
|
|
// Segment leads by engagement
|
|
public function segmentLeads() {
|
|
// Update segments based on activity
|
|
$this->pdo->exec("
|
|
UPDATE admin.leads SET segment =
|
|
CASE
|
|
WHEN last_open > NOW() - INTERVAL '7 days' THEN 'hot'
|
|
WHEN last_open > NOW() - INTERVAL '30 days' THEN 'warm'
|
|
ELSE 'cold'
|
|
END
|
|
WHERE last_open IS NOT NULL
|
|
");
|
|
|
|
$segments = $this->pdo->query("
|
|
SELECT
|
|
COALESCE(segment, 'cold') as segment,
|
|
COUNT(*) as count
|
|
FROM admin.leads
|
|
GROUP BY segment
|
|
ORDER BY count DESC
|
|
")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
return $segments;
|
|
}
|
|
|
|
// Get targeting recommendations
|
|
public function getRecommendations($volume = 10000) {
|
|
$recommendations = [];
|
|
|
|
// Best performing ISPs from brain_winners
|
|
$winners = $this->pdo->query("SELECT isp_target, inbox_rate, stability_score FROM admin.brain_winners ORDER BY inbox_rate DESC LIMIT 5")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Best countries by open rate
|
|
$countries = $this->pdo->query("SELECT country_code, country_name, avg_open_rate FROM admin.country_profiles ORDER BY avg_open_rate DESC LIMIT 5")->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// Optimal send times
|
|
$now = new DateTime();
|
|
$hour = (int)$now->format('H');
|
|
|
|
$recommendations = [
|
|
'target_isps' => array_column($winners, 'isp_target'),
|
|
'target_countries' => array_column($countries, 'country_code'),
|
|
'suggested_volume' => [
|
|
'hot_segment' => min($volume * 0.2, 2000),
|
|
'warm_segment' => min($volume * 0.3, 3000),
|
|
'cold_segment' => min($volume * 0.5, 5000)
|
|
],
|
|
'optimal_times' => [
|
|
'morning' => '09:00-11:00 local',
|
|
'afternoon' => '14:00-16:00 local'
|
|
],
|
|
'best_days' => ['Tuesday', 'Wednesday', 'Thursday'],
|
|
'brain_winners' => $winners
|
|
];
|
|
|
|
return $recommendations;
|
|
}
|
|
|
|
// Score a specific lead
|
|
public function scoreLead($email) {
|
|
$lead = $this->pdo->query("SELECT * FROM admin.leads WHERE email = '$email'")->fetch(PDO::FETCH_ASSOC);
|
|
if (!$lead) return ['error' => 'Lead not found'];
|
|
|
|
$score = 0;
|
|
|
|
// Recency score
|
|
if (!empty($lead['last_open'])) {
|
|
$lastOpen = new DateTime($lead['last_open']);
|
|
$daysSince = (new DateTime())->diff($lastOpen)->days;
|
|
if ($daysSince < 7) $score += 40;
|
|
elseif ($daysSince < 30) $score += 25;
|
|
elseif ($daysSince < 90) $score += 10;
|
|
}
|
|
|
|
// Frequency score
|
|
$opens = $lead['opens_count'] ?? 0;
|
|
if ($opens > 10) $score += 30;
|
|
elseif ($opens > 5) $score += 20;
|
|
elseif ($opens > 0) $score += 10;
|
|
|
|
// ISP quality score
|
|
$email_domain = substr(strrchr($email, "@"), 1);
|
|
$goodIsps = ['gmail.com', 'outlook.com', 'gmx.de', 'web.de'];
|
|
if (in_array($email_domain, $goodIsps)) $score += 20;
|
|
|
|
return [
|
|
'email' => $email,
|
|
'score' => $score,
|
|
'segment' => $score >= 50 ? 'hot' : ($score >= 25 ? 'warm' : 'cold'),
|
|
'details' => $lead
|
|
];
|
|
}
|
|
}
|
|
|
|
$analytics = new DataAnalytics($pdo);
|
|
|
|
switch ($action) {
|
|
case 'by_country':
|
|
echo json_encode(['countries' => $analytics->analyzeByCountry()]);
|
|
break;
|
|
case 'by_isp':
|
|
echo json_encode(['isps' => $analytics->analyzeByISP()]);
|
|
break;
|
|
case 'segments':
|
|
echo json_encode(['segments' => $analytics->segmentLeads()]);
|
|
break;
|
|
case 'recommendations':
|
|
$volume = $_GET['volume'] ?? 10000;
|
|
echo json_encode($analytics->getRecommendations($volume));
|
|
break;
|
|
case 'score':
|
|
$email = $_GET['email'] ?? '';
|
|
echo json_encode($analytics->scoreLead($email));
|
|
break;
|
|
case 'full':
|
|
echo json_encode([
|
|
'by_country' => $analytics->analyzeByCountry(),
|
|
'by_isp' => $analytics->analyzeByISP(),
|
|
'segments' => $analytics->segmentLeads(),
|
|
'recommendations' => $analytics->getRecommendations()
|
|
]);
|
|
break;
|
|
default:
|
|
echo json_encode([
|
|
'name' => 'Data Analytics API',
|
|
'actions' => ['by_country', 'by_isp', 'segments', 'recommendations', 'score', 'full'],
|
|
'description' => 'Client list analysis and targeting intelligence'
|
|
]);
|
|
}
|
|
|