67 lines
2.7 KiB
PHP
67 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Client;
|
|
use App\Models\GoogleAdGroup;
|
|
use App\Models\GoogleCampaign;
|
|
use App\Models\GoogleClient;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Services\GoogleAdsService;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class GetGoogleAdGroupDetails extends Command
|
|
{
|
|
protected $signature = 'google-ads:get-adgroup-details';
|
|
protected $description = 'Get ad group details 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')->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) {
|
|
//Beginning of campaign
|
|
$adGroups = $adsService->listAdGroupsByCampaignId($client->customer_id, $campaign->campaign_id);
|
|
if (! empty($adGroups)) {
|
|
foreach ($adGroups as $adGroup) {
|
|
GoogleAdGroup::updateOrCreate(
|
|
[
|
|
'google_campaign_id' => $campaign->id,
|
|
'ad_group_id' => $adGroup['ad_group_id'],
|
|
],
|
|
[
|
|
'name' => $adGroup['ad_group_name'],
|
|
'status' => $adGroup['status'],
|
|
'type' => $adGroup['type'],
|
|
'cpc_bid_micros' => $adGroup['cpc_bid_micros'],
|
|
]
|
|
);
|
|
$this->info("Saved Ad Group: {$adGroup['ad_group_name']} for Campaign: {$campaign->name}");
|
|
}
|
|
} else {
|
|
$this->info("No ad group 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;
|
|
}
|
|
|
|
}
|
|
}
|