Skip to content

Commit

Permalink
feat: record non-onboarding product intents for data warehouse (#25859)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
raquelmsmith and github-actions[bot] authored Oct 29, 2024
1 parent 30cfe53 commit e6877d3
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 11 deletions.
7 changes: 7 additions & 0 deletions frontend/src/scenes/data-warehouse/new/sourceWizardLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,6 +17,7 @@ import {
manualLinkSources,
ManualLinkSourceType,
PipelineTab,
ProductKey,
SourceConfig,
SourceFieldConfig,
} from '~/types'
Expand Down Expand Up @@ -731,6 +733,8 @@ export const sourceWizardLogic = kea<sourceWizardLogicType>([
['resetTable', 'createTableSuccess'],
dataWarehouseSettingsLogic,
['loadSources'],
teamLogic,
['addProductIntent'],
],
}),
reducers({
Expand Down Expand Up @@ -1129,6 +1133,9 @@ export const sourceWizardLogic = kea<sourceWizardLogicType>([
setManualLinkingProvider: () => {
actions.onNext()
},
selectConnector: () => {
actions.addProductIntent({ product_type: ProductKey.DATA_WAREHOUSE, intent_context: 'selected connector' })
},
})),
urlToAction(({ actions }) => ({
'/data-warehouse/:kind/redirect': ({ kind = '' }, searchParams) => {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/scenes/teamActivityDescriber.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/scenes/teamLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,14 @@ export const teamLogic = kea<teamLogicType>([
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`, {
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,13 @@ export interface TeamType extends TeamBasicType {
extra_settings?: Record<string, string | number | boolean | undefined>
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<TeamType>`, but it's only used in the shared dashboard/insight
Expand Down
24 changes: 21 additions & 3 deletions posthog/api/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@
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):
Expand Down Expand Up @@ -195,7 +199,9 @@ def get_live_events_token(self, project: Project) -> Optional[str]:
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:
Expand Down Expand Up @@ -572,7 +578,7 @@ def add_product_intent(self, request: request.Request, *args, **kwargs):
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",
Expand All @@ -582,6 +588,10 @@ def add_product_intent(self, request: request.Request, *args, **kwargs):
"$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,
)
Expand Down Expand Up @@ -612,6 +622,10 @@ def complete_product_onboarding(self, request: request.Request, *args, **kwargs)
"$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,
)
Expand All @@ -626,6 +640,10 @@ def complete_product_onboarding(self, request: request.Request, *args, **kwargs)
"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,
)
Expand Down
28 changes: 23 additions & 5 deletions posthog/api/team.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@

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,
Expand All @@ -43,7 +43,11 @@
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
Expand Down Expand Up @@ -211,7 +215,9 @@ def get_live_events_token(self, team: Team) -> Optional[str]:
)

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:
Expand Down Expand Up @@ -582,7 +588,7 @@ def add_product_intent(self, request: request.Request, *args, **kwargs):
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",
Expand All @@ -592,6 +598,10 @@ def add_product_intent(self, request: request.Request, *args, **kwargs):
"$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,
)
Expand Down Expand Up @@ -621,6 +631,10 @@ def complete_product_onboarding(self, request: request.Request, *args, **kwargs)
"$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,
)
Expand All @@ -635,6 +649,10 @@ def complete_product_onboarding(self, request: request.Request, *args, **kwargs)
"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,
)
Expand Down
2 changes: 1 addition & 1 deletion posthog/api/test/__snapshots__/test_api_docs.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
3 changes: 2 additions & 1 deletion posthog/api/test/__snapshots__/test_decide.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -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
'''
Expand Down
9 changes: 9 additions & 0 deletions posthog/api/test/test_team.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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():
Expand Down Expand Up @@ -1042,6 +1043,10 @@ def test_can_add_product_intent(
"$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,
)
Expand Down Expand Up @@ -1073,6 +1078,10 @@ def test_can_complete_product_onboarding(
"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,
)
Expand Down

0 comments on commit e6877d3

Please sign in to comment.