diff --git a/products/workflows/backend/presentation/views/hog_flow.py b/products/workflows/backend/presentation/views/hog_flow.py index 1b5aa5efc0dd..a5ad712a5fe3 100644 --- a/products/workflows/backend/presentation/views/hog_flow.py +++ b/products/workflows/backend/presentation/views/hog_flow.py @@ -35,7 +35,7 @@ from rest_framework.response import Response from rest_framework.serializers import BaseSerializer -from posthog.schema import ProductKey +from posthog.schema import ProductIntentContext, ProductKey from posthog.hogql.compiler.bytecode import create_bytecode from posthog.hogql.context import HogQLContext @@ -57,10 +57,17 @@ from posthog.auth import InternalAPIAuthentication from posthog.clickhouse.query_tagging import Feature, tag_queries from posthog.dataclasses import frozen -from posthog.event_usage import AGENT_EVENT_SOURCES, EventSource, get_event_source, report_user_action +from posthog.event_usage import ( + AGENT_EVENT_SOURCES, + EventSource, + get_event_source, + get_request_analytics_properties, + report_user_action, +) from posthog.exceptions_capture import capture_exception from posthog.helpers.impersonation import is_impersonated from posthog.models import Team, User +from posthog.models.product_intent.product_intent import ProductIntent from posthog.models.property.parse import expand_cohort_properties, parse_property_group_data from posthog.permissions import ( AccessControlPermission, @@ -5123,6 +5130,20 @@ def _report_workflow_action( except Exception as e: logger.warning("Failed to capture workflow usage event", event=event, error=str(e)) + def _register_workflows_product_intent(self, workflow: Workflow) -> None: + if is_impersonated(self.request) or workflow.origin_product == HogFlowOriginProduct.LOOPS: + return + try: + ProductIntent.register( + team=self.team, + product_type=ProductKey.WORKFLOWS, + context=ProductIntentContext.WORKFLOW_CREATED, + user=cast(User, self.request.user), + metadata=get_request_analytics_properties(self.request), + ) + except Exception as e: + capture_exception(e) + def perform_create(self, serializer): if self._is_mcp_request(self.request) and serializer.validated_data.get("status") == HogFlowState.ACTIVE: raise exceptions.ValidationError( @@ -5136,6 +5157,7 @@ def perform_create(self, serializer): validated_data=serializer.validated_data, ) serializer.instance = self._written_workflow(result) + self._register_workflows_product_intent(serializer.instance) self._log_activity(serializer.instance.id, serializer.instance.name, "created", detail_type="standard") self._emit_resource_edited(serializer.instance) diff --git a/products/workflows/backend/tests/api/test_hog_flow.py b/products/workflows/backend/tests/api/test_hog_flow.py index 74625ddb857a..7b90b51a0078 100644 --- a/products/workflows/backend/tests/api/test_hog_flow.py +++ b/products/workflows/backend/tests/api/test_hog_flow.py @@ -23,6 +23,7 @@ from posthog.models.activity_logging.activity_log import ActivityLog from posthog.models.integration import Integration from posthog.models.personal_api_key import PersonalAPIKey +from posthog.models.product_intent.product_intent import ProductIntent from posthog.models.utils import generate_random_token_personal, hash_key_value from posthog.test.fixtures import create_app_metric2 @@ -5563,6 +5564,53 @@ def _get_hog_flow_activity(self, flow_id: Optional[str] = None) -> list: assert activity.status_code == status.HTTP_200_OK return activity.json().get("results") + def _post_webhook_hog_flow(self, **extra_fields: Any): + hog_flow, _ = self._create_hog_flow_with_action( + { + "template_id": "template-webhook", + "inputs": {"url": {"value": "https://example.com"}}, + } + ) + return self.client.post(f"/api/projects/{self.team.id}/hog_flows", {**hog_flow, **extra_fields}) + + def _has_workflows_product_intent(self) -> bool: + return ProductIntent.objects.filter(team=self.team, product_type="workflows").exists() + + def test_create_hog_flow_registers_workflows_product_intent(self): + response = self._post_webhook_hog_flow() + assert response.status_code == status.HTTP_201_CREATED, response.json() + + intent = ProductIntent.objects.get(team=self.team, product_type="workflows") + assert intent.contexts == {"workflow_created": 1} + + def test_create_loop_registers_no_workflows_product_intent(self): + response = self._post_webhook_hog_flow(origin_product="loops") + + assert response.status_code == status.HTTP_201_CREATED, response.json() + assert not self._has_workflows_product_intent() + + def test_create_hog_flow_from_impersonated_session_registers_no_workflows_product_intent(self): + with patch("products.workflows.backend.presentation.views.hog_flow.is_impersonated", return_value=True): + response = self._post_webhook_hog_flow() + + assert response.status_code == status.HTTP_201_CREATED, response.json() + assert not self._has_workflows_product_intent() + + def test_create_hog_flow_reports_a_failed_workflows_product_intent_to_error_tracking(self): + registration_error = RuntimeError("intent store unavailable") + with ( + patch( + "products.workflows.backend.presentation.views.hog_flow.ProductIntent.register", + side_effect=registration_error, + ), + patch("products.workflows.backend.presentation.views.hog_flow.capture_exception") as capture_exception, + ): + response = self._post_webhook_hog_flow() + + assert response.status_code == status.HTTP_201_CREATED, response.json() + assert HogFlow.objects.filter(team=self.team, id=response.json()["id"]).exists() + capture_exception.assert_any_call(registration_error) + def test_create_hog_flow_logs_activity(self): hog_flow, _ = self._create_hog_flow_with_action( { diff --git a/products/workflows/frontend/WorkflowsScene.tsx b/products/workflows/frontend/WorkflowsScene.tsx index e9f4a2b3040c..ef56dbaf5739 100644 --- a/products/workflows/frontend/WorkflowsScene.tsx +++ b/products/workflows/frontend/WorkflowsScene.tsx @@ -9,7 +9,6 @@ import { useFeatureFlag } from 'lib/hooks/useFeatureFlag' import { LemonTabs } from 'lib/lemon-ui/LemonTabs' import { trackedActionToUrl } from 'lib/logic/scenes/trackedActionToUrl' import { removeProjectIdIfPresent } from 'lib/utils/kea-router' -import { addProductIntent } from 'lib/utils/product-intents' import { capitalizeFirstLetter } from 'lib/utils/strings' import { sceneConfigurations } from 'scenes/scenes' import { Scene, SceneExport } from 'scenes/sceneTypes' @@ -17,7 +16,7 @@ import { urls } from 'scenes/urls' import { SceneContent } from '~/layout/scenes/components/SceneContent' import { SceneTitleSection } from '~/layout/scenes/components/SceneTitleSection' -import { ProductIntentContext, ProductKey } from '~/queries/schema/schema-general' +import { ProductKey } from '~/queries/schema/schema-general' import { AccessControlLevel, AccessControlResourceType, Breadcrumb } from '~/types' import { EmailSuspensionBanner } from './EmailSuspensionBanner' @@ -200,10 +199,6 @@ export function WorkflowsScene(props: WorkflowsSceneProps = {}): JSX.Element { } const showsNewWorkflowButton = currentTab === 'workflows' || currentTab === 'templates' || currentTab === 'calendar' const onNewWorkflow = (): void => { - void addProductIntent({ - product_type: ProductKey.WORKFLOWS, - intent_context: ProductIntentContext.WORKFLOW_CREATED, - }) // Open the template chooser on the type the list shows, so a person on the // Messaging tab sees messaging templates first. setTypeFilter(currentTab === 'workflows' ? templateTypeForListType(filters.type) : 'all')