From e6877d34365e314f0dc46e9d530382bac090735a Mon Sep 17 00:00:00 2001 From: Raquel Smith Date: Tue, 29 Oct 2024 09:52:29 -0700 Subject: [PATCH] feat: record non-onboarding product intents for data warehouse (#25859) Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> --- .../data-warehouse/new/sourceWizardLogic.tsx | 7 +++++ frontend/src/scenes/teamActivityDescriber.tsx | 1 + frontend/src/scenes/teamLogic.tsx | 3 +- frontend/src/types.ts | 7 +++++ posthog/api/project.py | 24 ++++++++++++++-- posthog/api/team.py | 28 +++++++++++++++---- .../api/test/__snapshots__/test_api_docs.ambr | 2 +- .../api/test/__snapshots__/test_decide.ambr | 3 +- posthog/api/test/test_team.py | 9 ++++++ 9 files changed, 73 insertions(+), 11 deletions(-) diff --git a/frontend/src/scenes/data-warehouse/new/sourceWizardLogic.tsx b/frontend/src/scenes/data-warehouse/new/sourceWizardLogic.tsx index 2d8bba2f256..95a525987b8 100644 --- a/frontend/src/scenes/data-warehouse/new/sourceWizardLogic.tsx +++ b/frontend/src/scenes/data-warehouse/new/sourceWizardLogic.tsx @@ -6,6 +6,7 @@ import api from 'lib/api' import posthog from 'posthog-js' import { preflightLogic } from 'scenes/PreflightCheck/preflightLogic' import { Scene } from 'scenes/sceneTypes' +import { teamLogic } from 'scenes/teamLogic' import { urls } from 'scenes/urls' import { @@ -16,6 +17,7 @@ import { manualLinkSources, ManualLinkSourceType, PipelineTab, + ProductKey, SourceConfig, SourceFieldConfig, } from '~/types' @@ -731,6 +733,8 @@ export const sourceWizardLogic = kea([ ['resetTable', 'createTableSuccess'], dataWarehouseSettingsLogic, ['loadSources'], + teamLogic, + ['addProductIntent'], ], }), reducers({ @@ -1129,6 +1133,9 @@ export const sourceWizardLogic = kea([ setManualLinkingProvider: () => { actions.onNext() }, + selectConnector: () => { + actions.addProductIntent({ product_type: ProductKey.DATA_WAREHOUSE, intent_context: 'selected connector' }) + }, })), urlToAction(({ actions }) => ({ '/data-warehouse/:kind/redirect': ({ kind = '' }, searchParams) => { diff --git a/frontend/src/scenes/teamActivityDescriber.tsx b/frontend/src/scenes/teamActivityDescriber.tsx index 7c1a5cd22e4..e3c4f9d2705 100644 --- a/frontend/src/scenes/teamActivityDescriber.tsx +++ b/frontend/src/scenes/teamActivityDescriber.tsx @@ -347,6 +347,7 @@ const teamActionsMapping: Record< updated_at: () => null, uuid: () => null, live_events_token: () => null, + product_intents: () => null, } function nameAndLink(logItem?: ActivityLogItem): JSX.Element { diff --git a/frontend/src/scenes/teamLogic.tsx b/frontend/src/scenes/teamLogic.tsx index 3f6d1022705..0e6754609ca 100644 --- a/frontend/src/scenes/teamLogic.tsx +++ b/frontend/src/scenes/teamLogic.tsx @@ -145,13 +145,14 @@ export const teamLogic = kea([ resetToken: async () => await api.update(`api/environments/${values.currentTeamId}/reset_token`, {}), addProductIntent: async ({ product_type, + intent_context, }: { product_type: ProductKey intent_context?: string | null }) => await api.update(`api/environments/${values.currentTeamId}/add_product_intent`, { product_type, - intent_context: null, + intent_context: intent_context ?? undefined, }), recordProductIntentOnboardingComplete: async ({ product_type }: { product_type: ProductKey }) => await api.update(`api/environments/${values.currentTeamId}/complete_product_onboarding`, { diff --git a/frontend/src/types.ts b/frontend/src/types.ts index 3d08ee1fb75..6b78307b817 100644 --- a/frontend/src/types.ts +++ b/frontend/src/types.ts @@ -545,6 +545,13 @@ export interface TeamType extends TeamBasicType { extra_settings?: Record modifiers?: HogQLQueryModifiers default_modifiers?: HogQLQueryModifiers + product_intents?: ProductIntentType[] +} + +export interface ProductIntentType { + product_type: string + created_at: string + onboarding_completed_at?: string } // This type would be more correct without `Partial`, but it's only used in the shared dashboard/insight diff --git a/posthog/api/project.py b/posthog/api/project.py index 0c60c7e649a..8efca11b219 100644 --- a/posthog/api/project.py +++ b/posthog/api/project.py @@ -44,7 +44,11 @@ from posthog.permissions import ( TeamMemberStrictManagementPermission, ) from posthog.user_permissions import UserPermissions, UserPermissionsSerializerMixin -from posthog.utils import get_ip_address, get_week_start_for_country_code +from posthog.utils import ( + get_instance_realm, + get_ip_address, + get_week_start_for_country_code, +) class ProjectSerializer(serializers.ModelSerializer): @@ -195,7 +199,9 @@ class ProjectBackwardCompatSerializer(ProjectBackwardCompatBasicSerializer, User def get_product_intents(self, obj): project = obj team = project.passthrough_team - return ProductIntent.objects.filter(team=team).values("product_type", "created_at", "onboarding_completed_at") + return ProductIntent.objects.filter(team=team).values( + "product_type", "created_at", "onboarding_completed_at", "updated_at" + ) @staticmethod def validate_session_recording_linked_flag(value) -> dict | None: @@ -572,7 +578,7 @@ class ProjectViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet): product_intent.updated_at = datetime.now(tz=UTC) product_intent.save() - if created and isinstance(user, User): + if isinstance(user, User): report_user_action( user, "user showed product intent", @@ -582,6 +588,10 @@ class ProjectViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet): "$current_url": current_url, "$session_id": session_id, "intent_context": request.data.get("intent_context"), + "is_first_intent_for_product": created, + "intent_created_at": product_intent.created_at, + "intent_updated_at": product_intent.updated_at, + "realm": get_instance_realm(), }, team=team, ) @@ -612,6 +622,10 @@ class ProjectViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet): "$current_url": current_url, "$session_id": session_id, "intent_context": request.data.get("intent_context"), + "is_first_intent_for_product": created, + "intent_created_at": product_intent.created_at, + "intent_updated_at": product_intent.updated_at, + "realm": get_instance_realm(), }, team=team, ) @@ -626,6 +640,10 @@ class ProjectViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet): "product_key": product_type, "$current_url": current_url, "$session_id": session_id, + "intent_context": request.data.get("intent_context"), + "intent_created_at": product_intent.created_at, + "intent_updated_at": product_intent.updated_at, + "realm": get_instance_realm(), }, team=team, ) diff --git a/posthog/api/team.py b/posthog/api/team.py index b92f3acaf35..148471919cb 100644 --- a/posthog/api/team.py +++ b/posthog/api/team.py @@ -6,17 +6,17 @@ from uuid import UUID from django.shortcuts import get_object_or_404 from loginas.utils import is_impersonated_session -from posthog.auth import PersonalAPIKeyAuthentication -from posthog.jwt import PosthogJwtAudience, encode_jwt from rest_framework import exceptions, request, response, serializers, viewsets from rest_framework.permissions import BasePermission, IsAuthenticated from posthog.api.routing import TeamAndOrgViewSetMixin from posthog.api.shared import TeamBasicSerializer from posthog.api.utils import action +from posthog.auth import PersonalAPIKeyAuthentication from posthog.constants import AvailableFeature from posthog.event_usage import report_user_action from posthog.geoip import get_geoip_properties +from posthog.jwt import PosthogJwtAudience, encode_jwt from posthog.models import ProductIntent, Team, User from posthog.models.activity_logging.activity_log import ( Detail, @@ -43,7 +43,11 @@ from posthog.permissions import ( get_organization_from_view, ) from posthog.user_permissions import UserPermissions, UserPermissionsSerializerMixin -from posthog.utils import get_ip_address, get_week_start_for_country_code +from posthog.utils import ( + get_instance_realm, + get_ip_address, + get_week_start_for_country_code, +) class PremiumMultiProjectPermissions(BasePermission): # TODO: Rename to include "Env" in name @@ -211,7 +215,9 @@ class TeamSerializer(serializers.ModelSerializer, UserPermissionsSerializerMixin ) def get_product_intents(self, obj): - return ProductIntent.objects.filter(team=obj).values("product_type", "created_at", "onboarding_completed_at") + return ProductIntent.objects.filter(team=obj).values( + "product_type", "created_at", "onboarding_completed_at", "updated_at" + ) @staticmethod def validate_session_recording_linked_flag(value) -> dict | None: @@ -582,7 +588,7 @@ class TeamViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet): product_intent.updated_at = datetime.now(tz=UTC) product_intent.save() - if created and isinstance(user, User): + if isinstance(user, User): report_user_action( user, "user showed product intent", @@ -592,6 +598,10 @@ class TeamViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet): "$current_url": current_url, "$session_id": session_id, "intent_context": request.data.get("intent_context"), + "is_first_intent_for_product": created, + "intent_created_at": product_intent.created_at, + "intent_updated_at": product_intent.updated_at, + "realm": get_instance_realm(), }, team=team, ) @@ -621,6 +631,10 @@ class TeamViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet): "$current_url": current_url, "$session_id": session_id, "intent_context": request.data.get("intent_context"), + "is_first_intent_for_product": created, + "intent_created_at": product_intent.created_at, + "intent_updated_at": product_intent.updated_at, + "realm": get_instance_realm(), }, team=team, ) @@ -635,6 +649,10 @@ class TeamViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet): "product_key": product_type, "$current_url": current_url, "$session_id": session_id, + "intent_context": request.data.get("intent_context"), + "intent_created_at": product_intent.created_at, + "intent_updated_at": product_intent.updated_at, + "realm": get_instance_realm(), }, team=team, ) diff --git a/posthog/api/test/__snapshots__/test_api_docs.ambr b/posthog/api/test/__snapshots__/test_api_docs.ambr index a5f9b394809..6ef31c65301 100644 --- a/posthog/api/test/__snapshots__/test_api_docs.ambr +++ b/posthog/api/test/__snapshots__/test_api_docs.ambr @@ -97,8 +97,8 @@ '/home/runner/work/posthog/posthog/posthog/api/survey.py: Warning [SurveyViewSet > SurveySerializer]: unable to resolve type hint for function "get_conditions". Consider using a type hint or @extend_schema_field. Defaulting to string.', '/home/runner/work/posthog/posthog/posthog/api/web_experiment.py: Warning [WebExperimentViewSet]: could not derive type of path parameter "project_id" because model "posthog.models.web_experiment.WebExperiment" contained no such field. Consider annotating parameter with @extend_schema. Defaulting to "string".', 'Warning: encountered multiple names for the same choice set (HrefMatchingEnum). This may be unwanted even though the generated schema is technically correct. Add an entry to ENUM_NAME_OVERRIDES to fix the naming.', - 'Warning: enum naming encountered a non-optimally resolvable collision for fields named "kind". The same name has been used for multiple choice sets in multiple components. The collision was resolved with "KindCfaEnum". add an entry to ENUM_NAME_OVERRIDES to fix the naming.', 'Warning: enum naming encountered a non-optimally resolvable collision for fields named "kind". The same name has been used for multiple choice sets in multiple components. The collision was resolved with "Kind069Enum". add an entry to ENUM_NAME_OVERRIDES to fix the naming.', + 'Warning: enum naming encountered a non-optimally resolvable collision for fields named "kind". The same name has been used for multiple choice sets in multiple components. The collision was resolved with "KindCfaEnum". add an entry to ENUM_NAME_OVERRIDES to fix the naming.', 'Warning: enum naming encountered a non-optimally resolvable collision for fields named "type". The same name has been used for multiple choice sets in multiple components. The collision was resolved with "TypeF73Enum". add an entry to ENUM_NAME_OVERRIDES to fix the naming.', 'Warning: encountered multiple names for the same choice set (EffectivePrivilegeLevelEnum). This may be unwanted even though the generated schema is technically correct. Add an entry to ENUM_NAME_OVERRIDES to fix the naming.', 'Warning: encountered multiple names for the same choice set (MembershipLevelEnum). This may be unwanted even though the generated schema is technically correct. Add an entry to ENUM_NAME_OVERRIDES to fix the naming.', diff --git a/posthog/api/test/__snapshots__/test_decide.ambr b/posthog/api/test/__snapshots__/test_decide.ambr index 93ec860efb0..18fc2e400e3 100644 --- a/posthog/api/test/__snapshots__/test_decide.ambr +++ b/posthog/api/test/__snapshots__/test_decide.ambr @@ -523,7 +523,8 @@ ''' SELECT "posthog_productintent"."product_type", "posthog_productintent"."created_at", - "posthog_productintent"."onboarding_completed_at" + "posthog_productintent"."onboarding_completed_at", + "posthog_productintent"."updated_at" FROM "posthog_productintent" WHERE "posthog_productintent"."team_id" = 2 ''' diff --git a/posthog/api/test/test_team.py b/posthog/api/test/test_team.py index ba697d07369..3a881facfea 100644 --- a/posthog/api/test/test_team.py +++ b/posthog/api/test/test_team.py @@ -26,6 +26,7 @@ from posthog.models.utils import generate_random_token_personal from posthog.temporal.common.client import sync_connect from posthog.temporal.common.schedule import describe_schedule from posthog.test.base import APIBaseTest +from posthog.utils import get_instance_realm def team_api_test_factory(): @@ -1042,6 +1043,10 @@ def team_api_test_factory(): "$session_id": "test_session_id", "intent_context": "onboarding product selected", "$set_once": {"first_onboarding_product_selected": "product_analytics"}, + "is_first_intent_for_product": True, + "intent_created_at": datetime(2024, 1, 1, 0, 0, 0, tzinfo=UTC), + "intent_updated_at": datetime(2024, 1, 1, 0, 0, 0, tzinfo=UTC), + "realm": get_instance_realm(), }, team=self.team, ) @@ -1073,6 +1078,10 @@ def team_api_test_factory(): "product_key": "product_analytics", "$current_url": "https://posthogtest.com/my-url", "$session_id": "test_session_id", + "intent_context": None, + "intent_created_at": datetime(2024, 1, 1, 0, 0, 0, tzinfo=UTC), + "intent_updated_at": datetime(2024, 1, 5, 0, 0, 0, tzinfo=UTC), + "realm": get_instance_realm(), }, team=self.team, )