-
- item && item.label}
- onChange={(event) => debouncedFetchOptions(event.target.value)}
- onBlur={onBlur}
- />
-
- {!loading && }
- {loading && }
-
-
-
-
- {renderOptions()}
-
-
+
+
+ { props.values.map((value) => {
+ return (
+
{value.label} removeOption(value, e)} className="cursor-pointer font-bold ml-1">×
+ )
+ })
+ }
+
+
+ {!loading && }
+ {loading && }
+
-
+
+
+ { loading && (
+
+ Loading options...
+
+ )}
+ { noMatchesFound && (
+
+ No matches found in the current dashboard. Try selecting a different time range or searching for something different
+
+ )}
+ { matchesFound && (
+ visibleOptions.map((option, i) => {
+ const text = option.freeChoice ? `Filter by '${option.label}'` : option.label
+
+ return (
+ selectOption(option)}
+ onMouseEnter={() => setHighlightedIndex(i)}
+ text={text}
+ />
+ )
+ })
+ )}
+
+
+
+ );
+}
+
+function Spinner() {
+ return (
+
+
+
+
)
}
diff --git a/assets/js/dashboard/filters.js b/assets/js/dashboard/filters.js
index aa1e9277f..2d2e63ebd 100644
--- a/assets/js/dashboard/filters.js
+++ b/assets/js/dashboard/filters.js
@@ -4,21 +4,22 @@ import { AdjustmentsVerticalIcon, MagnifyingGlassIcon, XMarkIcon, PencilSquareIc
import classNames from 'classnames'
import { Menu, Transition } from '@headlessui/react'
-import { appliedFilters, navigateToQuery, formattedFilters } from './query'
+import { appliedFilters, navigateToQuery } from './query'
import {
FILTER_GROUPS,
formatFilterGroup,
filterGroupForFilter,
- parseQueryFilter
-} from "./stats/modals/filter";
+ parseQueryFilter,
+ formattedFilters
+} from "./util/filters";
function removeFilter(key, history, query) {
const newOpts = {
[key]: false
}
- if (key === 'country') { newOpts.country_name = false }
- if (key === 'region') { newOpts.region_name = false }
- if (key === 'city') { newOpts.city_name = false }
+ if (key === 'country') { newOpts.country_labels = false }
+ if (key === 'region') { newOpts.region_labels = false }
+ if (key === 'city') { newOpts.city_labels = false }
navigateToQuery(
history,
@@ -36,51 +37,15 @@ function clearAllFilters(history, query) {
);
}
-function filterText(key, rawValue, query) {
- const {type, value} = parseQueryFilter(rawValue)
-
- if (key === "goal") {
- return <>Completed goal
{value} >
- }
- if (key === "props") {
- const [metaKey, metaValue] = Object.entries(value)[0]
- const eventName = query.filters.goal ? query.filters.goal : 'event'
- const metaFilter = parseQueryFilter(metaValue)
- return <>{eventName}.{metaKey} {metaFilter.type}
{metaFilter.value} >
- }
- if (key === "browser_version") {
- const isNotSet = query.filters.browser === '(not set)' || (!query.filters.browser)
- const browserName = isNotSet ? 'Browser' : query.filters.browser
- return <>{browserName}.Version {type}
{value} >
- }
- if (key === "os_version") {
- const isNotSet = query.filters.os === '(not set)' || (!query.filters.os)
- const osName = isNotSet ? 'Operating System' : query.filters.os
- console.info('osname', osName)
- return <>{osName}.Version {type}
{value} >
- }
- if (key === "country") {
- const q = new URLSearchParams(window.location.search)
- const countryName = q.get('country_name')
- return <>Country {type}
{countryName} >
- }
-
- if (key === "region") {
- const q = new URLSearchParams(window.location.search)
- const regionName = q.get('region_name')
- return <>Region {type}
{regionName} >
- }
-
- if (key === "city") {
- const q = new URLSearchParams(window.location.search)
- const cityName = q.get('city_name')
- return <>City {type}
{cityName} >
- }
-
+function filterText(key, _rawValue, query) {
+ const {type, clauses} = parseQueryFilter(query, key)
const formattedFilter = formattedFilters[key]
- if (formattedFilter) {
- return <>{formattedFilter} {type}
{value} >
+ if (key === "props") {
+ const [[propKey, _propValue]] = Object.entries(query.filters['props'])
+ return <>props.{propKey} {type} {clauses.map(({label}) =>
{label} ).reduce((prev, curr) => [prev, ' or ', curr])} >
+ } else if (formattedFilter) {
+ return <>{formattedFilter} {type} {clauses.map(({label}) =>
{label} ).reduce((prev, curr) => [prev, ' or ', curr])} >
}
throw new Error(`Unknown filter: ${key}`)
diff --git a/assets/js/dashboard/keybinding.js b/assets/js/dashboard/keybinding.js
index 5654a170f..003eb573d 100644
--- a/assets/js/dashboard/keybinding.js
+++ b/assets/js/dashboard/keybinding.js
@@ -20,7 +20,6 @@ export function shouldIgnoreKeypress(event) {
return modifierPressed || isTyping
}
-
/**
* Returns whether the given keybinding has been pressed and should be
* processed. Events can be ignored based on `shouldIgnoreKeypress(event)`.
@@ -30,6 +29,6 @@ export function shouldIgnoreKeypress(event) {
*
*/
export function isKeyPressed(event, keybinding) {
- const keyPressed = event.key.toLowerCase() == keybinding.toLowerCase()
+ const keyPressed = event.key.toLowerCase() == keybinding.toLowerCase()
return keyPressed && !shouldIgnoreKeypress(event)
}
diff --git a/assets/js/dashboard/query.js b/assets/js/dashboard/query.js
index e1a56eb99..1e1cc69e0 100644
--- a/assets/js/dashboard/query.js
+++ b/assets/js/dashboard/query.js
@@ -161,28 +161,3 @@ export function eventName(query) {
}
return 'pageviews'
}
-
-export const formattedFilters = {
- 'goal': 'Goal',
- 'props': 'Property',
- 'prop_key': 'Property',
- 'prop_value': 'Value',
- 'source': 'Source',
- 'utm_medium': 'UTM Medium',
- 'utm_source': 'UTM Source',
- 'utm_campaign': 'UTM Campaign',
- 'utm_content': 'UTM Content',
- 'utm_term': 'UTM Term',
- 'referrer': 'Referrer URL',
- 'screen': 'Screen size',
- 'browser': 'Browser',
- 'browser_version': 'Browser Version',
- 'os': 'Operating System',
- 'os_version': 'Operating System Version',
- 'country': 'Country',
- 'region': 'Region',
- 'city': 'City',
- 'page': 'Page',
- 'entry_page': 'Entry Page',
- 'exit_page': 'Exit Page'
-}
diff --git a/assets/js/dashboard/stats/devices/index.js b/assets/js/dashboard/stats/devices/index.js
index 2ca67944c..47f1e307d 100644
--- a/assets/js/dashboard/stats/devices/index.js
+++ b/assets/js/dashboard/stats/devices/index.js
@@ -26,14 +26,13 @@ function BrowserVersions({ query, site }) {
}
const isNotSet = query.filters.browser === '(not set)'
- const browserName = isNotSet ? 'Browser' : query.filters.browser
const filter = isNotSet ? {} : { browser_version: 'name' }
return (
)
@@ -61,14 +60,13 @@ function OperatingSystemVersions({ query, site }) {
}
const isNotSet = query.filters.os === '(not set)'
- const osName = isNotSet ? 'Operating System' : query.filters.os
const filter = isNotSet ? {} : { os_version: 'name' }
return (
)
diff --git a/assets/js/dashboard/stats/locations/index.js b/assets/js/dashboard/stats/locations/index.js
index 23574ea66..6ce0bbd62 100644
--- a/assets/js/dashboard/stats/locations/index.js
+++ b/assets/js/dashboard/stats/locations/index.js
@@ -21,7 +21,7 @@ function Countries({query, site, onClick}) {
return (
{
- const rawFilterValue = query.filters[filter] || ''
- const {type, value} = parseQueryFilter(rawFilterValue)
-
- let filterLabel = value
+ const {type, clauses} = parseQueryFilter(query, filter)
- if (filter === 'country' && value !== '') {
- filterLabel = (new URLSearchParams(window.location.search)).get('country_name')
- }
- if (filter === 'region' && value !== '') {
- filterLabel = (new URLSearchParams(window.location.search)).get('region_name')
- }
- if (filter === 'city' && value !== '') {
- filterLabel = (new URLSearchParams(window.location.search)).get('city_name')
- }
- return Object.assign(result, { [filter]: { label: filterLabel, value: value, type } })
+ return Object.assign(result, { [filter]: { type, clauses } })
}, {})
}
-const FILTER_TYPES = {
- isNot: 'is not',
- contains: 'contains',
- is: 'is'
-};
-
-const FILTER_PREFIXES = {
- [FILTER_TYPES.isNot]: '!',
- [FILTER_TYPES.contains]: '~',
- [FILTER_TYPES.is]: ''
-};
-
-export function parseQueryFilter(queryFilter) {
- const type = Object.keys(FILTER_PREFIXES)
- .find(type => FILTER_PREFIXES[type] === queryFilter[0]) || FILTER_TYPES.is;
-
- const value = [FILTER_TYPES.isNot, FILTER_TYPES.contains].includes(type)
- ? queryFilter.substring(1)
- : queryFilter;
-
- return {type, value}
-}
-
-function toFilterQuery(value, type) {
- const prefix = FILTER_PREFIXES[type];
- return prefix + value.trim();
-}
-
function supportsContains(filterName) {
return ['page', 'entry_page', 'exit_page'].includes(filterName)
}
@@ -103,32 +53,6 @@ function withIndefiniteArticle(word) {
}
-export function formatFilterGroup(filterGroup) {
- if (filterGroup === 'utm') {
- return 'UTM tags'
- } else if (filterGroup === 'location') {
- return 'Location'
- } else if (filterGroup === 'props') {
- return 'Property'
- } else {
- return formattedFilters[filterGroup]
- }
-}
-
-export function filterGroupForFilter(filter) {
- const map = Object.entries(FILTER_GROUPS).reduce((filterToGroupMap, [group, filtersInGroup]) => {
- const filtersToAdd = {}
- filtersInGroup.forEach((filterInGroup) => {
- filtersToAdd[filterInGroup] = group
- })
-
- return { ...filterToGroupMap, ...filtersToAdd }
- }, {})
-
-
- return map[filter] || filter
-}
-
class FilterModal extends React.Component {
constructor(props) {
super(props)
@@ -148,9 +72,7 @@ class FilterModal extends React.Component {
}
handleKeydown(e) {
- if (e.ctrlKey || e.metaKey || e.shiftKey || e.altKey || e.isComposing || e.keyCode === 229) {
- return
- }
+ if (shouldIgnoreKeypress(e)) return
if (e.target.tagName === 'BODY' && e.key === 'Enter') {
this.handleSubmit()
@@ -160,19 +82,19 @@ class FilterModal extends React.Component {
handleSubmit() {
const { formState } = this.state;
- const filters = Object.entries(formState).reduce((res, [filterKey, { type, value, label }]) => {
- if (filterKey === 'country') { res.push({ filter: 'country_name', value: label }) }
- if (filterKey === 'region') { res.push({ filter: 'region_name', value: label }) }
- if (filterKey === 'city') { res.push({ filter: 'city_name', value: label }) }
+ const filters = Object.entries(formState).reduce((res, [filterKey, { type, clauses }]) => {
+ if (clauses.length === 0) { return res }
+ if (filterKey === 'country') { res.push({ filter: 'country_labels', value: clauses.map(clause => clause.label).join('|') }) }
+ if (filterKey === 'region') { res.push({ filter: 'region_labels', value: clauses.map(clause => clause.label).join('|') }) }
+ if (filterKey === 'city') { res.push({ filter: 'city_labels', value: clauses.map(clause => clause.label).join('|') }) }
if (filterKey === 'prop_value') { return res }
if (filterKey === 'prop_key') {
- let propValue = formState['prop_value']
- let filterValue = JSON.stringify({ [value]: toFilterQuery(propValue.value, propValue.type) })
- res.push({ filter: 'props', value: filterValue })
+ const [{value: propKey}] = clauses
+ res.push({ filter: 'props', value: JSON.stringify({ [propKey]: toFilterQuery(formState.prop_value.type, formState.prop_value.clauses) }) })
return res
}
- res.push({ filter: filterKey, value: toFilterQuery(value, type) })
+ res.push({ filter: filterKey, value: toFilterQuery(type, clauses) })
return res
}, [])
@@ -183,7 +105,7 @@ class FilterModal extends React.Component {
return (selection) => {
this.setState(prevState => ({
formState: Object.assign(prevState.formState, {
- [filterName]: Object.assign(prevState.formState[filterName], selection)
+ [filterName]: Object.assign(prevState.formState[filterName], { clauses: selection })
})
}))
}
@@ -201,7 +123,7 @@ class FilterModal extends React.Component {
return (input) => {
const { query, formState } = this.state
const formFilters = Object.fromEntries(
- Object.entries(formState).map(([k, v]) => [k, v.code || v.value])
+ Object.entries(formState).map(([filter, {type, clauses}]) => [filter, toFilterQuery(type, clauses)])
)
const updatedQuery = this.queryForSuggestions(query, formFilters, filter)
return api.get(apiPath(this.props.site, `/suggestions/${filter}`), updatedQuery, { q: input.trim() })
@@ -216,7 +138,17 @@ class FilterModal extends React.Component {
const propsFilter = formFilters.prop_key ? { [formFilters.prop_key]: '!(none)' } : null
return { ...query, filters: { ...query.filters, props: propsFilter } }
} else {
- return { ...query, filters: { ...query.filters, ...formFilters, [filter]: null } }
+ return { ...query, filters: { ...query.filters, ...formFilters, [filter]: this.negate(formFilters[filter]) } }
+ }
+ }
+
+ negate(filterVal) {
+ if (!filterVal) {
+ return filterVal
+ } else if (filterVal.startsWith('!')) {
+ return filterVal
+ } else {
+ return '!' + filterVal
}
}
@@ -225,7 +157,11 @@ class FilterModal extends React.Component {
}
isDisabled() {
- return Object.entries(this.state.formState).every(([_key, { value: val }]) => !val)
+ if (this.state.selectedFilterGroup === 'props') {
+ return Object.entries(this.state.formState).some(([_key, { clauses }]) => clauses.length === 0)
+ } else {
+ return Object.entries(this.state.formState).every(([_key, { clauses }]) => clauses.length === 0)
+ }
}
selectFiltersAndCloseModal(filters) {
@@ -243,8 +179,8 @@ class FilterModal extends React.Component {
}
renderSearchBox(filter) {
- const isStrict = this.state.selectedFilterGroup === 'location'
- return
+ const freeChoice = this.state.selectedFilterGroup === 'page'
+ return
}
renderFilterInputs() {
@@ -332,7 +268,7 @@ class FilterModal extends React.Component {
-
- {this.renderHints()}
>
)
}
- renderHints() {
- if (['page', 'entry_page', 'exit_page'].includes(this.state.selectedFilterGroup)) {
- return (
- Hint: You can use double asterisks to match any character e.g. /blog** to group all of your blog posts. Or use double asterisks in front and back (e.g. **keyword**) to group all URLs containing a specific keyword.
- )
- }
-
- return null
- }
-
render() {
return (
diff --git a/assets/js/dashboard/stats/modals/modal.js b/assets/js/dashboard/stats/modals/modal.js
index bfc6fe832..4b031f360 100644
--- a/assets/js/dashboard/stats/modals/modal.js
+++ b/assets/js/dashboard/stats/modals/modal.js
@@ -1,6 +1,7 @@
import React from "react";
import { createPortal } from "react-dom";
import { withRouter } from 'react-router-dom';
+import { shouldIgnoreKeypress } from '../../keybinding'
// This corresponds to the 'md' breakpoint on TailwindCSS.
const MD_WIDTH = 768;
@@ -46,7 +47,7 @@ class Modal extends React.Component {
}
handleKeyup(e) {
- if (e.code === 'Escape') {
+ if (!shouldIgnoreKeypress(e) && e.code === 'Escape') {
this.close()
}
}
diff --git a/assets/js/dashboard/util/filters.js b/assets/js/dashboard/util/filters.js
new file mode 100644
index 000000000..654dbe731
--- /dev/null
+++ b/assets/js/dashboard/util/filters.js
@@ -0,0 +1,146 @@
+export const FILTER_GROUPS = {
+ 'page': ['page', 'entry_page', 'exit_page'],
+ 'source': ['source', 'referrer'],
+ 'location': ['country', 'region', 'city'],
+ 'screen': ['screen'],
+ 'browser': ['browser', 'browser_version'],
+ 'os': ['os', 'os_version'],
+ 'utm': ['utm_medium', 'utm_source', 'utm_campaign', 'utm_term', 'utm_content'],
+ 'goal': ['goal'],
+ 'props': ['prop_key', 'prop_value']
+}
+
+export const FILTER_TYPES = {
+ isNot: 'is not',
+ contains: 'contains',
+ is: 'is'
+};
+
+export const FILTER_PREFIXES = {
+ [FILTER_TYPES.isNot]: '!',
+ [FILTER_TYPES.contains]: '~',
+ [FILTER_TYPES.is]: ''
+};
+
+// As of March 2023, Safari does not support negative lookbehind regexes. In case it throws an error, falls back to plain | matching. This means
+// escaping pipe characters in filters does not currently work in Safari
+let NON_ESCAPED_PIPE_REGEX;
+try {
+ NON_ESCAPED_PIPE_REGEX = new RegExp("(? escapeFilterValue(clause.value.trim())).join('|')
+ return prefix + result;
+}
+
+function parsePrefix(rawValue) {
+ const type = Object.keys(FILTER_PREFIXES)
+ .find(type => FILTER_PREFIXES[type] === rawValue[0]) || FILTER_TYPES.is;
+
+ const value = [FILTER_TYPES.isNot, FILTER_TYPES.contains].includes(type)
+ ? rawValue.substring(1)
+ : rawValue;
+
+ const values = value
+ .split(NON_ESCAPED_PIPE_REGEX)
+ .filter((clause) => !!clause)
+ .map((val) => val.replaceAll(ESCAPED_PIPE, '|'))
+
+ return {type, values}
+}
+
+export function parseQueryFilter(query, filter) {
+ if (filter === 'props') {
+ const rawValue = query.filters['props']
+ const [[_propKey, propVal]] = Object.entries(rawValue)
+ const {type, values} = parsePrefix(propVal)
+ const clauses = values.map(val => { return {value: val, label: val}})
+ return {type, clauses}
+ } else {
+ const {type, values} = parsePrefix(query.filters[filter] || '')
+
+ let labels = values
+
+ if (filter === 'country' && values.length > 0) {
+ const rawLabel = (new URLSearchParams(window.location.search)).get('country_labels') || ''
+ labels = rawLabel.split('|').filter(label => !!label)
+ }
+
+ if (filter === 'region' && values.length > 0) {
+ const rawLabel = (new URLSearchParams(window.location.search)).get('region_labels') || ''
+ labels = rawLabel.split('|').filter(label => !!label)
+ }
+
+ if (filter === 'city' && values.length > 0) {
+ const rawLabel = (new URLSearchParams(window.location.search)).get('city_labels') || ''
+ labels = rawLabel.split('|').filter(label => !!label)
+ }
+
+ const clauses = values.map((value, index) => { return {value, label: labels[index]}})
+
+ return {type, clauses}
+ }
+}
+
+export function formatFilterGroup(filterGroup) {
+ if (filterGroup === 'utm') {
+ return 'UTM tags'
+ } else if (filterGroup === 'location') {
+ return 'Location'
+ } else if (filterGroup === 'props') {
+ return 'Property'
+ } else {
+ return formattedFilters[filterGroup]
+ }
+}
+
+export function filterGroupForFilter(filter) {
+ const map = Object.entries(FILTER_GROUPS).reduce((filterToGroupMap, [group, filtersInGroup]) => {
+ const filtersToAdd = {}
+ filtersInGroup.forEach((filterInGroup) => {
+ filtersToAdd[filterInGroup] = group
+ })
+
+ return { ...filterToGroupMap, ...filtersToAdd }
+ }, {})
+
+
+ return map[filter] || filter
+}
+
+export const formattedFilters = {
+ 'goal': 'Goal',
+ 'props': 'Property',
+ 'prop_key': 'Property',
+ 'prop_value': 'Value',
+ 'source': 'Source',
+ 'utm_medium': 'UTM Medium',
+ 'utm_source': 'UTM Source',
+ 'utm_campaign': 'UTM Campaign',
+ 'utm_content': 'UTM Content',
+ 'utm_term': 'UTM Term',
+ 'referrer': 'Referrer URL',
+ 'screen': 'Screen size',
+ 'browser': 'Browser',
+ 'browser_version': 'Browser Version',
+ 'os': 'Operating System',
+ 'os_version': 'Operating System Version',
+ 'country': 'Country',
+ 'region': 'Region',
+ 'city': 'City',
+ 'page': 'Page',
+ 'entry_page': 'Entry Page',
+ 'exit_page': 'Exit Page'
+}
diff --git a/lib/plausible/stats/base.ex b/lib/plausible/stats/base.ex
index 7920ac8e7..02436a8c3 100644
--- a/lib/plausible/stats/base.ex
+++ b/lib/plausible/stats/base.ex
@@ -513,10 +513,16 @@ defmodule Plausible.Stats.Base do
{first_datetime, last_datetime}
end
+ @replaces %{
+ ~r/\*\*/ => ".*",
+ ~r/(? "[^/]*",
+ "(" => "\\(",
+ ")" => "\\)"
+ }
def page_regex(expr) do
- "^#{expr}$"
- |> String.replace(~r/\*\*/, ".*")
- |> String.replace(~r/(?
+ String.replace(regex, pattern, replacement)
+ end)
end
defp add_sample_hint(db_q, query) do
diff --git a/lib/plausible/stats/filters.ex b/lib/plausible/stats/filters.ex
index bb7af2cb7..5ac7a8585 100644
--- a/lib/plausible/stats/filters.ex
+++ b/lib/plausible/stats/filters.ex
@@ -63,6 +63,7 @@ defmodule Plausible.Stats.Filters do
cond do
is_negated && is_wildcard && is_list -> {:not_matches_member, val}
is_negated && is_contains && is_list -> {:not_matches_member, Enum.map(val, &"**#{&1}**")}
+ is_wildcard && is_list -> {:matches_member, val}
is_negated && is_wildcard -> {:does_not_match, val}
is_negated && is_list -> {:not_member, val}
is_negated && is_contains -> {:does_not_match, "**" <> val <> "**"}
diff --git a/test/plausible_web/controllers/api/stats_controller/pages_test.exs b/test/plausible_web/controllers/api/stats_controller/pages_test.exs
index 4cfe4f983..c549bb516 100644
--- a/test/plausible_web/controllers/api/stats_controller/pages_test.exs
+++ b/test/plausible_web/controllers/api/stats_controller/pages_test.exs
@@ -515,6 +515,51 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do
]
end
+ test "page filter escapes brackets",
+ %{conn: conn, site: site} do
+ populate_stats(site, [
+ build(:pageview,
+ pathname: "/blog/(/post-1",
+ user_id: @user_id,
+ timestamp: ~N[2021-01-01 00:00:00]
+ ),
+ build(:pageview,
+ pathname: "/blog/(/post-2",
+ user_id: @user_id,
+ timestamp: ~N[2021-01-01 00:01:00]
+ ),
+ build(:pageview,
+ pathname: "/",
+ timestamp: ~N[2021-01-01 00:00:00]
+ )
+ ])
+
+ filters = Jason.encode!(%{page: "/blog/(/**|/blog/)/**"})
+
+ conn =
+ get(
+ conn,
+ "/api/stats/#{site.domain}/pages?period=day&date=2021-01-01&filters=#{filters}&detailed=true"
+ )
+
+ assert json_response(conn, 200) == [
+ %{
+ "name" => "/blog/(/post-1",
+ "visitors" => 1,
+ "pageviews" => 1,
+ "bounce_rate" => 0,
+ "time_on_page" => 60
+ },
+ %{
+ "name" => "/blog/(/post-2",
+ "visitors" => 1,
+ "pageviews" => 1,
+ "bounce_rate" => nil,
+ "time_on_page" => nil
+ }
+ ]
+ end
+
test "can filter using the not_matches_member filter type",
%{conn: conn, site: site} do
populate_stats(site, [