定义dashboard的3个统计字段 Tiktok Shop Conversions、Tiktok Shop Conversion Value、TikTok GMV Max Ads
This commit is contained in:
parent
ee5e3b7bbd
commit
737d85ad4c
@ -37,10 +37,14 @@ class AdsDashboardService
|
||||
{
|
||||
// 1. 查询全部数据集
|
||||
$adcycleDataQuery = BpsAdInsight::alias('d');
|
||||
|
||||
$adRevenueQuery = BpsAdInsight::alias('d')
|
||||
->where('d.platform', 3); // 过滤 platform=3
|
||||
//dump($customerIds);
|
||||
// 2. 客户 ID 过滤(如果提供了)
|
||||
if (!empty($customerIds)) {
|
||||
$adcycleDataQuery->whereIn('d.account_id', $customerIds);
|
||||
$adRevenueQuery->whereIn('d.account_id', $customerIds);
|
||||
} else {
|
||||
return [
|
||||
'data' => [
|
||||
@ -98,6 +102,7 @@ class AdsDashboardService
|
||||
// 3. 时间范围筛选
|
||||
if ($startDate && $endDate) {
|
||||
$adcycleDataQuery->whereBetween('d.date', [$startDate, $endDate]);
|
||||
$adRevenueQuery->whereBetween('d.date', [$startDate, $endDate]);
|
||||
}
|
||||
|
||||
// 4. 获取数据并按平台和日期聚合
|
||||
@ -111,14 +116,23 @@ class AdsDashboardService
|
||||
COALESCE(SUM(d.adds_to_cart), 0) as adds_to_cart,
|
||||
COALESCE(SUM(d.purchases), 0) as purchases,
|
||||
COALESCE(SUM(d.purchases_all), 0) as purchases_all,
|
||||
COALESCE(SUM(d.tiktok_shop_conversion), 0) as tiktok_shop_conversion,
|
||||
COALESCE(SUM(d.platform_purchase), 0) as platform_purchase,
|
||||
COALESCE(SUM(d.purchases_value) / 1000000, 0) as purchases_value,
|
||||
COALESCE(SUM(d.platform_purchase_value) / 1000000, 0) as platform_purchase_value,
|
||||
COALESCE(SUM(d.tiktok_shop_conversion_value) / 1000000, 0) as tiktok_shop_conversion_value,
|
||||
COALESCE(SUM(d.revenue) / 1000000, 0) as revenue,
|
||||
COALESCE(SUM(d.total_cost) / 1000000, 0) as total_cost'
|
||||
)
|
||||
->group('d.platform') // 按平台和日期进行分组
|
||||
->select();
|
||||
// dump(ThinkDb::getLastSql());
|
||||
// 4. 获取tiktok数据并按 ad_id 分组,计算 revenue 的总和
|
||||
$adRevenueData = $adRevenueQuery->field(
|
||||
'd.ad_id, COALESCE(SUM(d.revenue) / 1000000, 0) as total_revenue' // 计算 revenue 的总和
|
||||
)
|
||||
->group('d.ad_id') // 按 ad_id 分组
|
||||
->select();
|
||||
// dump(ThinkDb::getLastSql());
|
||||
// 定义字段映射关系
|
||||
$fieldMapping = [
|
||||
@ -161,13 +175,13 @@ class AdsDashboardService
|
||||
'cpc' => 'cpc',
|
||||
'ctr' => 'ctr',
|
||||
'cpa' => 'cpa',
|
||||
'tiktok_shop_conversions' => '',
|
||||
'tiktok_shop_conversion_value' => '',
|
||||
'tiktok_shop_conversions' => 'tiktok_shop_conversion',
|
||||
'tiktok_shop_conversion_value' => 'tiktok_shop_conversion_value',
|
||||
'conversion_value' => 'platform_purchase_value',
|
||||
'web_conversion_value' => 'revenue',
|
||||
'purchases' => 'platform_purchase',
|
||||
'web_purchases' => 'purchases',
|
||||
'tiktok_gmv_max_ads' => '',
|
||||
'tiktok_gmv_max_ads' => 'max_ad_revenue',
|
||||
],
|
||||
];
|
||||
|
||||
@ -180,6 +194,15 @@ class AdsDashboardService
|
||||
],
|
||||
];
|
||||
|
||||
// 5. 计算最大的 total_revenue
|
||||
$maxAdRevenue = 0;
|
||||
foreach ($adRevenueData as $data) {
|
||||
if ($data['total_revenue'] > $maxAdRevenue) {
|
||||
$maxAdRevenue = $data['total_revenue'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 在 PHP 中计算指标
|
||||
foreach ($adcycleData as $data) {
|
||||
$platform = $data['platform']; // 获取平台类型
|
||||
@ -190,9 +213,11 @@ class AdsDashboardService
|
||||
$clicks = (int)$data['clicks'];
|
||||
$purchases = (int)$data['purchases'];
|
||||
$purchases_all = (int)$data['purchases_all'];
|
||||
$tiktok_shop_conversion = (int)$data['tiktok_shop_conversion'];
|
||||
$platform_purchase = (int)$data['platform_purchase'];
|
||||
$purchases_value = (float)$data['purchases_value'];
|
||||
$platform_purchase_value = (float)$data['platform_purchase_value'];
|
||||
$tiktok_shop_conversion_value = (float)$data['tiktok_shop_conversion_value'];
|
||||
|
||||
// 计算指标
|
||||
$roas = ($spend == 0) ? 0 : $revenue / $spend;
|
||||
@ -260,23 +285,32 @@ class AdsDashboardService
|
||||
case 'conversion_rate':
|
||||
$value = $conversionRate;
|
||||
break;
|
||||
case 'tiktok_shop_conversion':
|
||||
$value = number_format($tiktok_shop_conversion);
|
||||
break;
|
||||
case 'tiktok_shop_conversion_value':
|
||||
$value = '$' . number_format($tiktok_shop_conversion_value, 2);
|
||||
break;
|
||||
case 'max_ad_revenue':
|
||||
$value = '$' . number_format($maxAdRevenue, 2);
|
||||
break;
|
||||
case 'revenue_per_link_click':
|
||||
$value = '$' . number_format($revenuePerLinkClick, 2);
|
||||
break;
|
||||
case 'purchases':
|
||||
$value = $purchases;
|
||||
$value = number_format($purchases);
|
||||
break;
|
||||
case 'purchases_all':
|
||||
$value = $purchases_all;
|
||||
$value = number_format($purchases_all);
|
||||
break;
|
||||
case 'platform_purchase':
|
||||
$value = $platform_purchase;
|
||||
$value = number_format($platform_purchase);
|
||||
break;
|
||||
case 'clicks':
|
||||
$value = $clicks;
|
||||
$value = number_format($clicks);
|
||||
break;
|
||||
case 'impressions':
|
||||
$value = $impressions;
|
||||
$value = number_format($impressions);
|
||||
break;
|
||||
default:
|
||||
$value = '-';
|
||||
|
Loading…
Reference in New Issue
Block a user