Skip to content

: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.

feat: Add export nudge to Web Analytics (#63822)

Jordan Mryyanfa12fc35538e
diff --git a/common/tailwind/tailwind.config.js b/common/tailwind/tailwind.config.js
index d740cac8b589..d88d90a828ae 100644
--- a/common/tailwind/tailwind.config.js
+++ b/common/tailwind/tailwind.config.js
@@ -649,6 +649,7 @@ const config = {
'fade-out-delayed': 'fade-out-delayed 5s ease-out forwards',
// Quick horizontal shake
shake: 'shake 0.5s ease-in-out',
+ 'slide-in-right': 'slide-in-right 0.28s cubic-bezier(0.16, 1, 0.3, 1)',
},
keyframes: {
'pulse-glow': {
@@ -670,6 +671,10 @@ const config = {
'60%': { transform: 'translateX(-2px)' },
'80%': { transform: 'translateX(2px)' },
},
+ 'slide-in-right': {
+ '0%': { opacity: '0', transform: 'translateX(calc(100% + 1.5rem))' },
+ '100%': { opacity: '1', transform: 'translateX(0)' },
+ },
},
colors: {
...deprecatedColors,
diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsx
index 7365404a3e6d..14646df26260 100644
--- a/frontend/src/lib/constants.tsx
+++ b/frontend/src/lib/constants.tsx
@@ -509,7 +509,7 @@ export const FEATURE_FLAGS = {
WEB_ANALYTICS_PRECOMPUTE_TOGGLE: 'web-analytics-precompute-toggle', // owner: @lricoy #team-web-analytics
WEB_ANALYTICS_REGIONS_MAP: 'web-analytics-regions-map', // owner: @jordanm-posthog #team-web-analytics
WEB_ANALYTICS_SESSION_PROPERTY_CHARTS: 'web-analytics-session-property-charts', // owner: @lricoy #team-web-analytics
- WEB_ANALYTICS_SHARE_NUDGE: 'web-analytics-share-nudge', // owner: @pauldambra #team-web-analytics multivariate=control,banner,prompt,button
+ WEB_ANALYTICS_SHARE_NUDGE_V2: 'web-analytics-share-nudge-v2', // owner: @jordanm-posthog #team-web-analytics multivariate=control,control_b,banner,export
WEB_ANALYTICS_TILE_HEADER_V2: 'web-analytics-tile-header-v2', // owner: @jordanm-posthog #team-web-analytics multivariate=control,test
WEB_ANALYTICS_TILE_SKELETONS: 'web-analytics-tile-skeletons', // owner: @jordanm-posthog #team-web-analytics
WEB_ANALYTICS_TILE_TOGGLES: 'web-analytics-tile-toggles', // owner: @lricoy #team-web-analytics
diff --git a/frontend/src/scenes/web-analytics/ShareNudgePrompt.tsx b/frontend/src/scenes/web-analytics/ShareNudgePrompt.tsx
index 34d2f2955cc5..219d64913637 100644
--- a/frontend/src/scenes/web-analytics/ShareNudgePrompt.tsx
+++ b/frontend/src/scenes/web-analytics/ShareNudgePrompt.tsx
@@ -8,33 +8,36 @@ import { copyToClipboard } from 'lib/utils/copyToClipboard'
import { shareNudgeLogic } from 'scenes/web-analytics/shareNudgeLogic'
export function ShareNudgePrompt(): JSX.Element | null {
- const { promptVisible, promptAnchor } = useValues(shareNudgeLogic)
+ const { promptVisible, promptSource } = useValues(shareNudgeLogic)
const { dismissForSession } = useActions(shareNudgeLogic)
- if (!promptVisible || !promptAnchor) {
+ if (!promptVisible) {
return null
}
- const left = Math.min(promptAnchor.x, window.innerWidth - 300)
- const top = Math.min(promptAnchor.y + 8, window.innerHeight - 120)
-
const handleShare = (): void => {
void copyToClipboard(window.location.href, 'link to share')
- posthog.capture('web analytics share link copied', { source: 'intent_prompt' })
+ posthog.capture('web analytics share link copied', { source: promptSource })
dismissForSession()
}
return (
<div
- className="z-top fixed flex items-center gap-2 rounded border bg-surface-primary p-2 shadow-md"
- style={{ left, top, maxWidth: 280 }}
+ className="animate-slide-in-right z-top fixed inset-y-0 right-6 my-auto flex h-fit w-[340px] max-w-[calc(100vw-2rem)] flex-col gap-2 rounded-lg border bg-surface-primary p-4 shadow-lg"
data-attr="web-analytics-share-nudge-prompt"
>
- <span className="text-sm">Share this with a colleague?</span>
- <LemonButton type="primary" size="xsmall" icon={<IconShare />} onClick={handleShare}>
- Copy link
- </LemonButton>
- <LemonButton size="xsmall" icon={<IconX />} tooltip="Dismiss" onClick={dismissForSession} />
+ <div className="flex items-start justify-between gap-2">
+ <h4 className="m-0 text-base font-semibold">Share this view with a colleague</h4>
+ <LemonButton size="xsmall" icon={<IconX />} tooltip="Dismiss" onClick={dismissForSession} />
+ </div>
+ <p className="m-0 text-sm text-muted">
+ Web analytics is better with your team. Send this exact view so they see what you see.
+ </p>
+ <div className="flex">
+ <LemonButton type="primary" icon={<IconShare />} onClick={handleShare}>
+ Copy link
+ </LemonButton>
+ </div>
</div>
)
}
diff --git a/frontend/src/scenes/web-analytics/WebAnalyticsExport.tsx b/frontend/src/scenes/web-analytics/WebAnalyticsExport.tsx
index 15820c60ee70..eb9ed7ab8c08 100644
--- a/frontend/src/scenes/web-analytics/WebAnalyticsExport.tsx
+++ b/frontend/src/scenes/web-analytics/WebAnalyticsExport.tsx
@@ -1,6 +1,10 @@
+import { useActions } from 'kea'
+
import { IconCopy } from '@posthog/icons'
import { LemonButton, LemonMenu } from '@posthog/lemon-ui'
+import { shareNudgeLogic } from 'scenes/web-analytics/shareNudgeLogic'
+
import { QuerySchema } from '~/queries/schema/schema-general'
import { ExporterFormat, InsightLogicProps } from '~/types'
@@ -14,6 +18,7 @@ interface WebAnalyticsExportProps {
export function WebAnalyticsExport({ query, insightProps }: WebAnalyticsExportProps): JSX.Element | null {
const adapter = useWebTileExportAdapter(query, insightProps)
+ const { exportTriggered } = useActions(shareNudgeLogic)
if (!adapter) {
return null
@@ -22,6 +27,7 @@ export function WebAnalyticsExport({ query, insightProps }: WebAnalyticsExportPr
const handleCopy = (format: ExporterFormat): void => {
const tableData = adapter.toTableData()
exportTableData(tableData, format)
+ exportTriggered()
}
return (
diff --git a/frontend/src/scenes/web-analytics/shareNudgeLogic.test.ts b/frontend/src/scenes/web-analytics/shareNudgeLogic.test.ts
new file mode 100644
index 000000000000..596fd8441c0f
--- /dev/null
+++ b/frontend/src/scenes/web-analytics/shareNudgeLogic.test.ts
@@ -0,0 +1,141 @@
+import { MOCK_DEFAULT_ORGANIZATION } from 'lib/api.mock'
+
+import { expectLogic } from 'kea-test-utils'
+import posthog from 'posthog-js'
+
+import { FEATURE_FLAGS } from 'lib/constants'
+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
+import { organizationLogic } from 'scenes/organizationLogic'
+
+import { initKeaTests } from '~/test/init'
+
+import { shareNudgeLogic } from './shareNudgeLogic'
+
+jest.mock('posthog-js')
+
+const FLAG = FEATURE_FLAGS.WEB_ANALYTICS_SHARE_NUDGE_V2
+
+function setVariant(variant: string): void {
+ featureFlagLogic.actions.setFeatureFlags([], { [FLAG]: variant })
+}
+
+function setMemberCount(memberCount: number): void {
+ organizationLogic.actions.loadCurrentOrganizationSuccess({
+ ...MOCK_DEFAULT_ORGANIZATION,
+ member_count: memberCount,
+ })
+}
+
+function capturesOf(event: string): any[][] {
+ return (posthog.capture as jest.Mock).mock.calls.filter(([name]) => name === event)
+}
+
+describe('shareNudgeLogic', () => {
+ let logic: ReturnType<typeof shareNudgeLogic.build>
+ let randomSpy: jest.SpyInstance
+
+ beforeEach(() => {
+ initKeaTests()
+ featureFlagLogic.mount()
+ randomSpy = jest.spyOn(Math, 'random').mockReturnValue(0)
+ ;(posthog.capture as jest.Mock).mockClear()
+ })
+
+ afterEach(() => {
+ logic?.unmount()
+ featureFlagLogic.unmount()
+ jest.restoreAllMocks()
+ })
+
+ function mount(variant: string): void {
+ setVariant(variant)
+ logic = shareNudgeLogic()
+ logic.mount()
+ ;(posthog.capture as jest.Mock).mockClear()
+ }
+
+ it('shows the export prompt when variant is "export" and the probability gate passes', () => {
+ randomSpy.mockReturnValue(0.1)
+ mount('export')
+
+ logic.actions.exportTriggered()
+
+ expect(logic.values.promptVisible).toBe(true)
+ expect(logic.values.promptSource).toBe('export_prompt')
+ expect(capturesOf('web analytics share nudge prompt shown')).toEqual([
+ ['web analytics share nudge prompt shown', { source: 'export_prompt' }],
+ ])
+ })
+
+ it('does not show the prompt when the probability gate fails', () => {
+ randomSpy.mockReturnValue(0.9)
+ mount('export')
+
+ logic.actions.exportTriggered()
+
+ expect(logic.values.promptVisible).toBe(false)
+ expect(capturesOf('web analytics share nudge prompt shown')).toHaveLength(0)
+ })
+
+ it.each([['banner'], ['control'], ['control_b']])('does not show the prompt for the "%s" variant', (variant) => {
+ randomSpy.mockReturnValue(0)
+ mount(variant)
+
+ logic.actions.exportTriggered()
+
+ expect(logic.values.promptVisible).toBe(false)
+ })
+
+ it('does not show the prompt for a solo org even on the "export" variant', () => {
+ randomSpy.mockReturnValue(0)
+ setMemberCount(1)
+ mount('export')
+
+ logic.actions.exportTriggered()
+
+ expect(logic.values.promptVisible).toBe(false)
+ })
+
+ it('does not show the prompt when not enrolled in the flag', () => {
+ randomSpy.mockReturnValue(0)
+ featureFlagLogic.actions.setFeatureFlags([], {})
+ logic = shareNudgeLogic()
+ logic.mount()
+
+ logic.actions.exportTriggered()
+
+ expect(logic.values.promptVisible).toBe(false)
+ })
+
+ it('does not show the prompt once dismissed for the session', () => {
+ randomSpy.mockReturnValue(0)
+ mount('export')
+
+ logic.actions.dismissForSession()
+ logic.actions.exportTriggered()
+
+ expect(logic.values.promptVisible).toBe(false)
+ })
+
+ it('dismissForSession hides an open export prompt', async () => {
+ randomSpy.mockReturnValue(0)
+ mount('export')
+
+ logic.actions.exportTriggered()
+ expect(logic.values.promptVisible).toBe(true)
+
+ await expectLogic(logic, () => {
+ logic.actions.dismissForSession()
+ }).toMatchValues({ promptVisible: false, sessionDismissed: true })
+ })
+
+ it('captures the exposure event once for the "export" variant', () => {
+ setVariant('export')
+ logic = shareNudgeLogic()
+ logic.mount()
+
+ expect(capturesOf('web analytics share nudge exposed')).toEqual([
+ ['web analytics share nudge exposed', { variant: 'export' }],
+ ])
+ })
+})
diff --git a/frontend/src/scenes/web-analytics/shareNudgeLogic.ts b/frontend/src/scenes/web-analytics/shareNudgeLogic.ts
index 83ab2884b1af..524a3635b40b 100644
--- a/frontend/src/scenes/web-analytics/shareNudgeLogic.ts
+++ b/frontend/src/scenes/web-analytics/shareNudgeLogic.ts
@@ -8,14 +8,10 @@ import { organizationLogic } from 'scenes/organizationLogic'
import type { shareNudgeLogicType } from './shareNudgeLogicType'
-const FLAG = FEATURE_FLAGS.WEB_ANALYTICS_SHARE_NUDGE
+const FLAG = FEATURE_FLAGS.WEB_ANALYTICS_SHARE_NUDGE_V2
const DASHBOARD_SELECTOR = '[data-attr="web-analytics-dashboard"]'
const HOVER_DWELL_MS = 2500
-
-export interface PromptAnchor {
- x: number
- y: number
-}
+const EXPORT_PROMPT_PROBABILITY = 0.25
export const shareNudgeLogic = kea<shareNudgeLogicType>([
path(['scenes', 'web-analytics', 'shareNudgeLogic']),
@@ -23,8 +19,9 @@ export const shareNudgeLogic = kea<shareNudgeLogicType>([
values: [organizationLogic, ['currentOrganization'], featureFlagLogic, ['featureFlags']],
})),
actions({
- showPrompt: (anchor: PromptAnchor) => ({ anchor }),
+ showPrompt: (source: string) => ({ source }),
dismissForSession: true,
+ exportTriggered: true,
}),
reducers({
promptVisible: [
@@ -34,10 +31,10 @@ export const shareNudgeLogic = kea<shareNudgeLogicType>([
dismissForSession: () => false,
},
],
- promptAnchor: [
- null as PromptAnchor | null,
+ promptSource: [
+ null as string | null,
{
- showPrompt: (_, { anchor }) => anchor,
+ showPrompt: (_, { source }) => source,
},
],
sessionDismissed: [
@@ -65,10 +62,20 @@ export const shareNudgeLogic = kea<shareNudgeLogicType>([
],
emphasizeShareButton: [(s) => [s.variant], (variant): boolean => variant === 'button'],
intentPromptEnabled: [(s) => [s.variant], (variant): boolean => variant === 'prompt'],
+ exportPromptEnabled: [(s) => [s.variant], (variant): boolean => variant === 'export'],
}),
- listeners(() => ({
- showPrompt: () => {
- posthog.capture('web analytics share nudge prompt shown')
+ listeners(({ values, actions }) => ({
+ showPrompt: ({ source }) => {
+ posthog.capture('web analytics share nudge prompt shown', { source })
+ },
+ exportTriggered: () => {
+ if (!values.exportPromptEnabled || values.sessionDismissed) {
+ return
+ }
+ if (Math.random() >= EXPORT_PROMPT_PROBABILITY) {
+ return
+ }
+ actions.showPrompt('export_prompt')
},
})),
subscriptions(({ values, actions, cache }) => ({
@@ -100,8 +107,7 @@ export const shareNudgeLogic = kea<shareNudgeLogicType>([
if (!container || !container.contains(selection.anchorNode)) {
return
}
- const rect = selection.getRangeAt(0).getBoundingClientRect()
- actions.showPrompt({ x: rect.left, y: rect.bottom })
+ actions.showPrompt('intent_prompt')
}
const onMouseMove = (event: MouseEvent): void => {
@@ -110,12 +116,10 @@ export const shareNudgeLogic = kea<shareNudgeLogicType>([
cache.disposables.dispose('shareNudgeHoverDwell')
return
}
- const x = event.clientX
- const y = event.clientY
cache.disposables.add(() => {
const timer = setTimeout(() => {
if (shouldTrigger()) {
- actions.showPrompt({ x, y })
+ actions.showPrompt('intent_prompt')
}
}, HOVER_DWELL_MS)
return () => clearTimeout(timer)
diff --git a/frontend/src/scenes/web-analytics/webTileHeaderHooks.tsx b/frontend/src/scenes/web-analytics/webTileHeaderHooks.tsx
index d7f6b372ad23..6c8f1f50fad6 100644
--- a/frontend/src/scenes/web-analytics/webTileHeaderHooks.tsx
+++ b/frontend/src/scenes/web-analytics/webTileHeaderHooks.tsx
@@ -20,6 +20,7 @@ import {
import { ExporterFormat, InsightLogicProps } from '~/types'
import { TileId, WEB_ANALYTICS_DATA_COLLECTION_NODE_ID } from './common'
+import { shareNudgeLogic } from './shareNudgeLogic'
import {
CalendarHeatmapAdapter,
ExportAdapter,
@@ -96,6 +97,7 @@ export function useWebTileOverflowMenuItems({
}: UseWebTileOverflowMenuItemsArgs): LemonMenuItem[] {
const effectiveInsightProps = insightProps ?? NO_ACTIVE_TAB_INSIGHT_PROPS
const { openModal } = useActions(webAnalyticsModalLogic)
+ const { exportTriggered } = useActions(shareNudgeLogic)
const adapter = useWebTileExportAdapter(query, effectiveInsightProps)
return useMemo(() => {
@@ -114,6 +116,7 @@ export function useWebTileOverflowMenuItems({
return
}
exportTableData(adapter.toTableData(), ExporterFormat.CSV)
+ exportTriggered()
},
},
{
@@ -146,7 +149,7 @@ export function useWebTileOverflowMenuItems({
}
return items
- }, [tileId, tabId, query, canOpenModal, openModal, adapter, extraMenuItems])
+ }, [tileId, tabId, query, canOpenModal, openModal, adapter, extraMenuItems, exportTriggered])
}
export type WebTileOpenInsightProps = Required<Pick<LemonButtonProps, 'to' | 'onClick'>>