diff --git a/app/service/GoogleAdsReportService.php b/app/service/GoogleAdsReportService.php index 1aa9a57..caff200 100644 --- a/app/service/GoogleAdsReportService.php +++ b/app/service/GoogleAdsReportService.php @@ -106,14 +106,19 @@ class GoogleAdsReportService // 获取所有符合条件的数据(不分页) $allAds = $query->select()->toArray(); // 使用 toArray() 将对象转化为数组 + + $total_spend = array_sum(array_column($allAds, 'spend')); + $total_reach = array_sum(array_column($allAds, 'reach')); + $total_conversions_value = array_sum(array_column($allAds, 'conversions_value')); + // 汇总统计数据:基于所有数据而不是分页数据 $statistics = [ 'results' => '-', - 'reach' => array_sum(array_column($allAds, 'reach')) ?: 0, + 'reach' => $total_reach, + 'spend' => '$' . number_format($total_spend, 2) ?: '$0.00', 'revenue' => '-', - 'roas' => '-', + 'roas' => $total_spend == 0 ? '-' : number_format($total_conversions_value / $total_spend * 100, 2) . '%', 'profit' => '-', - 'spend' => '$' . number_format(array_sum(array_column($allAds, 'spend')), 2) ?: '$0.00', 'be_roas' => '-' ]; // 获取查询结果 @@ -150,6 +155,7 @@ class GoogleAdsReportService $assetUrl = $asset['asset_url']; } } + //ROAS = conversion_value / (cost_micros / 1000000) return [ 'id' => $item['ad_id'], @@ -160,7 +166,8 @@ class GoogleAdsReportService 'results' => $item['results'], 'reach' => $item['reach'], 'revenue' => $item['revenue'] == -1 ? '-' : $item['revenue'], - 'roas' => $item['roas'] == -1 ? '-' : $item['roas'], +// 'roas' => $item['roas'] == -1 ? '-' : $item['roas'], + 'roas' => $item['spend'] == 0 ? '-' : number_format($item['conversions_value'] / $item['spend'] * 100, 2) . '%', 'profit' => $item['profit'] == -1 ? '-' : $item['profit'], 'spend' => '$' . number_format($item['spend'], 2), 'campaign_name' => $item['campaign_name'], @@ -395,12 +402,12 @@ class GoogleAdsReportService $query = Campaign::alias('c') ->cache(false) // 强制不使用缓存 ->leftJoin('bps.bps_google_ad_day_data d', "c.campaign_id = d.campaign_id AND {$dateCondition}") - ->field('c.campaign_id, c.status as campaign_status, c.campaign_name, c.customer_id, - COALESCE(SUM(d.clicks), 0) as clicks, + ->field('c.campaign_id, c.status as campaign_status, c.campaign_name, c.customer_id, + COALESCE(SUM(d.clicks), 0) as clicks, COALESCE(SUM(d.cost_micros) / 1000000, 0) as spend, - COALESCE(SUM(d.conversions), 0) as results, - COALESCE(SUM(d.conversions_value), 0) as conversions_value, - COALESCE(SUM(d.impressions), 0) as reach, + COALESCE(SUM(d.conversions), 0) as results, + COALESCE(SUM(d.conversions_value), 0) as conversions_value, + COALESCE(SUM(d.impressions), 0) as reach, -1 as roas, -1 as be_roas, -1 as revenue, -1 as profit, -1 as delivery') ->group('c.campaign_id, c.status, c.customer_id, c.campaign_name') ->where('c.customer_id', 'in', $customerIds); // 添加 customerIds 条件 @@ -415,13 +422,16 @@ class GoogleAdsReportService // 获取所有符合条件的数据(不分页) $allCampaigns = $query->select()->toArray(); + $total_spend = array_sum(array_column($allCampaigns, 'spend')); + $total_reach = array_sum(array_column($allCampaigns, 'reach')); + $total_conversions_value = array_sum(array_column($allCampaigns, 'conversions_value')); // 汇总统计数据 $statistics = [ 'results' => '-', - 'reach' => array_sum(array_column($allCampaigns, 'reach')) ?: 0, - 'spend' => '$' . number_format(array_sum(array_column($allCampaigns, 'spend')), 2) ?: '$0.00', + 'reach' => $total_reach, + 'spend' => '$' . number_format($total_spend, 2) ?: '$0.00', 'revenue' => '-', - 'roas' => '-', + 'roas' => $total_spend == 0 ? '-' : number_format($total_conversions_value / $total_spend * 100, 2) . '%', 'profit' => '-', 'be_roas' => '-', ]; @@ -439,7 +449,8 @@ class GoogleAdsReportService 'results' => $item['results'], 'reach' => $item['reach'], 'revenue' => $item['revenue'] == -1 ? '-' : $item['revenue'], - 'roas' => $item['roas'] == -1 ? '-' : $item['roas'], +// 'roas' => $item['roas'] == -1 ? '-' : $item['roas'], + 'roas' => $item['spend'] == 0 ? '-' : number_format($item['conversions_value'] / $item['spend'] * 100, 2) . '%', 'profit' => $item['profit'] == -1 ? '-' : $item['profit'], 'spend' => '$' . number_format($item['spend'], 2), 'delivery' => self::$statusMapping[$item['campaign_status']], @@ -666,13 +677,17 @@ class GoogleAdsReportService // 获取所有符合条件的数据(不分页) $allAdGroups = $query->select()->toArray(); + $total_spend = array_sum(array_column($allAdGroups, 'spend')); + $total_reach = array_sum(array_column($allAdGroups, 'reach')); + $total_conversions_value = array_sum(array_column($allAdGroups, 'conversions_value')); + // 汇总统计数据:基于所有数据而不是分页数据 $statistics = [ 'results' => '-', - 'reach' => array_sum(array_column($allAdGroups, 'reach')) ?: 0, - 'spend' => '$' . number_format(array_sum(array_column($allAdGroups, 'spend')), 2) ?: '$0.00', + 'reach' => $total_reach, + 'spend' => '$' . number_format($total_spend, 2) ?: '$0.00', 'revenue' => '-', - 'roas' => '-', + 'roas' => $total_spend == 0 ? '-' : number_format($total_conversions_value / $total_spend * 100, 2) . '%', 'profit' => '-', 'be_roas' => '-' ]; @@ -694,7 +709,8 @@ class GoogleAdsReportService 'reach' => $item['reach'], 'spend' => '$' . number_format($item['spend'], 2), 'revenue' => $item['revenue'] == -1 ? '-' : $item['revenue'], - 'roas' => $item['roas'] == -1 ? '-' : $item['roas'], +// 'roas' => $item['roas'] == -1 ? '-' : $item['roas'], + 'roas' => $item['spend'] == 0 ? '-' : number_format($item['conversions_value'] / $item['spend'] * 100, 2) . '%', 'profit' => $item['profit'] == -1 ? '-' : $item['profit'], 'be_roas' => $item['be_roas'] == -1 ? '-' : $item['be_roas'], ];