76 lines
3.4 KiB
PHP
76 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Client;
|
|
use App\Models\GoogleAsset;
|
|
use App\Models\GoogleAssetMetric;
|
|
use App\Models\GoogleCampaign;
|
|
use App\Models\GoogleCampaignMetric;
|
|
use App\Models\GoogleClient;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Services\GoogleAdsService;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class GetGoogleAssetMetric extends Command
|
|
{
|
|
protected $signature = 'google-ads:get-asset-metrics';
|
|
protected $description = 'Get asset metrics from Google Ads';
|
|
public function handle()
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
$this->info("Fetching campaign details from Google Ads...");
|
|
$adsService = new GoogleAdsService();
|
|
$clients = Client::where('status', 'ENABLED')->where('customer_id', '4744166776')->get();
|
|
// $clients = Client::where('status', 'ENABLED')->get();
|
|
foreach ($clients as $client) {
|
|
$campaigns = GoogleCampaign::where('client_id', $client->id)->get();
|
|
if ($campaigns->isEmpty()) {
|
|
$this->info("No campaigns found for Client ID: {$client->customer_id}");
|
|
continue;
|
|
} else {
|
|
foreach ($campaigns as $campaign) {
|
|
$assets = GoogleAsset::where('google_campaign_id', $campaign->id)->get();
|
|
if (! empty($assets)) {
|
|
foreach ($assets as $asset) {
|
|
$metrics = $adsService->getAssetMetricsById($client->customer_id, $asset->asset_id, '1970-01-01');
|
|
if (! empty($metrics)) {
|
|
foreach ($metrics as $metric) {
|
|
GoogleAssetMetric::updateOrCreate(
|
|
[
|
|
'google_asset_id' => $asset->id,
|
|
'date' => $metric['date'],
|
|
],
|
|
[
|
|
'impressions' => $metric['impressions'],
|
|
'clicks' => $metric['clicks'],
|
|
'actual_spend' => $metric['cost'],
|
|
'conversions' => $metric['conversions'],
|
|
]
|
|
);
|
|
$this->info("Fetched Metric for Asset: {$asset->name} on Date: {$metric['date']} for Campaign: {$campaign->name}");
|
|
}
|
|
} else {
|
|
$this->info("No metrics found for Asset ID: {$asset->asset_id} in Campaign: {$campaign->name}");
|
|
}
|
|
}
|
|
} else {
|
|
$this->info("No assets found for Client ID: {$client->customer_id}, Campaign Name: {$campaign->name}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
Log::error('Error getting campaign details: '.$e->getMessage(), [
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
return 1;
|
|
}
|
|
|
|
}
|
|
}
|