Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assets/js/dashboard/stats/graph/fetch-top-stats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ describe(`${topStatsQueries.name}`, () => {
resolvedFilters: inputDashboardState.filters,
...inputDashboardState
}
const queries = topStatsQueries(dashboardState, metrics)
const queries = topStatsQueries(dashboardState, metrics, null)
expect(queries).toEqual(expectedQueries)
}
)
Expand Down
24 changes: 19 additions & 5 deletions assets/js/dashboard/stats/graph/fetch-top-stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ import { createStatsQuery, ReportParams, StatsQuery } from '../../stats-query'
import {
hasConversionGoalFilter,
hasPageFilter,
isRealTimeDashboard
isRealTimeDashboard,
remapToApiFilters
} from '../../util/filters'
import { isSegmentFilter } from '../../filtering/segments'

export function topStatsQueries(
dashboardState: DashboardState,
metrics: Metric[]
metrics: Metric[],
limitedToSegmentId: number | null
): [StatsQuery, StatsQuery | null] {
let currentVisitorsQuery = null

Expand All @@ -26,7 +29,16 @@ export function topStatsQueries(
metrics: ['visitors']
})

currentVisitorsQuery.filters = []
const filters = limitedToSegmentId

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue, blocking: This changes the meaning of "Current visitors" top stats block depending on whether the user accesses the dashboard using shared link limited to segment or normally, with the same segment applied.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the decision was to keep the current behaviour where current visitors ignore all filters (including enforced segment). That's now done in #6351, where I've switched current visitors back to the dedicated /current-visitors endpoint. Closing this PR in favour of 6351.

? dashboardState.filters.filter((f) => {
const [_op, _key, clauses] = f
return (
isSegmentFilter(f) && clauses[0] === limitedToSegmentId.toString()
)
})
: []

currentVisitorsQuery.filters = remapToApiFilters(filters)
}
const topStatsQuery = constructTopStatsQuery(dashboardState, metrics)

Expand All @@ -35,12 +47,14 @@ export function topStatsQueries(

export async function fetchTopStats(
site: PlausibleSite,
dashboardState: DashboardState
dashboardState: DashboardState,
limitedToSegmentId: number | null
) {
const metrics = chooseMetrics(site, dashboardState)
const [topStatsQuery, currentVisitorsQuery] = topStatsQueries(
dashboardState,
metrics
metrics,
limitedToSegmentId
)
const topStatsPromise = api.stats(site, topStatsQuery)

Expand Down
5 changes: 4 additions & 1 deletion assets/js/dashboard/stats/graph/visitor-graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getStaleTime } from '../../hooks/api-client'
import { MainGraph, MainGraphContainer, useMainGraphWidth } from './main-graph'
import { useGraphIntervalContext } from './graph-interval-context'
import { useSetImportsIncluded } from './imports-included-context'
import { useSegmentsContext } from '../../filtering/segments-context'

// height of at least one row of top stats
const DEFAULT_TOP_STATS_LOADING_HEIGHT_PX = 85
Expand All @@ -29,6 +30,8 @@ export default function VisitorGraph({
const { dashboardState } = useDashboardStateContext()
const isRealtime = dashboardState.period === DashboardPeriod.realtime
const queryClient = useQueryClient()
const { limitedToSegment } = useSegmentsContext()
const limitedToSegmentId = limitedToSegment ? limitedToSegment.id : null

const { selectedInterval } = useGraphIntervalContext()

Expand All @@ -47,7 +50,7 @@ export default function VisitorGraph({
queryKey: ['top-stats', { dashboardState }] as const,
queryFn: async ({ queryKey }) => {
const [_, opts] = queryKey
return await fetchTopStats(site, opts.dashboardState)
return await fetchTopStats(site, opts.dashboardState, limitedToSegmentId)
},
placeholderData: (previousData) => previousData,
staleTime: ({ queryKey }) => {
Expand Down
Loading