更新报告

This commit is contained in:
hgc 2025-01-03 17:07:04 +08:00
parent d1c91de640
commit 828ac82f09

View File

@ -88,7 +88,8 @@ 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')
-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) {
@ -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());
@ -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());
@ -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'],