From 828ac82f09662cdd953525607661f6aaa06487e4 Mon Sep 17 00:00:00 2001 From: hgc Date: Fri, 3 Jan 2025 17:07:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=8A=A5=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/GoogleAdsReportService.php | 175 +++++++++++++++---------- 1 file changed, 105 insertions(+), 70 deletions(-) diff --git a/app/service/GoogleAdsReportService.php b/app/service/GoogleAdsReportService.php index d8b7363..25e99c0 100644 --- a/app/service/GoogleAdsReportService.php +++ b/app/service/GoogleAdsReportService.php @@ -32,24 +32,24 @@ class GoogleAdsReportService $query->whereIn('a.customer_id', $customerIds); // 添加 customer_id 的查询约束 } else { return [ - 'pagination' => [ - 'startIndex' => 0, - 'maxResults' => $pageSize, - 'count' => 0, - 'pageNo' => $page, - 'pageSize' => $pageSize, - 'pages' => 0, - ], - 'statistics' => [ - 'results' => '-', - 'reach' => '-', - 'spend' => '-', - 'revenue' => '-', - 'roas' => '-', - 'profit' => '-', - 'be_roas' => '-', - ], - 'data' => [], + 'pagination' => [ + 'startIndex' => 0, + 'maxResults' => $pageSize, + 'count' => 0, + 'pageNo' => $page, + 'pageSize' => $pageSize, + 'pages' => 0, + ], + 'statistics' => [ + 'results' => '-', + 'reach' => '-', + 'spend' => '-', + 'revenue' => '-', + 'roas' => '-', + 'profit' => '-', + 'be_roas' => '-', + ], + 'data' => [], ]; } @@ -88,8 +88,9 @@ class GoogleAdsReportService 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('a.ad_id, a.ad_name, a.status, a.customer_id, a.ad_group_id, g.ad_group_name, a.campaign_id, c.campaign_name') + -1 as roas, -1 as be_roas, -1 as revenue, -1 as profit, -1 as delivery, + a.metadata') // Include metadata field + ->group('a.ad_id, a.ad_name, a.status, a.customer_id, a.ad_group_id, g.ad_group_name, a.campaign_id, c.campaign_name') ->where(function ($query) use ($keyword) { if ($keyword) { $query->where('a.ad_name', 'like', '%' . $keyword . '%'); @@ -113,8 +114,40 @@ class GoogleAdsReportService // 确保转换为数值,并格式化 -1 为 "-" $result = array_map(function ($item) { + + // Extract square marketing images from metadata + // Extract square marketing images from metadata + $metadata = $item['metadata']; + +// 如果是对象,转换为数组 + if (is_object($metadata)) { + $metadata = (array)$metadata; + } + $imageUrls = isset($metadata['square_marketing_images']) ? $metadata['square_marketing_images'] : []; + + // Extract asset_id from the image URL + $assetId = 0; + if (!empty($imageUrls)) { + // Example: "customers/8452924576/assets/191936503309" + $imagePath = $imageUrls[0]; // Get the last element + preg_match('/assets\/(\d+)/', $imagePath, $matches); + if (isset($matches[1])) { + $assetId = $matches[1]; + } + } + // Query the asset_url from the bps_google_ads_asset table + $assetUrl = ''; + if ($assetId) { + $asset = Asset::find($assetId); + if ($asset) { + $assetUrl = $asset['asset_url']; + } + } + return [ 'id' => $item['ad_id'], + 'customer_id' => $item['customer_id'], + 'ad_group_id' => $item['ad_group_id'], 'name' => $item['ad_name'] ?: '-', // 默认值为 '-' 'status' => $item['ad_status'], 'results' => $item['results'], @@ -125,10 +158,10 @@ class GoogleAdsReportService 'spend' => number_format($item['spend'], 2), 'campaign_name' => $item['campaign_name'], 'ad_set_name' => $item['ad_group_name'], // Assuming ad_group_name as ad_set_name - 'delivery' => $item['delivery'] == -1 ? '-' : $item['delivery'], - 'delivery_status' => '活动', // Assuming active as '活动' + 'delivery' => $item['ad_status'], + 'delivery_status' => '-', // Assuming active as '活动' 'be_roas' => $item['be_roas'] == -1 ? '-' : $item['be_roas'], - 'image_urls' => [], // Add logic to populate image URLs if available + 'image_url' => $assetUrl, // Add logic to populate image URLs if available ]; }, $ads->items()); @@ -154,9 +187,9 @@ class GoogleAdsReportService ]; return [ - 'pagination' => $pagination, - 'statistics' => $statistics, - 'data' => $result, + 'pagination' => $pagination, + 'statistics' => $statistics, + 'data' => $result, ]; } @@ -300,24 +333,24 @@ class GoogleAdsReportService // 检查 customerIds 是否为空,直接返回空结构 if (empty($customerIds)) { return [ - 'pagination' => [ - 'startIndex' => 0, - 'maxResults' => $pageSize, - 'count' => 0, - 'pageNo' => $page, - 'pageSize' => $pageSize, - 'pages' => 0, - ], - 'statistics' => [ - 'results' => '-', - 'reach' => '-', - 'spend' => '-', - 'revenue' => '-', - 'roas' => '-', - 'profit' => '-', - 'be_roas' => '-', - ], - 'data' => [], + 'pagination' => [ + 'startIndex' => 0, + 'maxResults' => $pageSize, + 'count' => 0, + 'pageNo' => $page, + 'pageSize' => $pageSize, + 'pages' => 0, + ], + 'statistics' => [ + 'results' => '-', + 'reach' => '-', + 'spend' => '-', + 'revenue' => '-', + 'roas' => '-', + 'profit' => '-', + 'be_roas' => '-', + ], + 'data' => [], ]; } // 动态构建日期条件 @@ -389,6 +422,7 @@ class GoogleAdsReportService $result = array_map(function ($item) { return [ 'id' => $item['campaign_id'], + 'customer_id' => $item['customer_id'], 'name' => $item['campaign_name'] ?: '-', 'status' => $item['campaign_status'], 'results' => $item['results'], @@ -397,8 +431,8 @@ class GoogleAdsReportService 'roas' => $item['roas'] == -1 ? '-' : $item['roas'], 'profit' => $item['profit'] == -1 ? '-' : $item['profit'], 'spend' => number_format($item['spend'], 2), - 'delivery' => $item['delivery'] == -1 ? '-' : $item['delivery'], - 'delivery_status' => '活动', // 默认状态 + 'delivery' => $item['campaign_status'], + 'delivery_status' => '-', // 默认状态 'be_roas' => $item['be_roas'] == -1 ? '-' : $item['be_roas'], ]; }, $campaigns->items()); @@ -414,9 +448,9 @@ class GoogleAdsReportService ]; return [ - 'pagination' => $pagination, - 'statistics' => $statistics, - 'data' => $result, + 'pagination' => $pagination, + 'statistics' => $statistics, + 'data' => $result, ]; } @@ -615,24 +649,24 @@ class GoogleAdsReportService // 检查 customerIds 是否为空,直接返回空结构 if (empty($customerIds)) { return [ - 'pagination' => [ - 'startIndex' => 0, - 'maxResults' => $pageSize, - 'count' => 0, - 'pageNo' => $page, - 'pageSize' => $pageSize, - 'pages' => 0, - ], - 'statistics' => [ - 'results' => '-', - 'reach' => '-', - 'spend' => '-', - 'revenue' => '-', - 'roas' => '-', - 'profit' => '-', - 'be_roas' => '-', - ], - 'data' => [], + 'pagination' => [ + 'startIndex' => 0, + 'maxResults' => $pageSize, + 'count' => 0, + 'pageNo' => $page, + 'pageSize' => $pageSize, + 'pages' => 0, + ], + 'statistics' => [ + 'results' => '-', + 'reach' => '-', + 'spend' => '-', + 'revenue' => '-', + 'roas' => '-', + 'profit' => '-', + 'be_roas' => '-', + ], + 'data' => [], ]; } @@ -703,6 +737,7 @@ class GoogleAdsReportService $result = array_map(function ($item) { return [ 'id' => $item['ad_group_id'], + 'customer_id' => $item['customer_id'], 'name' => $item['ad_group_name'] ?: '-', 'status' => $item['ad_group_status'], 'campaign_name' => $item['campaign_name'], @@ -728,9 +763,9 @@ class GoogleAdsReportService // 返回结果 return [ - 'pagination' => $pagination, - 'statistics' => $statistics, - 'data' => $result, + 'pagination' => $pagination, + 'statistics' => $statistics, + 'data' => $result, ]; }