Skip to content

Commit

Permalink
feat(web-analytics): Allow customers to compare against a different p…
Browse files Browse the repository at this point in the history
…eriod

Right now, we'd always compare our data against the last period, i.e. if we're looking at the data from the last 14 days, then you'd always compare it against the previous 14 days.

This is not always ideal, as someone might want to compare against numbers from the same time last year.

We've added the comparator component in a previous commit (hidden behind a FF) and we're now actually using that data in the backend.
  • Loading branch information
rafaeelaudibert committed Dec 11, 2024
1 parent 67b8765 commit 5eee1ba
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 150 deletions.
44 changes: 12 additions & 32 deletions frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -546,22 +546,6 @@
},
"type": "object"
},
"AssistantCompareFilter": {
"additionalProperties": false,
"properties": {
"compare": {
"default": false,
"description": "Whether to compare the current date range to a previous date range.",
"type": "boolean"
},
"compare_to": {
"default": "-7d",
"description": "The date range to compare to. The value is a relative date. Examples of relative dates are: `-1y` for 1 year ago, `-14m` for 14 months ago, `-100w` for 100 weeks ago, `-14d` for 14 days ago, `-30h` for 30 hours ago.",
"type": "string"
}
},
"type": "object"
},
"AssistantDateTimePropertyFilter": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -3792,9 +3776,13 @@
"additionalProperties": false,
"properties": {
"compare": {
"default": false,
"description": "Whether to compare the current date range to a previous date range.",
"type": "boolean"
},
"compare_to": {
"default": "-7d",
"description": "The date range to compare to. The value is a relative date. Examples of relative dates are: `-1y` for 1 year ago, `-14m` for 14 months ago, `-100w` for 100 weeks ago, `-14d` for 14 days ago, `-30h` for 30 hours ago.",
"type": "string"
}
},
Expand Down Expand Up @@ -12869,6 +12857,9 @@
"WebExternalClicksTableQuery": {
"additionalProperties": false,
"properties": {
"compareFilter": {
"$ref": "#/definitions/CompareFilter"
},
"conversionGoal": {
"anyOf": [
{
Expand Down Expand Up @@ -12982,6 +12973,9 @@
"WebGoalsQuery": {
"additionalProperties": false,
"properties": {
"compareFilter": {
"$ref": "#/definitions/CompareFilter"
},
"conversionGoal": {
"anyOf": [
{
Expand Down Expand Up @@ -13122,14 +13116,7 @@
"additionalProperties": false,
"properties": {
"compareFilter": {
"anyOf": [
{
"$ref": "#/definitions/CompareFilter"
},
{
"type": "null"
}
]
"$ref": "#/definitions/CompareFilter"
},
"conversionGoal": {
"anyOf": [
Expand Down Expand Up @@ -13261,14 +13248,7 @@
"$ref": "#/definitions/WebStatsBreakdown"
},
"compareFilter": {
"anyOf": [
{
"$ref": "#/definitions/CompareFilter"
},
{
"type": "null"
}
]
"$ref": "#/definitions/CompareFilter"
},
"conversionGoal": {
"anyOf": [
Expand Down
10 changes: 2 additions & 8 deletions frontend/src/queries/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1171,7 +1171,7 @@ export interface AssistantTrendsFilter {
yAxisScaleType?: TrendsFilterLegacy['y_axis_scale_type']
}

export interface AssistantCompareFilter {
export interface CompareFilter {
/**
* Whether to compare the current date range to a previous date range.
* @default false
Expand Down Expand Up @@ -1789,6 +1789,7 @@ interface WebAnalyticsQueryBase<R extends Record<string, any>> extends DataNode<
dateRange?: DateRange
properties: WebAnalyticsPropertyFilters
conversionGoal?: WebAnalyticsConversionGoal | null
compareFilter?: CompareFilter
sampling?: {
enabled?: boolean
forceSamplingRate?: SamplingRate
Expand All @@ -1800,7 +1801,6 @@ interface WebAnalyticsQueryBase<R extends Record<string, any>> extends DataNode<

export interface WebOverviewQuery extends WebAnalyticsQueryBase<WebOverviewQueryResponse> {
kind: NodeKind.WebOverviewQuery
compareFilter?: CompareFilter | null
includeLCPScore?: boolean
}

Expand Down Expand Up @@ -1852,7 +1852,6 @@ export enum WebStatsBreakdown {
export interface WebStatsTableQuery extends WebAnalyticsQueryBase<WebStatsTableQueryResponse> {
kind: NodeKind.WebStatsTableQuery
breakdownBy: WebStatsBreakdown
compareFilter?: CompareFilter | null
includeScrollDepth?: boolean // automatically sets includeBounceRate to true
includeBounceRate?: boolean
doPathCleaning?: boolean
Expand Down Expand Up @@ -2356,11 +2355,6 @@ export interface BreakdownFilter {
breakdown_hide_other_aggregation?: boolean | null // hides the "other" field for trends
}

export interface CompareFilter {
compare?: boolean
compare_to?: string
}

// TODO: Rename to `DashboardFilters` for consistency with `HogQLFilters`
export interface DashboardFilter {
date_from?: string | null
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/scenes/web-analytics/tiles/WebAnalyticsTile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const VariationCell = (
isPercentage ? `${(value * 100).toFixed(1)}%` : value.toLocaleString()

return function Cell({ value }) {
const { compareFilter } = useValues(webAnalyticsLogic)

if (!value) {
return null
}
Expand All @@ -57,10 +59,11 @@ const VariationCell = (
}

const [current, previous] = value as [number, number]

const pctChangeFromPrevious =
previous === 0 && current === 0 // Special case, render as flatline
? 0
: current === null
: current === null || !compareFilter || compareFilter.compare === false
? null
: previous === null || previous === 0
? Infinity
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/scenes/web-analytics/webAnalyticsLogic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
return { tileId, tabId }
},
setConversionGoalWarning: (warning: ConversionGoalWarning | null) => ({ warning }),
setCompareFilter: (compareFilter: CompareFilter | null) => ({ compareFilter }),
setCompareFilter: (compareFilter: CompareFilter) => ({ compareFilter }),
}),
reducers({
webAnalyticsFilters: [
Expand Down Expand Up @@ -475,7 +475,7 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
},
],
compareFilter: [
{ compare: true } as CompareFilter | null,
{ compare: true } as CompareFilter,
persistConfig,
{
setCompareFilter: (_, { compareFilter }) => compareFilter,
Expand Down Expand Up @@ -621,7 +621,7 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
display: ChartDisplayType.ActionsLineGraph,
...trendsFilter,
},
compareFilter: compareFilter || { compare: false },
compareFilter,
filterTestAccounts,
conversionGoal: featureFlags[FEATURE_FLAGS.WEB_ANALYTICS_CONVERSION_GOAL_FILTERS]
? conversionGoal
Expand Down Expand Up @@ -882,6 +882,7 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
kind: NodeKind.WebExternalClicksTableQuery,
properties: webAnalyticsFilters,
dateRange,
compareFilter, // NOTE: Backend is not using this yet
sampling,
limit: 10,
filterTestAccounts,
Expand Down Expand Up @@ -1290,6 +1291,7 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
kind: NodeKind.WebGoalsQuery,
properties: webAnalyticsFilters,
dateRange,
compareFilter,
sampling,
limit: 10,
filterTestAccounts,
Expand Down
Loading

0 comments on commit 5eee1ba

Please sign in to comment.