Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

fix: Broken experiments in embedded mode

Blue Mouse2a700795da60
diff --git a/backend/apps/cloud/src/experiment/experiment.controller.ts b/backend/apps/cloud/src/experiment/experiment.controller.ts
index c50c8e636..de5a0cd3f 100644
--- a/backend/apps/cloud/src/experiment/experiment.controller.ts
+++ b/backend/apps/cloud/src/experiment/experiment.controller.ts
@@ -134,6 +134,7 @@ export class ExperimentController {
async getProjectExperiments(
@CurrentUserId() userId: string,
@Param('projectId') projectId: string,
+ @Headers() headers: { 'x-password'?: string },
@Query('take', new ParseIntPipe({ optional: true })) take?: number,
@Query('skip', new ParseIntPipe({ optional: true })) skip?: number,
@Query('search') search?: string,
@@ -149,7 +150,7 @@ export class ExperimentController {
throw new NotFoundException('Project not found')
}
- this.projectService.allowedToView(project, userId)
+ this.projectService.allowedToView(project, userId, headers['x-password'])
this.projectService.assertProjectOwnerFeatureAccess(
project,
PlanFeatureCode.experiments,
@@ -190,6 +191,7 @@ export class ExperimentController {
async getExperiment(
@CurrentUserId() userId: string,
@Param('id') id: string,
+ @Headers() headers: { 'x-password'?: string },
) {
this.logger.log({ userId, id }, 'GET /experiment/:id')
@@ -203,7 +205,7 @@ export class ExperimentController {
experiment.project.id,
)
- this.projectService.allowedToView(project, userId)
+ this.projectService.allowedToView(project, userId, headers['x-password'])
this.projectService.assertProjectOwnerFeatureAccess(
project,
PlanFeatureCode.experiments,
@@ -958,6 +960,7 @@ export class ExperimentController {
async getExperimentResults(
@CurrentUserId() userId: string,
@Param('id') id: string,
+ @Headers() headers: { 'x-password'?: string },
@Query('period') period: string,
@Query('timeBucket') timeBucketParam?: TimeBucketType,
@Query('from') from?: string,
@@ -979,7 +982,7 @@ export class ExperimentController {
const project = await this.projectService.getFullProject(
experiment.project.id,
)
- this.projectService.allowedToView(project, userId)
+ this.projectService.allowedToView(project, userId, headers['x-password'])
this.projectService.assertProjectOwnerFeatureAccess(
project,
PlanFeatureCode.experiments,
diff --git a/backend/apps/cloud/src/goal/goal.controller.ts b/backend/apps/cloud/src/goal/goal.controller.ts
index 7ccfe44db..fc760c84f 100644
--- a/backend/apps/cloud/src/goal/goal.controller.ts
+++ b/backend/apps/cloud/src/goal/goal.controller.ts
@@ -145,6 +145,7 @@ export class GoalController {
async getGoal(
@CurrentUserId() userId: string,
@Param('goalId') goalId: string,
+ @Headers() headers: { 'x-password'?: string },
) {
this.logger.log({ userId, goalId }, 'GET /goal/:goalId')
@@ -159,7 +160,7 @@ export class GoalController {
const project = await this.projectService.getFullProject(goal.project.id)
- this.projectService.allowedToView(project, userId)
+ this.projectService.allowedToView(project, userId, headers['x-password'])
return {
..._omit(goal, ['project']),
diff --git a/backend/apps/community/src/experiment/experiment.controller.ts b/backend/apps/community/src/experiment/experiment.controller.ts
index c190b835e..bd0358a34 100644
--- a/backend/apps/community/src/experiment/experiment.controller.ts
+++ b/backend/apps/community/src/experiment/experiment.controller.ts
@@ -122,6 +122,7 @@ export class ExperimentController {
async getProjectExperiments(
@CurrentUserId() userId: string,
@Param('projectId') projectId: string,
+ @Headers() headers: { 'x-password'?: string },
@Query('take', new ParseIntPipe({ optional: true })) take?: number,
@Query('skip', new ParseIntPipe({ optional: true })) skip?: number,
@Query('search') search?: string,
@@ -137,7 +138,7 @@ export class ExperimentController {
throw new NotFoundException('Project not found')
}
- this.projectService.allowedToView(project, userId)
+ this.projectService.allowedToView(project, userId, headers['x-password'])
const result = await this.experimentService.paginate(
{ take, skip },
@@ -159,6 +160,7 @@ export class ExperimentController {
async getExperiment(
@CurrentUserId() userId: string,
@Param('id') id: string,
+ @Headers() headers: { 'x-password'?: string },
) {
this.logger.log({ userId, id }, 'GET /experiment/:id')
@@ -171,7 +173,7 @@ export class ExperimentController {
const project = await this.projectService.getFullProject(
experiment.projectId,
)
- this.projectService.allowedToView(project, userId)
+ this.projectService.allowedToView(project, userId, headers['x-password'])
return this.toDto(experiment)
}
@@ -597,6 +599,7 @@ export class ExperimentController {
async getExperimentResults(
@CurrentUserId() userId: string,
@Param('id') id: string,
+ @Headers() headers: { 'x-password'?: string },
@Query('period') period: string,
@Query('timeBucket') timeBucketParam?: TimeBucketType,
@Query('from') from?: string,
@@ -618,7 +621,7 @@ export class ExperimentController {
const project = await this.projectService.getFullProject(
experiment.projectId,
)
- this.projectService.allowedToView(project, userId)
+ this.projectService.allowedToView(project, userId, headers['x-password'])
experiment = await this.withGoal(experiment)
diff --git a/backend/apps/community/src/goal/goal.controller.ts b/backend/apps/community/src/goal/goal.controller.ts
index 69d9fced6..cab1b10e5 100644
--- a/backend/apps/community/src/goal/goal.controller.ts
+++ b/backend/apps/community/src/goal/goal.controller.ts
@@ -12,6 +12,7 @@ import {
HttpCode,
HttpStatus,
ParseIntPipe,
+ Headers,
} from '@nestjs/common'
import _isEmpty from 'lodash/isEmpty'
import _map from 'lodash/map'
@@ -124,10 +125,11 @@ export class GoalController {
) {}
@Get('/:goalId')
- @Auth()
+ @Auth(false, true)
async getGoal(
@CurrentUserId() userId: string,
@Param('goalId') goalId: string,
+ @Headers() headers: { 'x-password'?: string },
) {
this.logger.log({ userId, goalId }, 'GET /goal/:goalId')
@@ -139,7 +141,7 @@ export class GoalController {
const project = await this.projectService.getFullProject(goal.projectId)
- this.projectService.allowedToView(project, userId)
+ this.projectService.allowedToView(project, userId, headers['x-password'])
return {
...goal,
diff --git a/web/app/api/api.server.ts b/web/app/api/api.server.ts
index 0f0a05849..affe5dc5c 100644
--- a/web/app/api/api.server.ts
+++ b/web/app/api/api.server.ts
@@ -1397,8 +1397,13 @@ export interface Experiment {
export async function getExperimentServer(
request: Request,
experimentId: string,
+ password?: string,
): Promise<ServerFetchResult<Experiment>> {
- return serverFetch<Experiment>(request, `experiment/${experimentId}`)
+ const headers = password ? { 'x-password': password } : undefined
+
+ return serverFetch<Experiment>(request, `experiment/${experimentId}`, {
+ headers,
+ })
}
export interface ExperimentVariantResult {
@@ -1451,6 +1456,7 @@ export async function getExperimentResultsServer(
to = '',
timezone?: string,
filters: V2Filter[] = [],
+ password?: string,
): Promise<ServerFetchResult<ExperimentResults>> {
const params = new URLSearchParams({ period, timeBucket })
if (from) params.append('from', from)
@@ -1461,14 +1467,18 @@ export async function getExperimentResultsServer(
return serverFetch<ExperimentResults>(
request,
`experiment/${experimentId}/results?${params.toString()}`,
+ { headers: password ? { 'x-password': password } : undefined },
)
}
export async function getGoalServer(
request: Request,
goalId: string,
+ password?: string,
): Promise<ServerFetchResult<Goal>> {
- return serverFetch<Goal>(request, `goal/${goalId}`)
+ return serverFetch<Goal>(request, `goal/${goalId}`, {
+ headers: password ? { 'x-password': password } : undefined,
+ })
}
// ============================================================================
diff --git a/web/app/hooks/useAnalyticsProxy.ts b/web/app/hooks/useAnalyticsProxy.ts
index 5a839ed98..248057bf2 100644
--- a/web/app/hooks/useAnalyticsProxy.ts
+++ b/web/app/hooks/useAnalyticsProxy.ts
@@ -395,13 +395,18 @@ export function useExperimentResultsProxy() {
const [isLoading, setIsLoading] = useState(false)
const fetchResults = useCallback(
- async (experimentId: string, params: ClientAnalyticsParams) => {
+ async (
+ experimentId: string,
+ params: ClientAnalyticsParams,
+ projectId?: string,
+ ) => {
setIsLoading(true)
setError(null)
try {
const result = await postAnalytics<ExperimentResults>({
action: 'getExperimentResults',
+ projectId,
experimentId,
params,
})
@@ -426,26 +431,30 @@ export function useExperimentProxy() {
const [error, setError] = useState<string | null>(null)
const [isLoading, setIsLoading] = useState(false)
- const fetchExperiment = useCallback(async (experimentId: string) => {
- setIsLoading(true)
- setError(null)
+ const fetchExperiment = useCallback(
+ async (experimentId: string, projectId?: string) => {
+ setIsLoading(true)
+ setError(null)
- try {
- const result = await postAnalytics<Experiment>({
- action: 'getExperiment',
- experimentId,
- params: {},
- })
- setData(result.data)
- setError(result.error)
- return result.data
- } catch (err) {
- setError(err instanceof Error ? err.message : 'Unknown error')
- return null
- } finally {
- setIsLoading(false)
- }
- }, [])
+ try {
+ const result = await postAnalytics<Experiment>({
+ action: 'getExperiment',
+ projectId,
+ experimentId,
+ params: {},
+ })
+ setData(result.data)
+ setError(result.error)
+ return result.data
+ } catch (err) {
+ setError(err instanceof Error ? err.message : 'Unknown error')
+ return null
+ } finally {
+ setIsLoading(false)
+ }
+ },
+ [],
+ )
return { fetchExperiment, data, error, isLoading }
}
@@ -455,13 +464,14 @@ export function useGoalProxy() {
const [error, setError] = useState<string | null>(null)
const [isLoading, setIsLoading] = useState(false)
- const fetchGoal = useCallback(async (goalId: string) => {
+ const fetchGoal = useCallback(async (goalId: string, projectId?: string) => {
setIsLoading(true)
setError(null)
try {
const result = await postAnalytics<Goal>({
action: 'getGoal',
+ projectId,
goalId,
params: {},
})
diff --git a/web/app/pages/Project/tabs/Experiments/ExperimentResults.tsx b/web/app/pages/Project/tabs/Experiments/ExperimentResults.tsx
index 8bdfa2598..c5447d77b 100644
--- a/web/app/pages/Project/tabs/Experiments/ExperimentResults.tsx
+++ b/web/app/pages/Project/tabs/Experiments/ExperimentResults.tsx
@@ -49,6 +49,7 @@ import {
import DashboardHeader from '~/pages/Project/View/components/DashboardHeader'
import Filters from '~/pages/Project/View/components/Filters'
import { useViewProjectContext } from '~/pages/Project/View/ViewProject'
+import { useCurrentProject } from '~/providers/CurrentProjectProvider'
import { typeNameMapping } from '~/pages/Project/View/ViewProject.helpers'
import type { ProjectViewActionData } from '~/routes/projects.$id'
import Alert from '~/ui/Alert'
@@ -723,6 +724,7 @@ const ExperimentResults = ({
const goalProxy = useGoalProxy()
const completeFetcher = useFetcher<ProjectViewActionData>()
const processedCompleteRef = useRef<string | null>(null)
+ const { id: projectId } = useCurrentProject()
const { filters } = useViewProjectContext()
const [experiment, setExperiment] = useState<Experiment | null>(null)
@@ -760,21 +762,28 @@ const ExperimentResults = ({
try {
const [experimentData, resultsData] = await Promise.all([
- experimentProxy.fetchExperiment(experimentId),
- resultsProxy.fetchResults(experimentId, {
- period,
- timeBucket,
- from,
- to,
- timezone,
- filters,
- }),
+ experimentProxy.fetchExperiment(experimentId, projectId),
+ resultsProxy.fetchResults(
+ experimentId,
+ {
+ period,
+ timeBucket,
+ from,
+ to,
+ timezone,
+ filters,
+ },
+ projectId,
+ ),
])
let goalData: Goal | null = null
if (experimentData?.goalId) {
try {
- goalData = await goalProxy.fetchGoal(experimentData.goalId)
+ goalData = await goalProxy.fetchGoal(
+ experimentData.goalId,
+ projectId,
+ )
} catch {
goalData = null
}
diff --git a/web/app/pages/Project/tabs/Experiments/ExperimentSettingsModal.tsx b/web/app/pages/Project/tabs/Experiments/ExperimentSettingsModal.tsx
index 0547fc227..e96ed3bc5 100644
--- a/web/app/pages/Project/tabs/Experiments/ExperimentSettingsModal.tsx
+++ b/web/app/pages/Project/tabs/Experiments/ExperimentSettingsModal.tsx
@@ -159,7 +159,10 @@ const ExperimentSettingsModal = ({
setIsPlanningTouched(false)
setIsLoading(true)
try {
- const experiment = await experimentProxy.fetchExperiment(experimentId)
+ const experiment = await experimentProxy.fetchExperiment(
+ experimentId,
+ projectId,
+ )
if (!experiment) {
throw new Error('Failed to load experiment')
}
diff --git a/web/app/pages/Project/tabs/Experiments/ExperimentsView.tsx b/web/app/pages/Project/tabs/Experiments/ExperimentsView.tsx
index 444a9ded7..c22cab1d3 100644
--- a/web/app/pages/Project/tabs/Experiments/ExperimentsView.tsx
+++ b/web/app/pages/Project/tabs/Experiments/ExperimentsView.tsx
@@ -33,7 +33,10 @@ import {
useViewProjectContext,
useRefreshTriggers,
} from '~/pages/Project/View/ViewProject'
-import { useCurrentProject } from '~/providers/CurrentProjectProvider'
+import {
+ useCurrentProject,
+ useProjectPassword,
+} from '~/providers/CurrentProjectProvider'
import type { ProjectViewActionData } from '~/routes/projects.$id'
import Button from '~/ui/Button'
import { Badge } from '~/ui/Badge'
@@ -169,6 +172,7 @@ const ExperimentWinningRail = ({
filters: V2Filter[]
}) => {
const { t } = useTranslation()
+ const projectPassword = useProjectPassword(experiment.pid)
const {
data: winningData,
state: winningState,
@@ -190,6 +194,7 @@ const ExperimentWinningRail = ({
to,
timezone: timezone || '',
filters: filtersPayload,
+ password: projectPassword,
},
{ method: 'POST' },
)
@@ -198,6 +203,7 @@ const ExperimentWinningRail = ({
filtersPayload,
from,
period,
+ projectPassword,
shouldLoadResults,
submitWinning,
timeBucket,
@@ -843,6 +849,7 @@ const ExperimentsView = ({
timezone,
}: ExperimentsViewProps) => {
const { id } = useCurrentProject()
+ const projectPassword = useProjectPassword(id)
const { experimentsRefreshTrigger } = useRefreshTriggers()
const { filters, timeBucket } = useViewProjectContext()
const { t } = useTranslation()
@@ -914,12 +921,13 @@ const ExperimentsView = ({
take: String(take),
skip: String(skip),
search: search || '',
+ password: projectPassword,
},
{ method: 'POST' },
)
},
// eslint-disable-next-line react-hooks/exhaustive-deps
- [listFetcher.submit],
+ [listFetcher.submit, projectPassword],
)
// Handle list fetcher response
diff --git a/web/app/pages/Project/tabs/Goals/GoalSettingsModal.tsx b/web/app/pages/Project/tabs/Goals/GoalSettingsModal.tsx
index 380a2155c..db2e5c1fe 100644
--- a/web/app/pages/Project/tabs/Goals/GoalSettingsModal.tsx
+++ b/web/app/pages/Project/tabs/Goals/GoalSettingsModal.tsx
@@ -205,7 +205,7 @@ const GoalSettingsModal = ({
isOpen,
onClose,
onSuccess,
- projectId: _projectId,
+ projectId,
goalId,
tnMapping,
}: GoalSettingsModalProps) => {
@@ -261,7 +261,7 @@ const GoalSettingsModal = ({
if (!goalId) return
setIsLoading(true)
try {
- const goal = await goalProxy.fetchGoal(goalId)
+ const goal = await goalProxy.fetchGoal(goalId, projectId)
if (!goal) {
throw new Error('Failed to load goal')
}
diff --git a/web/app/pages/Subscribe/SubscribeIncluded.tsx b/web/app/pages/Subscribe/SubscribeIncluded.tsx
index c693c767c..eeab36e1e 100644
--- a/web/app/pages/Subscribe/SubscribeIncluded.tsx
+++ b/web/app/pages/Subscribe/SubscribeIncluded.tsx
@@ -71,7 +71,7 @@ export const SubscribeIncluded = ({
body: t('checkout.included.cards.websites.description'),
icon: GlobeIcon,
tint: 'text-cyan-500/[0.12] dark:text-cyan-300/[0.08]',
- placement: '-left-6 -bottom-9 rotate-6',
+ placement: '-right-6 -bottom-9 rotate-6',
},
{
figure: '10 / 25',
@@ -87,7 +87,7 @@ export const SubscribeIncluded = ({
body: t('checkout.included.cards.replays.description'),
icon: MonitorPlayIcon,
tint: 'text-pink-500/[0.1] dark:text-pink-300/[0.08]',
- placement: '-left-5 -bottom-9 -rotate-6',
+ placement: '-right-5 -bottom-9 -rotate-6',
},
]
diff --git a/web/app/routes/api.analytics.ts b/web/app/routes/api.analytics.ts
index 96efe3327..877288fda 100644
--- a/web/app/routes/api.analytics.ts
+++ b/web/app/routes/api.analytics.ts
@@ -387,6 +387,7 @@ export async function action({ request }: ActionFunctionArgs) {
formatDateForBackend(params.to) || '',
params.timezone,
params.filters || [],
+ password || undefined,
)
return data<ProxyResponse<ExperimentResults>>({
data: result.data,
@@ -405,7 +406,11 @@ export async function action({ request }: ActionFunctionArgs) {
{ status: 400 },
)
}
- const result = await getExperimentServer(request, body.experimentId)
+ const result = await getExperimentServer(
+ request,
+ body.experimentId,
+ password || undefined,
+ )
return data<ProxyResponse<Experiment>>({
data: result.data,
error: result.error
@@ -423,7 +428,11 @@ export async function action({ request }: ActionFunctionArgs) {
{ status: 400 },
)
}
- const result = await getGoalServer(request, body.goalId)
+ const result = await getGoalServer(
+ request,
+ body.goalId,
+ password || undefined,
+ )
return data<ProxyResponse<Goal>>({
data: result.data,
error: result.error
diff --git a/web/app/routes/projects.$id.tsx b/web/app/routes/projects.$id.tsx
index d10a9c710..ba639dec9 100644
--- a/web/app/routes/projects.$id.tsx
+++ b/web/app/routes/projects.$id.tsx
@@ -1755,6 +1755,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
// Experiments
case 'get-project-experiments': {
+ const password = getPassword()
const take = Number(formData.get('take') || '20')
const skip = Number(formData.get('skip') || '0')
const search = formData.get('search')?.toString() || ''
@@ -1770,6 +1771,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
`experiment/project/${projectId}?${params.toString()}`,
{
method: 'GET',
+ headers: password ? { 'x-password': password } : undefined,
},
)
@@ -1788,9 +1790,11 @@ export async function action({ request, params }: ActionFunctionArgs) {
case 'get-experiment': {
const experimentId = formData.get('experimentId')?.toString()
+ const password = getPassword()
const result = await serverFetch(request, `experiment/${experimentId}`, {
method: 'GET',
+ headers: password ? { 'x-password': password } : undefined,
})
if (result.error) {
@@ -2014,6 +2018,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
case 'get-experiment-results': {
const experimentId = formData.get('experimentId')?.toString()
+ const password = getPassword()
const period = formData.get('period')?.toString() || '7d'
const timeBucket = formData.get('timeBucket')?.toString() || 'day'
const fromDate = formData.get('from')?.toString() || ''
@@ -2032,6 +2037,7 @@ export async function action({ request, params }: ActionFunctionArgs) {
`experiment/${experimentId}/results?${params.toString()}`,
{
method: 'GET',
+ headers: password ? { 'x-password': password } : undefined,
},
)