diff --git a/app/service/GoogleAdsReportService.php b/app/service/GoogleAdsReportService.php index caff200..42b9ecc 100644 --- a/app/service/GoogleAdsReportService.php +++ b/app/service/GoogleAdsReportService.php @@ -907,6 +907,7 @@ class GoogleAdsReportService 'data' => [], 'chat_1_data' => [], // 返回按月汇总的 chat_data 'total' => 0, + 'statistics' => [], 'pagination' => [ 'startIndex' => 0, 'maxResults' => $pageSize, @@ -1034,8 +1035,7 @@ class GoogleAdsReportService if (!$adStatsCollection->isEmpty()) { // 如果该 ad_id 有对应的聚合数据,初始化资产汇总数据 if (!isset($assetSummaryData[$assetRelation->asset_id])) { - // 获取 asset 相关信息,通过模型查询 Asset - $asset = Asset::find($assetRelation->asset_id); // 根据 asset_id 查找对应的 Asset 数据 + $assetSummaryData[$assetRelation->asset_id] = [ 'asset_id' => $assetRelation->asset_id, @@ -1059,7 +1059,8 @@ class GoogleAdsReportService } else { $month = $adStats->month; // 格式化日期为 'YYYY-MM' } - + // 获取 asset 相关信息,通过模型查询 Asset + $asset = Asset::find($assetRelation->asset_id); // 根据 asset_id 查找对应的 Asset 数据 // 累加该 ad_id 的统计数据到对应的 asset_id 汇总 $assetSummaryData[$assetRelation->asset_id]['asset_name'] = $asset->asset_name; $assetSummaryData[$assetRelation->asset_id]['asset_type'] = $asset->asset_type; @@ -1085,14 +1086,16 @@ class GoogleAdsReportService } } } - +//return $assetSummaryData; // 5. 格式化输出数据 // 生成最终输出的数据 $resultData = []; $chat_data = []; + $statistics = $this->initializeStatistics(); // Initialize statistics before processing + foreach ($assetSummaryData as $assetId => $data) { // 计算 ROAS - $roas = $data['total_spend'] ? ($data['total_conversions_value'] / $data['total_spend']) * 100 : 0; + $roas = $data['total_spend'] ? ($data['total_conversions_value'] / $data['total_spend']) : 0; // 合并月度数据到总数据 $resultData[] = [ @@ -1102,7 +1105,7 @@ class GoogleAdsReportService 'creative_url' => $data['asset_url'], 'spend' => '$' . number_format($data['total_spend'], 2), 'purchase_value' => '-', - 'roas' => number_format($roas, 2) . '%', + 'roas' => number_format($roas, 2) . 'X', 'cpa' => '-', 'cpc_link_click' => '-', 'cpm' => '-', @@ -1145,6 +1148,10 @@ class GoogleAdsReportService $chat_data[$month]['total_spend'] += $monthlyData['spend']; $chat_data[$month]['total_conversions_value'] += $monthlyData['conversions_value']; + // Aggregate statistics for overall summary + $statistics['spend'] += $monthlyData['spend']; + $statistics['conversions_value'] += $monthlyData['conversions_value']; + } @@ -1157,11 +1164,15 @@ class GoogleAdsReportService $totalConversionsValue = $data['total_conversions_value']; // 如果有支出数据,计算 ROAS - $chat_data[$month]['roas'] = $totalSpend ? ($totalConversionsValue / $totalSpend) * 100 : 0; - // 格式化 ROAS 为百分比 - $chat_data[$month]['roas'] = number_format($chat_data[$month]['roas'], 2) . '%'; + $chat_data[$month]['roas'] = $totalSpend ? ($totalConversionsValue / $totalSpend) : 0; + // 格式化 ROAS 为倍数 + $chat_data[$month]['roas'] = round($chat_data[$month]['roas'], 2); } + // 计算整体的 ROAS + $statistics['spend'] = '$'.number_format($statistics['spend'], 2); + $statistics['roas'] = $statistics['spend'] > 0 ? ($statistics['conversions_value'] / $statistics['spend']) : 0; + // 返回分页数据 $totalItems = count($assetSummaryData); @@ -1181,8 +1192,40 @@ class GoogleAdsReportService 'pages' => $totalPages ], 'chat_1_data' => array_values($chat_data), // 返回按月汇总的 chat_data - 'data' => $resultDataPaginated + 'data' => $resultDataPaginated, + 'statistics' => $statistics ]; } + /** + * 初始化统计数据 + */ + private function initializeStatistics() + { + return [ + 'conversions_value' => 0, + 'spend' => 0, + 'purchase_value' => '-', // 可根据需求进一步计算 + 'roas' => 0, // 可以根据需要计算总体 ROAS + 'cpa' => '-', + 'cpc_link_click' => '-', + 'cpm' => '-', + 'cpc_all' => '-', + 'aov' => '-', + 'click_to_atc_ratio' => '-', + 'atc_to_purchase_ratio' => '-', + 'purchases' => '-', + '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' => '-' + // Add other stats as necessary + ]; + } }