75 lines
3.6 KiB
PHP
75 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\Client;
|
|
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 GetGoogleCampaignMetric extends Command
|
|
{
|
|
protected $signature = 'google-ads:get-campaign-metric';
|
|
protected $description = 'Get campaign metric 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 = 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
|
|
$metrics = $adsService->listCampaignsMetricsById($client->customer_id, $campaign->campaign_id, '1970-01-01');
|
|
// $metrics = $adsService->listCampaignsMetricsById($client->customer_id, $campaign->campaign_id);
|
|
if (! empty($metrics)) {
|
|
$this->info("Found metrics for Client ID: {$client->customer_id}, Campaign Name: {$campaign->name}");
|
|
foreach ($metrics as $metric) {
|
|
GoogleCampaignMetric::updateOrCreate(
|
|
[
|
|
'google_campaign_id' => $campaign->id,
|
|
'date' => $metric['date'],
|
|
'google_campaign_metric_id' => $metric['id']
|
|
],
|
|
[
|
|
// 'google_campaign_metric_id' => $metric['id'],
|
|
'impressions' => $metric['impressions'],
|
|
'date' => $metric['date'],
|
|
'clicks' => $metric['clicks'],
|
|
'daily_budget' => $metric['daily_budget'],
|
|
'actual_spend' => $metric['actual_spend'],
|
|
'conversions' => $metric['conversions'],
|
|
'interactions' => $metric['interactions'],
|
|
]
|
|
);
|
|
$this->info("Client ID: {$client->customer_id}, Campaign Name: {$campaign->name},Date: {$metric['date']} , Impressions: {$metric['impressions']}, Clicks: {$metric['clicks']}, Cost: {$metric['actual_spend']}");
|
|
}
|
|
} else {
|
|
$this->info("No metrics 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;
|
|
}
|
|
|
|
}
|
|
}
|