diff --git a/CHANGELOG.md b/CHANGELOG.md index 0cd3480c9..0359bcd86 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file. - Added `LISTEN_IP` configuration parameter plausible/analytics#1189 - The breakdown endpoint with the property query `property=event:goal` returns custom goal properties (within `props`) - Added IPv6 Ecto support (via the environment-variable `ECTO_IPV6`) +- New filter type: `contains`, available for `page`, `entry_page`, `exit_page` ### Fixed - UI fix where multi-line text in pills would not be underlined properly on small screens. @@ -30,6 +31,7 @@ All notable changes to this project will be documented in this file. ### Changed - Cache the tracking script for 24 hours +- Move `entry_page` and `exit_page` to be part of the `Page` filter group ## v1.4.1 diff --git a/assets/js/dashboard/filters.js b/assets/js/dashboard/filters.js index 727c7619c..1ad4373c8 100644 --- a/assets/js/dashboard/filters.js +++ b/assets/js/dashboard/filters.js @@ -5,7 +5,13 @@ import classNames from 'classnames' import { Menu, Transition } from '@headlessui/react' import { appliedFilters, navigateToQuery, formattedFilters } from './query' -import { FILTER_GROUPS, formatFilterGroup, filterGroupForFilter } from './stats/modals/filter' +import { + FILTER_GROUPS, + formatFilterGroup, + filterGroupForFilter, + toFilterType, + valueWithoutPrefix +} from "./stats/modals/filter"; function removeFilter(key, history, query) { const newOpts = { @@ -32,16 +38,9 @@ function clearAllFilters(history, query) { ); } -function filterType(val) { - if (typeof(val) === 'string' && val.startsWith('!')) { - return ['is not', val.substr(1)] - } - - return ['is', val] -} - function filterText(key, rawValue, query) { - const [type, value] = filterType(rawValue) + const type = toFilterType(rawValue) + const value = valueWithoutPrefix(rawValue) if (key === "goal") { return <>Completed goal {value}> diff --git a/assets/js/dashboard/stats/modals/filter.js b/assets/js/dashboard/stats/modals/filter.js index 8a7b3e633..22acc68a9 100644 --- a/assets/js/dashboard/stats/modals/filter.js +++ b/assets/js/dashboard/stats/modals/filter.js @@ -11,24 +11,23 @@ import * as api from '../../api' import {apiPath, siteBasePath} from '../../util/url' export const FILTER_GROUPS = { - 'page': ['page'], + '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'], - 'entry_page': ['entry_page'], - 'exit_page': ['exit_page'], 'goal': ['goal'] } function getFormState(filterGroup, query) { return FILTER_GROUPS[filterGroup].reduce((result, filter) => { - const filterValue = query.filters[filter] || '' - let filterName = filterValue + const rawFilterValue = query.filters[filter] || '' + const type = toFilterType(rawFilterValue) + const filterValue = valueWithoutPrefix(rawFilterValue) - const type = filterValue[0] === '!' ? 'is_not' : 'is' + let filterName = filterValue if (filter === 'country' && filterValue !== '') { filterName = (new URLSearchParams(window.location.search)).get('country_name') @@ -43,6 +42,38 @@ function getFormState(filterGroup, query) { }, {}) } +const FILTER_TYPES = { + isNot: 'is not', + contains: 'contains', + is: 'is' +}; + +const FILTER_PREFIXES = { + [FILTER_TYPES.isNot]: '!', + [FILTER_TYPES.contains]: '~', + [FILTER_TYPES.is]: '' +}; + +export function toFilterType(value) { + return Object.keys(FILTER_PREFIXES) + .find(type => FILTER_PREFIXES[type] === value[0]) || FILTER_TYPES.is; +} + +export function valueWithoutPrefix(value) { + return [FILTER_TYPES.isNot, FILTER_TYPES.contains].includes(toFilterType(value)) + ? value.substring(1) + : 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) +} + function withIndefiniteArticle(word) { if (word.startsWith('UTM')) { return `a ${ word}` @@ -113,10 +144,7 @@ class FilterModal extends React.Component { if (filterKey === 'region') { res.push({filter: 'region_name', value: name}) } if (filterKey === 'city') { res.push({filter: 'city_name', value: name}) } - let finalFilterValue = value - finalFilterValue = (type === 'is_not' ? '!' : '') + finalFilterValue.trim() - - res.push({filter: filterKey, value: finalFilterValue}) + res.push({filter: filterKey, value: toFilterQuery(value, type)}) return res }, []) @@ -239,34 +267,9 @@ class FilterModal extends React.Component { className="z-10 origin-top-left absolute left-0 mt-2 w-24 rounded-md shadow-lg bg-white dark:bg-gray-800 ring-1 ring-black ring-opacity-5 focus:outline-none" >