insight列表数量统计、creative列表接口更新
This commit is contained in:
parent
c0f52aec6c
commit
74359478bb
@ -511,6 +511,7 @@ class AdsInsightService
|
||||
'data' => [],
|
||||
];
|
||||
}
|
||||
$userCount = count($userIds);
|
||||
// dump($userIds);
|
||||
// 动态构建日期条件
|
||||
$dateCondition = '1=1'; // 默认没有日期限制
|
||||
@ -736,7 +737,7 @@ class AdsInsightService
|
||||
'pagination' => [
|
||||
'startIndex' => ($page - 1) * $pageSize,
|
||||
'maxResults' => $pageSize,
|
||||
'count' => 0,
|
||||
'count' => count($emptyUser),
|
||||
'pageNo' => $page,
|
||||
'pageSize' => $pageSize,
|
||||
'pages' => 0,
|
||||
@ -753,7 +754,8 @@ class AdsInsightService
|
||||
$pagination = [
|
||||
'startIndex' => ($page - 1) * $pageSize,
|
||||
'maxResults' => $pageSize,
|
||||
'count' => $users->total(),
|
||||
// 'count' => $users->total(),
|
||||
'count' => $userCount,
|
||||
'pageNo' => $page,
|
||||
'pageSize' => $pageSize,
|
||||
'pages' => ceil($users->total() / $pageSize),
|
||||
@ -812,16 +814,17 @@ class AdsInsightService
|
||||
'c.url AS creative_url', // 从 bps_ads_creative 表中选择 url
|
||||
'c.type AS creative_type', // 从 bps_ads_creative 表中选择 url
|
||||
'c.thumbnail_url', // 从 bps_ads_creative 表中选择 thumbnail_url
|
||||
ThinkDb::raw('SUM(i.spend) AS total_spend'),
|
||||
ThinkDb::raw('SUM(i.purchases_value) AS total_conversions_value'),
|
||||
ThinkDb::raw('SUM(i.purchases) AS total_conversions'),
|
||||
ThinkDb::raw('SUM(i.impressions) AS total_impressions'),
|
||||
ThinkDb::raw('SUM(i.clicks) AS total_clicks'),
|
||||
ThinkDb::raw('SUM(i.video_25) AS video_25'),
|
||||
ThinkDb::raw('SUM(i.video_50) AS video_50'),
|
||||
ThinkDb::raw('SUM(i.video_75) AS video_75'),
|
||||
ThinkDb::raw('SUM(i.video_100) AS video_100'),
|
||||
ThinkDb::raw('SUM(i.hold_rate) AS hold_rate')
|
||||
ThinkDb::raw('COALESCE(SUM(i.spend) / 1000000, 0) AS total_spend'),
|
||||
ThinkDb::raw('COALESCE(SUM(i.purchases_value) / 1000000, 0) AS total_purchases_value'),
|
||||
ThinkDb::raw('COALESCE(SUM(i.purchases), 0) AS total_purchases'),
|
||||
ThinkDb::raw('COALESCE(SUM(i.revenue) / 1000000, 0) AS total_revenue'),
|
||||
ThinkDb::raw('COALESCE(SUM(i.impressions), 0) AS total_impressions'),
|
||||
ThinkDb::raw('COALESCE(SUM(i.clicks), 0) AS total_clicks'),
|
||||
ThinkDb::raw('COALESCE(SUM(i.video_25),0) AS video_plays_25_rate'),
|
||||
ThinkDb::raw('COALESCE(SUM(i.video_50),0) AS video_plays_50_rate'),
|
||||
ThinkDb::raw('COALESCE(SUM(i.video_75),0) AS video_plays_75_rate'),
|
||||
ThinkDb::raw('COALESCE(SUM(i.video_100),0) AS video_plays_100_rate'),
|
||||
ThinkDb::raw('-1 AS hold_rate')
|
||||
]);
|
||||
|
||||
// 6. 执行查询并获取聚合结果
|
||||
@ -832,7 +835,8 @@ class AdsInsightService
|
||||
|
||||
// 7. 初始化广告创意的汇总数据和统计数据
|
||||
$creativeSummaryData = [];
|
||||
$statisticsData = $this->initializeStatistics();
|
||||
// $statisticsData = $this->initializeStatistics();
|
||||
$statisticsData = [];
|
||||
|
||||
// 8. 遍历查询结果并计算每个 creative 的相关统计数据
|
||||
foreach ($aggregatedData as $creativeData) {
|
||||
@ -846,7 +850,7 @@ class AdsInsightService
|
||||
'thumbnail_url' => $creativeData->thumbnail_url ?: '', // 使用联接查询中的 thumbnail_url
|
||||
'title' => $creativeData->title ?: '', // 使用联接查询中的 title
|
||||
'spend' => 0,
|
||||
'purchase_value' => '-',
|
||||
'purchases_value' => 0,
|
||||
'roas' => 0,
|
||||
'cpa' => '-',
|
||||
'cpc_link_click' => '-',
|
||||
@ -855,37 +859,40 @@ class AdsInsightService
|
||||
'aov' => '-',
|
||||
'click_to_atc_ratio' => '-',
|
||||
'atc_to_purchase_ratio' => '-',
|
||||
'purchases' => '-',
|
||||
'purchases' => 0,
|
||||
'first_frame_retention' => '-',
|
||||
'thumbstop' => '-',
|
||||
'ctr_outbound' => '-',
|
||||
'click_to_purchase' => '-',
|
||||
'ctr_all' => '-',
|
||||
'video_plays_25_rate' => '-',
|
||||
'video_plays_50_rate' => '-',
|
||||
'video_plays_75_rate' => '-',
|
||||
'video_plays_100_rate' => '-',
|
||||
'video_plays_25_rate' => 0,
|
||||
'video_plays_50_rate' => 0,
|
||||
'video_plays_75_rate' => 0,
|
||||
'video_plays_100_rate' => 0,
|
||||
'hold_rate' => '-',
|
||||
'total_conversions_value' => 0,
|
||||
'total_conversions' => 0,
|
||||
'total_impressions' => 0,
|
||||
'impressions' => 0,
|
||||
'clicks' => 0,
|
||||
'revenue' => 0,
|
||||
'ad_count' => 0
|
||||
];
|
||||
}
|
||||
|
||||
// 更新该 creative_id 的统计数据
|
||||
$creativeSummaryData[$creativeData->creative_id]['spend'] += $creativeData->total_spend;
|
||||
$creativeSummaryData[$creativeData->creative_id]['total_conversions_value'] += $creativeData->total_conversions_value;
|
||||
$creativeSummaryData[$creativeData->creative_id]['total_impressions'] += $creativeData->total_impressions;
|
||||
|
||||
// 汇总总体统计数据
|
||||
$statisticsData['spend'] += $creativeData->total_spend;
|
||||
$statisticsData['conversions_value'] += $creativeData->total_conversions_value;
|
||||
$statisticsData['impressions'] += $creativeData->total_impressions;
|
||||
|
||||
$creativeSummaryData[$creativeData->creative_id]['spend'] += $creativeData->total_spend;
|
||||
$creativeSummaryData[$creativeData->creative_id]['purchases_value'] += $creativeData->total_purchases_value;
|
||||
$creativeSummaryData[$creativeData->creative_id]['purchases'] += $creativeData->total_purchases;
|
||||
$creativeSummaryData[$creativeData->creative_id]['impressions'] += $creativeData->total_impressions;
|
||||
$creativeSummaryData[$creativeData->creative_id]['clicks'] += $creativeData->total_clicks;
|
||||
$creativeSummaryData[$creativeData->creative_id]['revenue'] += $creativeData->total_revenue;
|
||||
$creativeSummaryData[$creativeData->creative_id]['video_plays_25_rate'] += $creativeData->video_plays_25_rate;
|
||||
$creativeSummaryData[$creativeData->creative_id]['video_plays_50_rate'] += $creativeData->video_plays_50_rate;
|
||||
$creativeSummaryData[$creativeData->creative_id]['video_plays_75_rate'] += $creativeData->video_plays_75_rate;
|
||||
$creativeSummaryData[$creativeData->creative_id]['video_plays_100_rate'] += $creativeData->video_plays_100_rate;
|
||||
// 计算 ROAS
|
||||
$roas = $creativeSummaryData[$creativeData->creative_id]['spend'] > 0
|
||||
? $creativeSummaryData[$creativeData->creative_id]['total_conversions_value'] / $creativeSummaryData[$creativeData->creative_id]['spend']
|
||||
? $creativeSummaryData[$creativeData->creative_id]['revenue'] / $creativeSummaryData[$creativeData->creative_id]['spend']
|
||||
: 0;
|
||||
$creativeSummaryData[$creativeData->creative_id]['roas'] = $roas > 0 ? number_format($roas, 2) . 'X' : '-';
|
||||
|
||||
@ -893,11 +900,79 @@ class AdsInsightService
|
||||
$creativeSummaryData[$creativeData->creative_id]['ad_count'] = rand(10, 200); // 每个 creative_id 对应一个广告
|
||||
}
|
||||
|
||||
// 汇总总体统计数据
|
||||
$statisticsData['spend'] = array_sum(array_column($creativeSummaryData, 'spend'));
|
||||
$statisticsData['purchases_value'] = array_sum(array_column($creativeSummaryData, 'purchases_value'));
|
||||
$statisticsData['purchases'] = array_sum(array_column($creativeSummaryData, 'purchases'));
|
||||
$statisticsData['impressions'] = array_sum(array_column($creativeSummaryData, 'impressions'));
|
||||
$statisticsData['clicks'] = array_sum(array_column($creativeSummaryData, 'clicks'));
|
||||
$statisticsData['revenue'] = array_sum(array_column($creativeSummaryData, 'revenue'));
|
||||
// 汇总统计数据
|
||||
$statistics = [
|
||||
'spend' => '$' . number_format($statisticsData['spend'], 2), // 格式化金额
|
||||
'purchases_value' => '$' . number_format($statisticsData['purchases_value'], 2), // 格式化金额
|
||||
'roas' => $statisticsData['spend'] == 0 ? '-' : round($statisticsData['revenue'] / $statisticsData['spend'], 2) . 'X',
|
||||
'cpa' => '-', // 格式化金额
|
||||
'cpc_link_click' => '-', // 格式化金额
|
||||
'cpm' => '-', // 格式化金额
|
||||
'cpc_all' => '-', //格式化金额
|
||||
'aov' => '-', // 格式化金额
|
||||
'click_to_atc_ratio' => '-', // 格式化百分比
|
||||
'atc_to_purchase_ratio' => '-',// 格式化百分比
|
||||
'purchases' => $statisticsData['purchases'],
|
||||
'first_frame_retention' => '-',
|
||||
'thumbstop' => '-',
|
||||
'ctr_outbound' => '-',
|
||||
'click_to_purchase' => '-',
|
||||
'ctr_all' => ($statisticsData['impressions'] > 0) ? number_format(($statisticsData['clicks'] / $statisticsData['impressions']) * 100, 2) . '%' : '-', // 格式化为百分比
|
||||
'video_plays_25_rate' => array_sum(array_column($creativeSummaryData, 'video_plays_25_rate')),
|
||||
'video_plays_50_rate' => array_sum(array_column($creativeSummaryData, 'video_plays_50_rate')),
|
||||
'video_plays_75_rate' => array_sum(array_column($creativeSummaryData, 'video_plays_75_rate')),
|
||||
'video_plays_100_rate' => array_sum(array_column($creativeSummaryData, 'video_plays_100_rate')),
|
||||
'hold_rate' => '-',// 格式化百分比
|
||||
];
|
||||
|
||||
// 格式化返回的创意数据
|
||||
$formattedData = array_map(function ($item) {
|
||||
return [
|
||||
'creative_id' => $item['creative_id'],
|
||||
'creative' => $item['creative'] ?: '-',
|
||||
'creative_type' => $item['creative_type'],
|
||||
'creative_url' => $item['creative_url'],
|
||||
'thumbnail_url' => $item['thumbnail_url'],
|
||||
'spend' => '$' . number_format($item['spend'], 2),
|
||||
'purchases_value' => '$' . number_format($item['purchases_value'], 2),
|
||||
'roas' => $item['roas'],
|
||||
'cpa' => '-',
|
||||
'cpc_link_click' => '-',
|
||||
'cpm' => '-',
|
||||
'cpc_all' => '-',
|
||||
'aov' => '-',
|
||||
'click_to_atc_ratio' => '-',
|
||||
'atc_to_purchase_ratio' => '-',
|
||||
'purchases' => $item['purchases'],
|
||||
'first_frame_retention' => '-',
|
||||
'thumbstop' => '-',
|
||||
'ctr_outbound' => '-',
|
||||
'click_to_purchase' => '-',
|
||||
'ctr_all' => ($item['impressions'] > 0) ? number_format(($item['clicks'] / $item['impressions']) * 100, 2) . '%' : '-',
|
||||
'total_conversions_value' => '$' . number_format($item['purchases_value'], 2), //准备删除
|
||||
'impressions' => $item['impressions'],
|
||||
'video_plays_25_rate' => $item['video_plays_25_rate'],
|
||||
'video_plays_50_rate' => $item['video_plays_50_rate'],
|
||||
'video_plays_75_rate' => $item['video_plays_75_rate'],
|
||||
'video_plays_100_rate' => $item['video_plays_100_rate'],
|
||||
'hold_rate' => '-',
|
||||
'ad_count' => $item['ad_count'],
|
||||
// 添加更多的格式化字段
|
||||
];
|
||||
}, $creativeSummaryData);
|
||||
|
||||
// 9. 返回分页数据
|
||||
return [
|
||||
'data' => array_values($creativeSummaryData),
|
||||
'data' => array_values($formattedData),
|
||||
'total' => count($creativeSummaryData),
|
||||
'statistics' => $statisticsData, // 汇总的统计数据
|
||||
'statistics' => $statistics, // 汇总的统计数据
|
||||
'pagination' => [
|
||||
'startIndex' => ($page - 1) * $pageSize,
|
||||
'maxResults' => $pageSize,
|
||||
@ -916,11 +991,12 @@ class AdsInsightService
|
||||
private function initializeStatistics()
|
||||
{
|
||||
return [
|
||||
'conversions_value' => 0,
|
||||
'impressions' => 0,
|
||||
'clicks' => 0,
|
||||
'revenue' => 0,
|
||||
'spend' => 0,
|
||||
'purchase_value' => '-', // 可根据需求进一步计算
|
||||
'roas' => 0, // 可以根据需要计算总体 ROAS
|
||||
'purchases_value' => 0,
|
||||
'roas' => 0,
|
||||
'cpa' => '-',
|
||||
'cpc_link_click' => '-',
|
||||
'cpm' => '-',
|
||||
@ -928,17 +1004,17 @@ class AdsInsightService
|
||||
'aov' => '-',
|
||||
'click_to_atc_ratio' => '-',
|
||||
'atc_to_purchase_ratio' => '-',
|
||||
'purchases' => '-',
|
||||
'purchases' => 0,
|
||||
'first_frame_retention' => '-',
|
||||
'thumbstop' => '-',
|
||||
'ctr_outbound' => '-',
|
||||
'click_to_purchase' => '-',
|
||||
'ctr_all' => '-',
|
||||
'video_plays_25_rate' => '-',
|
||||
'video_plays_50_rate' => '-',
|
||||
'video_plays_75_rate' => '-',
|
||||
'video_plays_100_rate' => '-',
|
||||
'hold_rate' => '-'
|
||||
'video_plays_25_rate' => 0,
|
||||
'video_plays_50_rate' => 0,
|
||||
'video_plays_75_rate' => 0,
|
||||
'video_plays_100_rate' => 0,
|
||||
'hold_rate' => '-',
|
||||
// Add other stats as necessary
|
||||
];
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user