diff --git a/CHANGELOG.md b/CHANGELOG.md index 299917996..f87cffa09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ All notable changes to this project will be documented in this file. ### Added +- Allow filtering by multiple custom properties - Wildcard and member filtering on the Stats API `event:goal` property - Allow filtering with `contains`/`matches` operator for custom properties - Add `referrers.csv` to CSV export diff --git a/assets/js/dashboard/components/combobox.js b/assets/js/dashboard/components/combobox.js index 428f858cd..ffc92ba3f 100644 --- a/assets/js/dashboard/components/combobox.js +++ b/assets/js/dashboard/components/combobox.js @@ -91,8 +91,10 @@ export default function PlausibleCombobox(props) { } } - function isDisabled(option) { - return props.values.some((val) => val.value === option.value) + function isOptionDisabled(option) { + const optionAlreadySelected = props.values.some((val) => val.value === option.value) + const optionDisabled = (props.disabledOptions || []).some((val) => val?.value === option.value) + return optionAlreadySelected || optionDisabled } function fetchOptions(query) { @@ -161,7 +163,7 @@ export default function PlausibleCombobox(props) { searchRef.current.focus() } }, [props.values.length === 0]) - + const searchBoxClass = 'border-none py-1 px-0 w-full inline-block rounded-md focus:outline-none focus:ring-0 text-sm' const containerClass = classNames('relative w-full', { @@ -217,15 +219,15 @@ export default function PlausibleCombobox(props) { } function renderDropDownContent() { - const matchesFound = visibleOptions.length > 0 && visibleOptions.some(option => !isDisabled(option)) + const matchesFound = visibleOptions.length > 0 && visibleOptions.some(option => !isOptionDisabled(option)) if (loading) { return
Loading options...
} - + if (matchesFound) { return visibleOptions - .filter(option => !isDisabled(option)) + .filter(option => !isOptionDisabled(option)) .map((option, i) => { const text = option.freeChoice ? `Filter by '${option.label}'` : option.label diff --git a/assets/js/dashboard/components/filter-type-selector.js b/assets/js/dashboard/components/filter-type-selector.js index 784930a36..2baf85f0a 100644 --- a/assets/js/dashboard/components/filter-type-selector.js +++ b/assets/js/dashboard/components/filter-type-selector.js @@ -1,6 +1,6 @@ import React, { Fragment } from "react"; -import { FILTER_TYPES } from "../util/filters"; +import { FILTER_OPERATIONS } from "../util/filters"; import { Menu, Transition } from "@headlessui/react"; import { ChevronDownIcon } from '@heroicons/react/20/solid' import { isFreeChoiceFilter, supportsIsNot } from "../util/filters"; @@ -61,9 +61,9 @@ export default function FilterTypeSelector(props) { className="z-10 origin-top-left absolute left-0 mt-2 w-full rounded-md shadow-lg bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5 focus:outline-none" >
- {renderTypeItem(FILTER_TYPES.is, true)} - {renderTypeItem(FILTER_TYPES.isNot, supportsIsNot(filterName))} - {renderTypeItem(FILTER_TYPES.contains, isFreeChoiceFilter(filterName))} + {renderTypeItem(FILTER_OPERATIONS.is, true)} + {renderTypeItem(FILTER_OPERATIONS.isNot, supportsIsNot(filterName))} + {renderTypeItem(FILTER_OPERATIONS.contains, isFreeChoiceFilter(filterName))}
diff --git a/assets/js/dashboard/filters.js b/assets/js/dashboard/filters.js index b2144002b..784b7c600 100644 --- a/assets/js/dashboard/filters.js +++ b/assets/js/dashboard/filters.js @@ -10,12 +10,23 @@ import { formatFilterGroup, filterGroupForFilter, parseQueryFilter, + parseQueryPropsFilter, formattedFilters } from "./util/filters"; -function removeFilter(key, history, query) { - const newOpts = { - [key]: false +function removeFilter(filterType, key, history, query) { + const newOpts = {} + if (filterType === 'props') { + if (Object.keys(query.filters.props).length == 1) { + newOpts.props = false + } else { + newOpts.props = JSON.stringify({ + ...query.filters.props, + [key]: undefined, + }) + } + } else { + newOpts[key] = false } if (key === 'country') { newOpts.country_labels = false } if (key === 'region') { newOpts.region_labels = false } @@ -37,34 +48,38 @@ function clearAllFilters(history, query) { ); } -function filterText(key, _rawValue, query) { - const {type, clauses} = parseQueryFilter(query, key) +function filterText(filterType, key, query) { const formattedFilter = formattedFilters[key] - if (key === "props") { - const [[propKey, _propValue]] = Object.entries(query.filters['props']) - return <>Property {propKey} {type} {clauses.map(({label}) => {label}).reduce((prev, curr) => [prev, ' or ', curr])} + if (filterType === "props") { + const { propKey, clauses, type } = parseQueryPropsFilter(query).find((filter) => filter.propKey.value === key) + return <>Property {propKey.label} {type} {clauses.map(({label}) => {label}).reduce((prev, curr) => [prev, ' or ', curr])} } else if (formattedFilter) { + const {type, clauses} = parseQueryFilter(query, key) return <>{formattedFilter} {type} {clauses.map(({label}) => {label}).reduce((prev, curr) => [prev, ' or ', curr])} } throw new Error(`Unknown filter: ${key}`) } -function renderDropdownFilter(site, history, [key, value], query) { +function renderDropdownFilter(site, history, { key, value, filterType }, query) { return ( - +
- {filterText(key, value, query)} + {filterText(filterType, key, query)} - removeFilter(key, history, query)}> + removeFilter(filterType, key, history, query)} + >
@@ -202,13 +217,21 @@ class Filters extends React.Component { }); }; - renderListFilter(history, [key, value], query) { + renderListFilter(history, { key, value, filterType }, query) { return ( - - - {filterText(key, value, query)} + + + {filterText(filterType, key, query)} - removeFilter(key, history, query)}> + removeFilter(filterType, key, history, query)} + > diff --git a/assets/js/dashboard/query.js b/assets/js/dashboard/query.js index 3aaec0a3d..82e9f4e16 100644 --- a/assets/js/dashboard/query.js +++ b/assets/js/dashboard/query.js @@ -1,8 +1,8 @@ import React from 'react' import { Link, withRouter } from 'react-router-dom' -import {nowForSite} from './util/date' +import { nowForSite } from './util/date' import * as storage from './util/storage' -import { COMPARISON_DISABLED_PERIODS, getStoredComparisonMode, isComparisonEnabled,getStoredMatchDayOfWeek } from './comparison-input' +import { COMPARISON_DISABLED_PERIODS, getStoredComparisonMode, isComparisonEnabled, getStoredMatchDayOfWeek } from './comparison-input' import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; @@ -65,9 +65,13 @@ export function parseQuery(querystring, site) { } export function appliedFilters(query) { - return Object.keys(query.filters) - .map((key) => [key, query.filters[key]]) - .filter(([_key, value]) => !!value); + const propKeys = Object.entries(query.filters.props || {}) + .map(([key, value]) => ({ key, value, filterType: 'props' })) + + return Object.entries(query.filters) + .map(([key, value]) => ({ key, value, filterType: key })) + .filter(({ key, value }) => key !== 'props' && !!value) + .concat(propKeys) } function generateQueryString(data) { @@ -122,7 +126,7 @@ class QueryLink extends React.Component { const QueryLinkWithRouter = withRouter(QueryLink) export { QueryLinkWithRouter as QueryLink }; -function QueryButton({history, query, to, disabled, className, children, onClick}) { +function QueryButton({ history, query, to, disabled, className, children, onClick }) { return (