88 lines
2.8 KiB
PHP
88 lines
2.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Social;
|
|
|
|
use App\Models\SocialAccount;
|
|
use App\Services\Social\Concerns\HasSocialHttpClient;
|
|
use Carbon\CarbonInterface;
|
|
use Illuminate\Http\Client\PendingRequest;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class FacebookAnalytics
|
|
{
|
|
use HasSocialHttpClient;
|
|
|
|
private string $baseUrl = 'https://graph.facebook.com/v20.0';
|
|
|
|
private string $accessToken;
|
|
|
|
public function getMetrics(SocialAccount $account, ?CarbonInterface $since = null, ?CarbonInterface $until = null): array
|
|
{
|
|
$since ??= now()->subDays(7);
|
|
$until ??= now();
|
|
|
|
$cacheKey = "analytics:facebook:{$account->id}:{$since->format('Y-m-d')}:{$until->format('Y-m-d')}";
|
|
$cacheTtl = app()->isProduction() ? 3600 : 1;
|
|
|
|
return Cache::remember($cacheKey, $cacheTtl, function () use ($account, $since, $until) {
|
|
return $this->fetchMetricsFromApi($account, $since, $until);
|
|
});
|
|
}
|
|
|
|
private function fetchMetricsFromApi(SocialAccount $account, CarbonInterface $since, CarbonInterface $until): array
|
|
{
|
|
$this->accessToken = $account->access_token;
|
|
|
|
$response = $this->getHttpClient()
|
|
->get("{$this->baseUrl}/{$account->platform_user_id}/insights", [
|
|
'metric' => 'page_impressions_unique,page_posts_impressions_unique,page_post_engagements,page_daily_follows,page_video_views',
|
|
'period' => 'day',
|
|
'since' => $since->startOfDay()->unix(),
|
|
'until' => $until->endOfDay()->unix(),
|
|
'access_token' => $this->accessToken,
|
|
]);
|
|
|
|
if ($response->failed()) {
|
|
Log::warning('Facebook page insights fetch failed', [
|
|
'body' => $this->redactResponseBody($response->body()),
|
|
]);
|
|
|
|
return [];
|
|
}
|
|
|
|
$data = data_get($response->json(), 'data', []);
|
|
$metrics = [];
|
|
|
|
foreach ($data as $metric) {
|
|
$name = data_get($metric, 'name');
|
|
$values = data_get($metric, 'values', []);
|
|
|
|
if (empty($values)) {
|
|
continue;
|
|
}
|
|
|
|
$total = collect($values)->sum('value');
|
|
|
|
$label = match ($name) {
|
|
'page_impressions_unique' => 'Page Impressions',
|
|
'page_posts_impressions_unique' => 'Posts Impressions',
|
|
'page_post_engagements' => 'Posts Engagement',
|
|
'page_daily_follows' => 'Page Followers',
|
|
'page_video_views' => 'Video Views',
|
|
default => ucfirst(str_replace('_', ' ', $name)),
|
|
};
|
|
|
|
$metrics[] = ['label' => $label, 'value' => $total];
|
|
}
|
|
|
|
return $metrics;
|
|
}
|
|
|
|
private function getHttpClient(): PendingRequest
|
|
{
|
|
return $this->socialHttp();
|
|
}
|
|
}
|