报告数据优化1

This commit is contained in:
huangguancheng 2025-01-11 11:24:24 +08:00
parent eb4cb3b983
commit 4ccc1ebeee

View File

@ -81,7 +81,7 @@ class AdsInsightService
// 添加关键字过滤条件
$query->where(function ($query) use ($keyword, $platformType) {
if ($keyword) {
$query->where('c.campaign_name', 'like', '%' . $keyword . '%');
$query->where('c.name', 'like', '%' . $keyword . '%');
}
if ($platformType) {
$platformType = (int)$platformType;
@ -585,10 +585,10 @@ class AdsInsightService
'cost_per_purchase' => '$' . number_format($cost_per_purchase, 2) ?: '$0.00', // 格式化销售额$total_purchases_value,
'revenue' => '$' . number_format($total_revenue, 2) ?: '$0.00', // 格式化收入
'total_cost' => '$' . number_format($total_cost, 2) ?: '$0.00', // 格式化总成本
'conversion_rate' => '-', // 没有计算逻辑,保持为 '-'
'net_profit' => '-', // 没有提供 net_profit保持为 '-'
'net_profit_margin' => '-', // 没有提供 net_profit_margin保持为 '-'
'net_profit_on_ad_spend' => '-', // 没有提供 net_profit_on_ad_spend保持为 '-'
'conversion_rate' => $total_clicks == 0 ? '-' : round(($total_purchases / $total_clicks) * 100, 2) . '%', // 转换率
'net_profit' => ($total_revenue - $total_cost) >= 0 ? '+$' . number_format($total_revenue - $total_cost, 2) : '-$' . number_format($total_revenue - $total_cost, 2), // 净利润
'net_profit_margin' => $total_revenue == 0 ? '-' : round(($total_revenue - $total_cost) / $total_revenue, 2) * 100 . '%', // 净利润率
'net_profit_on_ad_spend' => $total_spend == 0 ? '-' : round(($total_revenue - $total_cost) / $total_spend, 2), // 广告支出净利润
'ctr' => $ctr, // CTR 字段
];
@ -649,22 +649,94 @@ class AdsInsightService
$total_cost = $data['total_cost'];
$total_clicks = $data['clicks'];
$total_impressions = $data['impressions'];
// $total_adds_to_cart = $data['adds_to_cart'];
// $total_cost_per_atc = $data['cost_per_atc'];
// $total_purchases = $data['purchases'];
// $total_assisted_purchases = $data['assisted_purchases'];
// $total_last_clicked_purchases = $data['last_clicked_purchases'];
// CTR 的计算:点击率 = 点击数 / 展示数
$ctr = $data['impressions'] > 0 ? number_format(($data['clicks'] / $data['impressions']) * 100, 2) . '%' : '-';
// Conversion Rate 的计算:转换率 = 购买数 / 点击数
$conversion_rate = $data['clicks'] > 0 ? round(($data['purchases'] / $data['clicks']) * 100, 2) . '%' : '-';
// Net Profit 的计算:净利润 = 收入 - 总成本
$net_profit = ($data['revenue'] - $data['total_cost']) >= 0 ? '+$' . number_format($data['revenue'] - $data['total_cost'], 2) : '-$' . number_format($data['revenue'] - $data['total_cost'], 2);
// Net Profit Margin 的计算:净利润率 = 净利润 / 收入
$net_profit_margin = $data['revenue'] > 0 ? round(($data['revenue'] - $data['total_cost']) / $data['revenue'], 2) * 100 . '%' : '-';
// Net Profit on Ad Spend 的计算:广告支出净利润 = (收入 - 总成本) / 广告支出
$net_profit_on_ad_spend = $data['spend'] > 0 ? round(($data['revenue'] - $data['total_cost']) / $data['spend'], 2) : '-';
// 计算 ROAS, CTR 和其他需要的字段
$aggregatedUsers[$userId]['roas'] = $total_spend == 0 ? '-' : round($total_purchases_value / $total_spend, 2);
$aggregatedUsers[$userId]['amount_spend'] = '$' . number_format($total_spend, 2) ?: '$0.00';
$aggregatedUsers[$userId]['purchases_value'] = '$' . number_format($total_purchases_value, 2) ?: '$0.00';
$aggregatedUsers[$userId]['cost_per_purchase'] = $data['purchases'] == 0 ? '$0.00' : '$' . number_format($total_purchases_value / $data['purchases'], 2);
$aggregatedUsers[$userId]['revenue'] = '$' . number_format($total_revenue, 2) ?: '$0.00';
$aggregatedUsers[$userId]['total_cost'] = '$' . number_format($total_cost, 2) ?: '$0.00';
$aggregatedUsers[$userId]['conversion_rate'] = $total_impressions == 0 ? '-' : round(($total_clicks / $total_impressions) * 100, 2) . '%';
$aggregatedUsers[$userId]['ctr'] = $total_impressions == 0 ? '-' : round(($total_clicks / $total_impressions) * 100, 2) . '%';
$aggregatedUsers[$userId]['roas'] = $total_spend == 0 ? '-' : round($total_purchases_value / $total_spend, 2);
$aggregatedUsers[$userId]['amount_spend'] = '$' . number_format($total_spend, 2) ?: '$0.00';
$aggregatedUsers[$userId]['purchases_value'] = '$' . number_format($total_purchases_value, 2) ?: '$0.00';
$aggregatedUsers[$userId]['cost_per_purchase'] = $data['purchases'] == 0 ? '$0.00' : '$' . number_format($total_purchases_value / $data['purchases'], 2);
$aggregatedUsers[$userId]['revenue'] = '$' . number_format($total_revenue, 2) ?: '$0.00';
$aggregatedUsers[$userId]['total_cost'] = '$' . number_format($total_cost, 2) ?: '$0.00';
$aggregatedUsers[$userId]['conversion_rate'] = $conversion_rate;
$aggregatedUsers[$userId]['ctr'] = $ctr;
$aggregatedUsers[$userId]['net_profit'] = $net_profit;
$aggregatedUsers[$userId]['net_profit_margin'] = $net_profit_margin;
$aggregatedUsers[$userId]['net_profit_on_ad_spend'] = $net_profit_on_ad_spend;
}
// 如果 aggregatedUsers 为空,返回默认的数据结构
if (empty($aggregatedUsers)) {
$emptyUser = [];
$query = ThirdUser::alias('u')
->cache(false) // 强制不使用缓存
->leftJoin('bps.bps_third_user_advertiser a', "u.id = a.doc_")
->field('u.id as user_id,u.third_type,a.advertiser_name');
// 添加关键字过滤条件
$query->where(function ($query) use ($keyword, $platformType) {
// if ($keyword) {
// $query->where('a.advertiser_name', 'like', '%' . $keyword . '%');
// }
if ($platformType) {
$a = (int)$platformType;
$platformTypeNames = [1=>'facebook',2=>'google',3=>'tiktok'];
$query->where('u.third_type', '=', $platformTypeNames[$a]);
}
});
// 获取所有符合条件的数据(不分页)
$allUsers = $query->select()->toArray();
foreach ($allUsers as $user) {
if (in_array($user['user_id'], $userIds)) {
$emptyUser[] = [
'user_id' => $user['user_id'],
'platform_type' => (int)$platformType,
'advertiser_name' => $user['advertiser_name'],
'assisted_purchases' => 0,
'last_clicked_purchases' => 0,
'spend' => 0,
'impressions' => 0,
'clicks' => 0,
'adds_to_cart' => 0,
'cost_per_atc' => 0,
'purchases' => 0,
'purchases_value' => 0,
'revenue' => 0,
'total_cost' => 0,
'conversion_rate' => '-',
'net_profit' => '-',
'net_profit_margin' => '-',
'net_profit_on_ad_spend' => '-',
'ctr' => '-',
];
}
}
// dump($allUsers,'222');
return [
'pagination' => [
'startIndex' => ($page - 1) * $pageSize,
'maxResults' => $pageSize,
'count' => 0,
'pageNo' => $page,
'pageSize' => $pageSize,
'pages' => 0,
],
'statistics' => $statistics,
'data' => $emptyUser
];
}
// 使用 array_values 移除键名
$dataWithoutKeys = array_values($aggregatedUsers);
// 最终分页信息
@ -996,7 +1068,7 @@ class AdsInsightService
$dateObj = \DateTime::createFromFormat('Ymd', $dateStr);
// 获取 ISO 周格式
return $dateObj->format('o-W'). 'W';
return $dateObj->format('o-W') . 'W';
}
/**