From cb7b5b9fbd83f618cf4ebbe6582a9202b4989033 Mon Sep 17 00:00:00 2001 From: Vignesh Joglekar Date: Fri, 21 May 2021 01:49:52 -0500 Subject: [PATCH] First pass on manual filter & path regex/negated filters Still needs: - Form structure on filter modal - Edit filter button - Filter dropdown UI improvement - Filter modal mount data collection - Tests - Potentially negating other filters --- assets/css/modal.css | 1 - assets/js/dashboard/filters.js | 29 ++-- assets/js/dashboard/historical.js | 2 +- assets/js/dashboard/query.js | 19 +++ assets/js/dashboard/router.js | 4 + assets/js/dashboard/stats/modals/filter.js | 97 +++++++++++++ assets/js/dashboard/stats/modals/modal.js | 2 +- lib/plausible/stats/clickhouse.ex | 156 +++++++++++++-------- 8 files changed, 234 insertions(+), 76 deletions(-) create mode 100644 assets/js/dashboard/stats/modals/filter.js diff --git a/assets/css/modal.css b/assets/css/modal.css index 10028c9ea..2a36314ef 100644 --- a/assets/css/modal.css +++ b/assets/css/modal.css @@ -38,7 +38,6 @@ .modal__container { background-color: #fff; padding: 1rem 2rem; - max-width: 860px; border-radius: 4px; margin: 50px auto; box-sizing: border-box; diff --git a/assets/js/dashboard/filters.js b/assets/js/dashboard/filters.js index 48e4e082a..ba88d006f 100644 --- a/assets/js/dashboard/filters.js +++ b/assets/js/dashboard/filters.js @@ -1,5 +1,5 @@ import React from 'react'; -import { withRouter } from 'react-router-dom' +import { withRouter, Link } from 'react-router-dom' import { countFilters, navigateToQuery, removeQueryParam } from './query' import Datamap from 'datamaps' import Transition from "../transition.js"; @@ -181,7 +181,7 @@ class Filters extends React.Component { renderListFilter(history, [key, value], query) { return ( - + {this.filterText(key, value, query)} this.removeFilter(key, history, query)}>✕ ) @@ -198,11 +198,15 @@ class Filters extends React.Component { renderDropDownContent() { const { viewport } = this.state; - const { history, query } = this.props; + const { history, query, site } = this.props; return (
this.dropDownNode = node}>
+ + + Add Filter + {this.appliedFilters.map((filter) => this.renderDropdownFilter(history, filter, query))}
this.clearAllFilters(history, query)}> Clear All Filters @@ -239,11 +243,16 @@ class Filters extends React.Component { } renderFilterList() { - const { history, query } = this.props; + const { history, query, site } = this.props; + const {viewport} = this.state; return ( -
+
{(this.appliedFilters.map((filter) => this.renderListFilter(history, filter, query)))} + + + {viewport <= 768 ? "Filter" : "Add Filter"} +
); } @@ -251,15 +260,11 @@ class Filters extends React.Component { render() { const { wrapped, viewport } = this.state; - if (this.appliedFilters.length > 0) { - if (wrapped === 2 || viewport <= 768) { - return this.renderDropDown(); - } - - return this.renderFilterList(); + if (this.appliedFilters.length > 0 && (wrapped === 2 || viewport <= 768)) { + return this.renderDropDown(); } - return null; + return this.renderFilterList(); } } diff --git a/assets/js/dashboard/historical.js b/assets/js/dashboard/historical.js index f2380ac54..3723e9028 100644 --- a/assets/js/dashboard/historical.js +++ b/assets/js/dashboard/historical.js @@ -34,7 +34,7 @@ class Historical extends React.Component {
- +
diff --git a/assets/js/dashboard/query.js b/assets/js/dashboard/query.js index 1fa3c8fdb..2c98a9061 100644 --- a/assets/js/dashboard/query.js +++ b/assets/js/dashboard/query.js @@ -159,3 +159,22 @@ export function eventName(query) { } return 'pageviews' } + +export const formattedFilters = { + 'goal': 'Goal', + 'props': 'Props', + 'source': 'Source', + 'utm_medium': 'UTM Medium', + 'utm_source': 'UTM Source', + 'utm_campaign': 'UTM Campaign', + 'referrer': 'Referrer', + 'screen': 'Screen size', + 'browser': 'Browser', + 'browser_version': 'Browser Version', + 'os': 'Operating System', + 'os_version': 'Operating System Version', + 'country': 'Country', + 'page': 'Page', + 'entry_page': 'Entry Page', + 'exit_page': 'Exit Page' +} diff --git a/assets/js/dashboard/router.js b/assets/js/dashboard/router.js index 23d223ba6..6f70bb5df 100644 --- a/assets/js/dashboard/router.js +++ b/assets/js/dashboard/router.js @@ -7,6 +7,7 @@ import PagesModal from './stats/modals/pages' import EntryPagesModal from './stats/modals/entry-pages' import ExitPagesModal from './stats/modals/exit-pages' import CountriesModal from './stats/modals/countries' +import FilterModal from './stats/modals/filter' import {BrowserRouter, Switch, Route, useLocation} from "react-router-dom"; @@ -50,6 +51,9 @@ export default function Router({site, loggedIn}) { + + + diff --git a/assets/js/dashboard/stats/modals/filter.js b/assets/js/dashboard/stats/modals/filter.js new file mode 100644 index 000000000..427e5f66e --- /dev/null +++ b/assets/js/dashboard/stats/modals/filter.js @@ -0,0 +1,97 @@ +import React from "react"; +import { withRouter, Link } from 'react-router-dom' + +import Modal from './modal' +import { parseQuery, formattedFilters } from '../../query' + +class FilterModal extends React.Component { + constructor(props) { + super(props) + this.state = { + query: parseQuery(props.location.search, props.site), + selectedFilter: this.props.match.params.field || "", + updatedValue: "", + negated: false + } + + this.editableGoals = Object.keys(this.state.query.filters).filter(filter => !['goal', 'props'].includes(filter)) + } + + componentDidMount() { + + } + + renderBody() { + const { selectedFilter, negated, updatedValue, query } = this.state; + + const updatedQuery = new URLSearchParams(window.location.search) + const finalFilterValue = (['page', 'entry_page', 'exit_page'].includes(selectedFilter) && negated ? '!' : '') + updatedValue + const validQuery = this.editableGoals.includes(selectedFilter) && updatedValue + updatedQuery.set(selectedFilter, finalFilterValue) + + return ( + +

{query.filters[selectedFilter] ? 'Edit' : 'Add'} a Filter

+ +
+
+ + + {['page', 'entry_page', 'exit_page'].includes(selectedFilter) && +
+ +
+ } + + {selectedFilter && + { this.setState({ updatedValue: e.target.value }) }} + /> + } + + + {query.filters[selectedFilter] ? 'Update' : 'Add'} Filter + + +
+
+ ) + } + + render() { + return ( + + { this.renderBody()} + + ) + } +} + +export default withRouter(FilterModal) diff --git a/assets/js/dashboard/stats/modals/modal.js b/assets/js/dashboard/stats/modals/modal.js index 292b2d110..cd6603baa 100644 --- a/assets/js/dashboard/stats/modals/modal.js +++ b/assets/js/dashboard/stats/modals/modal.js @@ -47,7 +47,7 @@ class Modal extends React.Component {
-
+
{this.props.children}
diff --git a/lib/plausible/stats/clickhouse.ex b/lib/plausible/stats/clickhouse.ex index a525799dd..d07386a5a 100644 --- a/lib/plausible/stats/clickhouse.ex +++ b/lib/plausible/stats/clickhouse.ex @@ -257,13 +257,7 @@ defmodule Plausible.Stats.Clickhouse do from(s in referrers, where: s.referrer_source != "") end - referrers = - if query.filters["page"] do - page = query.filters["page"] - from(s in referrers, where: s.entry_page == ^page) - else - referrers - end + referrers = apply_page_as_entry_page(referrers, site, query) referrers = if include_details do @@ -326,13 +320,7 @@ defmodule Plausible.Stats.Clickhouse do end defp apply_page_as_entry_page(db_query, _site, query) do - page = query.filters["page"] - - if is_binary(page) do - from(s in db_query, where: s.entry_page == ^page) - else - db_query - end + include_path_filter_entry(db_query, query.filters["page"]) end def utm_mediums(site, query, limit \\ 9, page \\ 1, show_noref \\ false) do @@ -1153,21 +1141,9 @@ defmodule Plausible.Stats.Clickhouse do sessions_q end - sessions_q = - if query.filters["entry_page"] do - entry_page = query.filters["entry_page"] - from(s in sessions_q, where: s.entry_page == ^entry_page) - else - sessions_q - end + sessions_q = include_path_filter_entry(sessions_q, query.filters["entry_page"]) - sessions_q = - if query.filters["exit_page"] do - exit_page = query.filters["exit_page"] - from(s in sessions_q, where: s.exit_page == ^exit_page) - else - sessions_q - end + sessions_q = include_path_filter_exit(sessions_q, query.filters["exit_page"]) q = from(e in "events", @@ -1191,13 +1167,7 @@ defmodule Plausible.Stats.Clickhouse do q end - q = - if query.filters["page"] do - page = query.filters["page"] - from(e in q, where: e.pathname == ^page) - else - q - end + q = include_path_filter(q, query.filters["page"]) if query.filters["props"] do [{key, val}] = query.filters["props"] |> Enum.into([]) @@ -1347,21 +1317,9 @@ defmodule Plausible.Stats.Clickhouse do q end - q = - if query.filters["entry_page"] do - entry_page = query.filters["entry_page"] - from(s in q, where: s.entry_page == ^entry_page) - else - q - end + q = include_path_filter_entry(q, query.filters["entry_page"]) - q = - if query.filters["exit_page"] do - exit_page = query.filters["exit_page"] - from(s in q, where: s.exit_page == ^exit_page) - else - q - end + q = include_path_filter_exit(q, query.filters["exit_page"]) if query.filters["referrer"] do ref = query.filters["referrer"] @@ -1461,13 +1419,7 @@ defmodule Plausible.Stats.Clickhouse do q end - q = - if query.filters["page"] do - page = query.filters["page"] - from(e in q, where: e.pathname == ^page) - else - q - end + q = include_path_filter(q, query.filters["page"]) if query.filters["props"] do [{key, val}] = query.filters["props"] |> Enum.into([]) @@ -1547,12 +1499,9 @@ defmodule Plausible.Stats.Clickhouse do end if path do - if String.match?(path, ~r/\*/) do - path_regex = - "^#{path}\/?$" - |> String.replace(~r/\*\*/, ".*") - |> String.replace(~r/(? String.replace(~r/\*\*/, ".*") + |> String.replace(~r/(?