Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

1011 B
Raw Blame
from __future__ import annotations

from typing import TYPE_CHECKING

import posthoganalytics

from posthog.permissions import _FORCE_ENABLED_FLAGS

if TYPE_CHECKING:
    from posthog.models.team import Team
    from posthog.models.user import User


def widget_flag_enabled(flag: str, *, team: Team, user: User | None = None) -> bool:
    """Match the existing in-app widget flag evaluation."""
    if flag in _FORCE_ENABLED_FLAGS:
        return True

    distinct_id = (user.distinct_id or str(user.uuid)) if user is not None else str(team.uuid)
    organization_id = str(team.organization_id)
    project_id = str(team.id)

    return bool(
        posthoganalytics.feature_enabled(
            flag,
            distinct_id,
            groups={"organization": organization_id, "project": project_id},
            group_properties={"organization": {"id": organization_id}, "project": {"id": project_id}},
            only_evaluate_locally=False,
            send_feature_flag_events=False,
        )
    )