74 lines
3.3 KiB
PHP
74 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Client;
|
|
use App\Models\GoogleAd;
|
|
use App\Models\GoogleAdGroup;
|
|
use App\Models\GoogleCampaign;
|
|
use App\Models\GoogleClient;
|
|
use App\Models\GoogleKeywords;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use App\Services\GoogleAdsService;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class GetGoogleKeywordsDetails extends Command
|
|
{
|
|
protected $signature = 'google-ads:get-keywords-details';
|
|
protected $description = 'Get keywords 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')->where('customer_id', '2048068576')->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) {
|
|
$adGroups = GoogleAdGroup::where('google_campaign_id', $campaign->id)->get();
|
|
if (! empty($adGroups)) {
|
|
foreach ($adGroups as $adGroup) {
|
|
$keywords = $adsService->listKeywordsByAdGroupId($client->customer_id, $adGroup->ad_group_id);
|
|
if (! empty($keywords)) {
|
|
foreach ($keywords as $keyword) {
|
|
GoogleKeywords::updateOrCreate(
|
|
[
|
|
'google_ad_group_id' => $adGroup->id,
|
|
'keyword_id' => $keyword['keyword_id'],
|
|
],
|
|
[
|
|
'text' => $keyword['text'],
|
|
'match_type' => $keyword['match_type'],
|
|
'status' => $keyword['status'],
|
|
]
|
|
);
|
|
$this->info("Fetched Keyword: {$keyword['text']} (Match Type: {$keyword['match_type']}) for Ad Group: {$adGroup->name} in Campaign: {$campaign->name}");
|
|
}
|
|
} else {
|
|
$this->info("No keywords found for Client ID: {$client->customer_id}, Ad Group Name: {$adGroup->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;
|
|
}
|
|
|
|
}
|
|
}
|