: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
import { JSONContent } from '@tiptap/core'
import { IconMessage, IconQuestion } from '@posthog/icons'
import { uuid } from 'lib/utils/dom'
import {
PRODUCT_TOUR_STEP_WIDTHS,
ProductTourStep,
ProductTourStepTranslation,
ProductTourStepType,
ProductTourStepWidth,
ProductTourSurveyQuestion,
ProductTourSurveyQuestionType,
StepOrderVersion,
SurveyPosition,
} from '~/types'
export function resolveStepTranslation(step: ProductTourStep, lang: string | null): ProductTourStep {
if (!lang) {
return step
}
const t: ProductTourStepTranslation | undefined = step.translations?.[lang]
if (!t) {
return step
}
const resolved = { ...step }
if (t.content !== undefined) {
resolved.content = t.content
}
if (t.buttons && resolved.buttons) {
resolved.buttons = {
primary: resolved.buttons.primary && { ...resolved.buttons.primary, ...t.buttons.primary },
secondary: resolved.buttons.secondary && { ...resolved.buttons.secondary, ...t.buttons.secondary },
}
}
if (t.survey && resolved.survey) {
resolved.survey = { ...resolved.survey, ...t.survey }
}
return resolved
}
export const DEFAULT_RATING_QUESTION = 'How helpful was this tour?'
export const DEFAULT_OPEN_QUESTION = 'Any feedback on this tour?'
export function getStepIcon(type: ProductTourStepType): JSX.Element {
if (type === 'survey') {
return <IconQuestion className="w-3.5 h-3.5" />
}
return <IconMessage className="w-3.5 h-3.5" />
}
export function getStepLabel(type: ProductTourStepType): string {
if (type === 'survey') {
return 'Survey'
}
return 'Pop-up'
}
export function getDefaultStepContent(): JSONContent {
return {
type: 'doc',
content: [
{
type: 'heading',
attrs: { level: 1 },
content: [{ type: 'text', text: 'Step title' }],
},
{
type: 'paragraph',
content: [{ type: 'text', text: 'Describe what this element does...' }],
},
],
}
}
export function getExplainerStepContent(): JSONContent {
return {
type: 'doc',
content: [
{
type: 'heading',
attrs: { level: 2 },
content: [{ type: 'text', text: 'Welcome to Product Tours!' }],
},
{
type: 'paragraph',
content: [{ type: 'text', text: 'Here are some tips to get you going 🦔' }],
},
{
type: 'bulletList',
content: [
{
type: 'listItem',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Add an element to display this step as a tooltip',
},
],
},
],
},
{
type: 'listItem',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: 'Configure when & where to show the tour',
},
],
},
],
},
{
type: 'listItem',
content: [
{
type: 'paragraph',
content: [
{
type: 'text',
text: "Customize your tour's theme",
},
],
},
],
},
{
type: 'listItem',
content: [
{
type: 'paragraph',
content: [{ type: 'text', text: 'Add custom buttons' }],
},
],
},
],
},
],
}
}
export function getDefaultSurveyContent(type: ProductTourSurveyQuestionType): ProductTourSurveyQuestion {
const defaults: Partial<ProductTourSurveyQuestion> = {
backButtonText: 'Back',
submitButtonText: 'Submit',
}
if (type === 'rating') {
return {
...defaults,
type: 'rating',
questionText: DEFAULT_RATING_QUESTION,
display: 'emoji',
scale: 5,
lowerBoundLabel: 'Not at all',
upperBoundLabel: 'Very much',
}
}
return {
...defaults,
type: 'open',
questionText: DEFAULT_OPEN_QUESTION,
}
}
export function getStepTitle(step: ProductTourStep, index: number): string {
if (step.type === 'survey' && step.survey?.questionText) {
const text = step.survey.questionText
return text.length > 30 ? text.slice(0, 30) + '...' : text
}
if (step.content && typeof step.content === 'object') {
const doc = step.content as { content?: Array<{ content?: Array<{ text?: string }> }> }
const firstContent = doc.content?.[0]
if (firstContent?.content?.[0]?.text) {
const text = firstContent.content[0].text
return text.length > 30 ? text.slice(0, 30) + '...' : text
}
}
const typeLabel = getStepLabel(step.type)
return `${typeLabel} step ${index + 1}`
}
export function getWidthValue(maxWidth: ProductTourStep['maxWidth']): number {
if (typeof maxWidth === 'number') {
return maxWidth
}
if (maxWidth && maxWidth in PRODUCT_TOUR_STEP_WIDTHS) {
return PRODUCT_TOUR_STEP_WIDTHS[maxWidth as ProductTourStepWidth]
}
return PRODUCT_TOUR_STEP_WIDTHS.default
}
export function hasElementTarget(step: ProductTourStep): boolean {
if (step.elementTargeting === 'manual') {
return !!step.selector
} else if (step.elementTargeting === 'auto') {
return !!step.inferenceData
}
return false
}
export function hasIncompleteTargeting(step: ProductTourStep): boolean {
return step.elementTargeting !== undefined && !hasElementTarget(step)
}
export function createDefaultStep(type: ProductTourStepType): ProductTourStep {
const baseStep: ProductTourStep = {
id: uuid(),
type,
content: type === 'survey' ? null : getDefaultStepContent(),
progressionTrigger: 'button',
modalPosition: SurveyPosition.MiddleCenter,
}
if (type === 'survey') {
return { ...baseStep, survey: getDefaultSurveyContent('rating') }
}
return baseStep
}
function hasStepsChanged(currentSteps: ProductTourStep[], history: StepOrderVersion[] | undefined): boolean {
if (!history || history.length === 0) {
return true
}
const latestVersion = history[history.length - 1]
if (currentSteps.length !== latestVersion.steps.length) {
return true
}
return currentSteps.some((step, index) => step.id !== latestVersion.steps[index].id)
}
export function getUpdatedStepOrderHistory(
currentSteps: ProductTourStep[],
existingHistory: StepOrderVersion[] | undefined
): StepOrderVersion[] {
const history = existingHistory ? [...existingHistory] : []
if (hasStepsChanged(currentSteps, history)) {
history.push({
id: uuid(),
steps: currentSteps,
created_at: new Date().toISOString(),
})
}
return history
}