54 lines
2.1 KiB
PHP
54 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Client;
|
|
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 GetGoogleCampaignDetails extends Command
|
|
{
|
|
protected $signature = 'google-ads:get-campaign-details';
|
|
protected $description = 'Get campaign 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();
|
|
$clients = Client::where('status', 'ENABLED')->where('customer_id', '4744166776')->get();
|
|
foreach ($clients as $client) {
|
|
$campaigns = $adsService->listCampaigns($client->customer_id);
|
|
foreach ($campaigns as $campaign) {
|
|
$campaign = GoogleCampaign::updateOrCreate(
|
|
['client_id' => $client->id, 'campaign_id' => $campaign['id']],
|
|
[
|
|
'campaign_id' => $campaign['id'],
|
|
'name' => $campaign['name'],
|
|
'status' => $campaign['status'],
|
|
'channel' => $campaign['channel'],
|
|
'start_date' => $campaign['start_date'],
|
|
'end_date' => $campaign['end_date'],
|
|
'sub_channel' => $campaign['sub_channel'],
|
|
]
|
|
);
|
|
$this->info("Client ID: {$client->customer_id}, Campaign Name: {$campaign['name']}, Status: {$campaign['status']}");
|
|
}
|
|
}
|
|
DB::commit();
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
Log::error('Error getting campaign details: '.$e->getMessage(), [
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
return 1;
|
|
}
|
|
|
|
}
|
|
}
|