: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
from typing import cast
from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_field
from rest_framework import serializers, status, viewsets
from rest_framework.decorators import action
from rest_framework.exceptions import NotFound, ValidationError
from rest_framework.request import Request
from rest_framework.response import Response
from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.models.team.team import Team
from posthog.models.user import User
from products.access_control.backend.facade.user_access_control import UserAccessControl
from products.growth.backend.models import ProductPushCampaign
from products.growth.backend.product_push.selection import project_uses_product, resolve_product_path
class ProductPushCampaignSerializer(serializers.ModelSerializer):
id = serializers.UUIDField(
read_only=True,
help_text="Campaign id. Stable for the campaign's lifetime — key per-user dismissal state on it.",
)
product_key = serializers.CharField(
read_only=True,
help_text="ProductKey value of the product being pushed (e.g. 'session_replay').",
)
product_path = serializers.SerializerMethodField(
help_text="Sidebar path of the pushed product in the product catalog, for display resolution. "
"Null when the key maps to no released catalog item.",
)
reason_text = serializers.CharField(
read_only=True,
allow_null=True,
help_text="Custom promo copy written by the TAM. Null means the client should use its default copy.",
)
started_at = serializers.DateTimeField(read_only=True, help_text="When this campaign started.")
ends_at = serializers.DateTimeField(
read_only=True, allow_null=True, help_text="When this campaign is planned to end."
)
class Meta:
model = ProductPushCampaign
fields = ["id", "product_key", "product_path", "reason_text", "started_at", "ends_at"]
read_only_fields = fields
@extend_schema_field(serializers.CharField(allow_null=True))
def get_product_path(self, campaign: ProductPushCampaign) -> str | None:
return resolve_product_path(campaign.product_key)
class ProductPushCampaignViewSet(TeamAndOrgViewSetMixin, viewsets.GenericViewSet):
"""Read-only view of an organization's product push campaign state.
The organization_id parent lookup scopes the queryset; org membership is
enforced by the mixin's default permissions.
"""
scope_object = "INTERNAL"
serializer_class = ProductPushCampaignSerializer
queryset = ProductPushCampaign.objects.all()
@extend_schema(
description="The organization's currently active product push campaign. 204 when no campaign is "
"active, or when the given project already uses the campaign's product.",
parameters=[
OpenApiParameter(
name="team_id",
type=int,
location=OpenApiParameter.QUERY,
required=False,
description="Team id of the project the caller is viewing. When that project already uses "
"the campaign's product, the response is 204 so the promo isn't shown there.",
)
],
responses={200: ProductPushCampaignSerializer, 204: None, 404: None},
)
@action(detail=False, methods=["GET"])
def active(self, request: Request, **kwargs) -> Response:
campaign = self.get_queryset().filter(status=ProductPushCampaign.Status.ACTIVE).order_by("-started_at").first()
if campaign is None:
return Response(status=status.HTTP_204_NO_CONTENT)
team_id_param = request.query_params.get("team_id")
if team_id_param is not None:
try:
team_id = int(team_id_param)
except ValueError:
raise ValidationError({"team_id": "Must be an integer team id."})
team = Team.objects.filter(id=team_id, organization=self.organization).first()
if team is None:
raise NotFound("Team not found.")
# This is an org-scoped viewset, so the mixin's user_access_control has no team context.
# Build a team-scoped one to check per-project access.
team_access_control = UserAccessControl(user=cast(User, request.user), team=team)
if not team_access_control.check_access_level_for_object(team, "member"):
raise NotFound("Team not found.")
if project_uses_product(team.project_id, campaign.product_key, team.organization_id):
return Response(status=status.HTTP_204_NO_CONTENT)
return Response(self.get_serializer(campaign).data)