Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

1.5 KB
Raw Blame
<?php

declare(strict_types=1);

namespace App\Support\Analytics;

final class ChannelMetrics
{
    /** @var list<string> */
    public const array ORDER = [
        'followers', 'net_followers', 'posts', 'reactions', 'comments', 'engagement_rate', 'views', 'impressions',
        'shares', 'reposts', 'quotes', 'saves', 'clicks', 'follows_gained', 'reach', 'watch_time_minutes', 'average_watch_time_seconds',
    ];

    /** @var array<string, string> */
    public const array SORTS = [
        'reactions' => 'metric.reactions_count',
        'comments' => 'metric.comments_count',
        'engagement_rate' => '(metric.engagement_count * 1.0) / NULLIF(metric.exposure_count, 0)',
        'views' => 'metric.views_count',
        'impressions' => 'metric.impressions_count',
        'shares' => 'metric.shares_count',
        'saves' => 'metric.saves_count',
        'reach' => 'metric.reach_count',
    ];

    /** @return list<string> */
    public static function sortable(): array
    {
        return array_values(array_filter(self::ORDER, fn (string $metric): bool => array_key_exists($metric, self::SORTS)));
    }

    /** @param  list<string>  $available */
    public static function sortFor(?string $requested, array $available): string
    {
        $sortable = array_values(array_intersect(self::sortable(), $available));

        if ($requested !== null && in_array($requested, $sortable, true)) {
            return $requested;
        }

        return $sortable[0] ?? self::sortable()[0];
    }
}