32 lines
804 B
PHP
32 lines
804 B
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
class GoogleAdsSpendService
|
|
{
|
|
public function __construct(
|
|
private readonly GoogleAdsService $adsService,
|
|
) {}
|
|
|
|
public function forDateRange(string $customerId, string $startDate, string $endDate): float
|
|
{
|
|
$spending = 0.0;
|
|
|
|
foreach ($this->adsService->listCampaigns($customerId) as $campaign) {
|
|
$metrics = $this->adsService->listCampaignsMetricsById(
|
|
$customerId,
|
|
(string) $campaign['id'],
|
|
$startDate,
|
|
$endDate,
|
|
);
|
|
|
|
$spending += array_sum(array_map(
|
|
fn (array $metric): float => (float) ($metric['actual_spend'] ?? 0),
|
|
$metrics,
|
|
));
|
|
}
|
|
|
|
return round($spending, 6);
|
|
}
|
|
}
|