diff --git a/api/contacts-segmentation-live.php b/api/contacts-segmentation-live.php index 6877fd514..703514e17 100644 --- a/api/contacts-segmentation-live.php +++ b/api/contacts-segmentation-live.php @@ -13,11 +13,16 @@ function q($sql) { return $rows; } -$out = ['ts' => date('c'), 'generator' => 'contacts-segmentation-live.php v2']; +$out = ['ts' => date('c'), 'generator' => 'contacts-segmentation-live.php v3 (pipeline included)']; $summary = []; -foreach ([['leads','admin.leads'],['send_contacts','admin.send_contacts'],['weval_leads','admin.weval_leads']] as $pair) { - $rows = q("SELECT COALESCE(segment_type, 'UNCLASSIFIED'), count(*) FROM " . $pair[1] . " GROUP BY 1 ORDER BY 2 DESC"); +foreach ([ + ['leads','admin.leads'], + ['send_contacts','admin.send_contacts'], + ['weval_leads','admin.weval_leads'], + ['pipeline_contacts','admin.pipeline_contacts'] +] as $pair) { + $rows = q("SELECT COALESCE(segment_type,'UNCLASSIFIED'), count(*) FROM " . $pair[1] . " GROUP BY 1 ORDER BY 2 DESC"); foreach ($rows as $r) $summary[$pair[0]][$r[0]] = (int)$r[1]; } $out['summary'] = $summary; @@ -25,22 +30,30 @@ $out['summary'] = $summary; $industries = []; $rows = q("SELECT industry, SUM(cnt) FROM admin.contacts_breakdown WHERE segment_type='B2B' AND industry IS NOT NULL GROUP BY 1 ORDER BY 2 DESC"); foreach ($rows as $r) $industries[$r[0]] = (int)$r[1]; -$out['industries_b2b'] = $industries; +$out['industries_b2b_all_sources'] = $industries; -$pc = q("SELECT industry, count(*) FROM admin.pipeline_companies GROUP BY 1 ORDER BY 2 DESC LIMIT 20"); +$rows = q("SELECT industry, count(*) FROM admin.pipeline_contacts WHERE segment_type='B2B' AND industry IS NOT NULL GROUP BY 1 ORDER BY 2 DESC"); +$pc_ind = []; +foreach ($rows as $r) $pc_ind[$r[0]] = (int)$r[1]; +$out['industries_pipeline_contacts_b2b'] = $pc_ind; + +$pc = q("SELECT industry, count(*) FROM admin.pipeline_companies GROUP BY 1 ORDER BY 2 DESC LIMIT 25"); $pc_out = []; foreach ($pc as $r) $pc_out[$r[0]] = (int)$r[1]; -$out['pipeline_companies'] = $pc_out; +$out['pipeline_companies_by_industry'] = $pc_out; -$dom = q("SELECT industry, SPLIT_PART(email, '@', 2) d, count(*) FROM admin.leads WHERE segment_type='B2B' GROUP BY 1,2 ORDER BY 3 DESC LIMIT 30"); -$dom_out = []; -foreach ($dom as $r) { - if (count($r) >= 3) $dom_out[] = ['industry' => $r[0], 'domain' => $r[1], 'count' => (int)$r[2]]; -} -$out['top_b2b_domains'] = $dom_out; +$tt = q("SELECT job_title, count(*) FROM admin.pipeline_contacts WHERE (job_title ILIKE '%director%' OR job_title ILIKE '%directeur%' OR job_title ILIKE '%ceo%' OR job_title ILIKE '%cfo%' OR job_title ILIKE '%cto%' OR job_title ILIKE '%chief%' OR job_title ILIKE '%achat%' OR job_title ILIKE '%procurement%') AND job_title != '' GROUP BY 1 ORDER BY 2 DESC LIMIT 20"); +$tt_out = []; +foreach ($tt as $r) if (count($r) >= 2) $tt_out[] = ['title' => $r[0], 'count' => (int)$r[1]]; +$out['top_decision_makers'] = $tt_out; -$tot = q("SELECT (SELECT count(*) FROM admin.leads), (SELECT count(*) FROM admin.leads WHERE segment_type IS NOT NULL), (SELECT count(*) FROM admin.send_contacts), (SELECT count(*) FROM admin.send_contacts WHERE segment_type IS NOT NULL), (SELECT count(*) FROM admin.pipeline_companies)"); -if (!empty($tot[0]) && count($tot[0]) >= 5) { +$src = q("SELECT source, count(*) FROM admin.pipeline_contacts GROUP BY 1 ORDER BY 2 DESC LIMIT 10"); +$src_out = []; +foreach ($src as $r) if (count($r) >= 2) $src_out[$r[0]] = (int)$r[1]; +$out['contacts_by_source'] = $src_out; + +$tot = q("SELECT (SELECT count(*) FROM admin.leads), (SELECT count(*) FROM admin.leads WHERE segment_type IS NOT NULL), (SELECT count(*) FROM admin.send_contacts), (SELECT count(*) FROM admin.send_contacts WHERE segment_type IS NOT NULL), (SELECT count(*) FROM admin.pipeline_companies), (SELECT count(*) FROM admin.pipeline_contacts), (SELECT count(*) FROM admin.pipeline_contacts WHERE company_id IS NOT NULL)"); +if (!empty($tot[0]) && count($tot[0]) >= 7) { $p = $tot[0]; $out['progress'] = [ 'leads_total' => (int)$p[0], @@ -48,8 +61,11 @@ if (!empty($tot[0]) && count($tot[0]) >= 5) { 'send_contacts_total' => (int)$p[2], 'send_contacts_classified' => (int)$p[3], 'pipeline_companies_total' => (int)$p[4], - 'leads_pct' => $p[0] > 0 ? round(100 * $p[1] / $p[0], 1) : 0, - 'send_contacts_pct' => $p[2] > 0 ? round(100 * $p[3] / $p[2], 1) : 0, + 'pipeline_contacts_total' => (int)$p[5], + 'pipeline_contacts_linked_company' => (int)$p[6], + 'leads_pct' => $p[0] > 0 ? round(100*$p[1]/$p[0], 1) : 0, + 'send_contacts_pct' => $p[2] > 0 ? round(100*$p[3]/$p[2], 1) : 0, + 'contacts_linked_pct' => $p[5] > 0 ? round(100*$p[6]/$p[5], 1) : 0, ]; }