Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Add 'contains' and 'not contains' filters

Blue Mousef82b29beaaf2
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 75563a0ce..44a46ad05 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -929,7 +929,7 @@ export class AnalyticsService {
const converted = _reduce(
parsed,
(prev, curr) => {
- const { column, filter, isExclusive = false } = curr
+ const { column, filter, isExclusive = false, isContains = false } = curr
if (column === 'ev' && ignoreEV) {
return prev
@@ -966,7 +966,7 @@ export class AnalyticsService {
// }
// res.push({ filter: encoded, isExclusive })
- res.push({ filter: f, isExclusive })
+ res.push({ filter: f, isExclusive, isContains })
}
} else {
// let encoded = filter
@@ -976,7 +976,7 @@ export class AnalyticsService {
// }
// res.push({ filter: encoded, isExclusive })
- res.push({ filter, isExclusive })
+ res.push({ filter, isExclusive, isContains })
}
if (prev[column]) {
@@ -1001,7 +1001,7 @@ export class AnalyticsService {
query += ' OR '
}
- const { filter, isExclusive } = converted[column][f]
+ const { filter, isExclusive, isContains } = converted[column][f]
let sqlColumn = column
let isArrayDataset = false
@@ -1014,7 +1014,9 @@ export class AnalyticsService {
column === 'entryPage'
? 'argMin(pg, created)'
: 'argMax(pg, created)'
- const subQueryForPages = `SELECT psid FROM (SELECT psid, ${pageSelector} as page FROM analytics WHERE pid = {pid:FixedString(12)} GROUP BY psid) WHERE page = {${param}:String}`
+ const subQueryForPages = isContains
+ ? `SELECT psid FROM (SELECT psid, ${pageSelector} as page FROM analytics WHERE pid = {pid:FixedString(12)} GROUP BY psid) WHERE page ILIKE concat('%', {${param}:String}, '%')`
+ : `SELECT psid FROM (SELECT psid, ${pageSelector} as page FROM analytics WHERE pid = {pid:FixedString(12)} GROUP BY psid) WHERE page = {${param}:String}`
// For exclusive filter (isNot) we exclude sessions with matching entry/exit page
query += `psid ${isExclusive ? 'NOT IN' : 'IN'} (${subQueryForPages})`
@@ -1027,9 +1029,15 @@ export class AnalyticsService {
const keyParam = `qfk_${col}_${f}`
params[keyParam] = key
- query += `indexOf(meta.key, {${keyParam}:String}) > 0 AND meta.value[indexOf(meta.key, {${keyParam}:String})] ${
- isExclusive ? '!= ' : '='
- } {${param}:String}`
+ if (isContains) {
+ query += `indexOf(meta.key, {${keyParam}:String}) > 0 AND ${
+ isExclusive ? 'NOT ' : ''
+ }(meta.value[indexOf(meta.key, {${keyParam}:String})] ILIKE concat('%', {${param}:String}, '%'))`
+ } else {
+ query += `indexOf(meta.key, {${keyParam}:String}) > 0 AND meta.value[indexOf(meta.key, {${keyParam}:String})] ${
+ isExclusive ? '!= ' : '='
+ } {${param}:String}`
+ }
continue
}
@@ -1058,11 +1066,20 @@ export class AnalyticsService {
continue
}
- query += isArrayDataset
- ? `indexOf(${sqlColumn}, {${param}:String}) ${
- isExclusive ? '=' : '>'
- } 0`
- : `${isExclusive ? 'NOT ' : ''}${sqlColumn} = {${param}:String}`
+ if (isContains) {
+ if (isArrayDataset) {
+ // any array element contains substring (case-insensitive)
+ query += `${isExclusive ? 'NOT ' : ''}arrayExists(v -> positionCaseInsensitive(v, {${param}:String}) > 0, ${sqlColumn})`
+ } else {
+ query += `${isExclusive ? 'NOT ' : ''}${sqlColumn} ILIKE concat('%', {${param}:String}, '%')`
+ }
+ } else {
+ query += isArrayDataset
+ ? `indexOf(${sqlColumn}, {${param}:String}) ${
+ isExclusive ? '=' : '>'
+ } 0`
+ : `${isExclusive ? 'NOT ' : ''}${sqlColumn} = {${param}:String}`
+ }
}
query += ')'
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index b696a208d..2d3a55219 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -910,7 +910,7 @@ export class AnalyticsService {
const converted = _reduce(
parsed,
(prev, curr) => {
- const { column, filter, isExclusive = false } = curr
+ const { column, filter, isExclusive = false, isContains = false } = curr
if (column === 'ev' && ignoreEV) {
return prev
@@ -947,7 +947,7 @@ export class AnalyticsService {
// }
// res.push({ filter: encoded, isExclusive })
- res.push({ filter: f, isExclusive })
+ res.push({ filter: f, isExclusive, isContains })
}
} else {
// let encoded = filter
@@ -957,7 +957,7 @@ export class AnalyticsService {
// }
// res.push({ filter: encoded, isExclusive })
- res.push({ filter, isExclusive })
+ res.push({ filter, isExclusive, isContains })
}
if (prev[column]) {
@@ -982,7 +982,7 @@ export class AnalyticsService {
query += ' OR '
}
- const { filter, isExclusive } = converted[column][f]
+ const { filter, isExclusive, isContains } = converted[column][f]
let sqlColumn = column
let isArrayDataset = false
@@ -995,7 +995,9 @@ export class AnalyticsService {
column === 'entryPage'
? 'argMin(pg, created)'
: 'argMax(pg, created)'
- const subQueryForPages = `SELECT psid FROM (SELECT psid, ${pageSelector} as page FROM analytics WHERE pid = {pid:FixedString(12)} GROUP BY psid) WHERE page = {${param}:String}`
+ const subQueryForPages = isContains
+ ? `SELECT psid FROM (SELECT psid, ${pageSelector} as page FROM analytics WHERE pid = {pid:FixedString(12)} GROUP BY psid) WHERE page ILIKE concat('%', {${param}:String}, '%')`
+ : `SELECT psid FROM (SELECT psid, ${pageSelector} as page FROM analytics WHERE pid = {pid:FixedString(12)} GROUP BY psid) WHERE page = {${param}:String}`
// For exclusive filter (isNot) we exclude sessions with matching entry/exit page
query += `psid ${isExclusive ? 'NOT IN' : 'IN'} (${subQueryForPages})`
@@ -1007,10 +1009,15 @@ export class AnalyticsService {
const key = column.replace(/^ev:key:/, '').replace(/^tag:key:/, '')
const keyParam = `qfk_${col}_${f}`
params[keyParam] = key
-
- query += `indexOf(meta.key, {${keyParam}:String}) > 0 AND meta.value[indexOf(meta.key, {${keyParam}:String})] ${
- isExclusive ? '!= ' : '='
- } {${param}:String}`
+ if (isContains) {
+ query += `indexOf(meta.key, {${keyParam}:String}) > 0 AND ${
+ isExclusive ? 'NOT ' : ''
+ }(meta.value[indexOf(meta.key, {${keyParam}:String})] ILIKE concat('%', {${param}:String}, '%'))`
+ } else {
+ query += `indexOf(meta.key, {${keyParam}:String}) > 0 AND meta.value[indexOf(meta.key, {${keyParam}:String})] ${
+ isExclusive ? '!= ' : '='
+ } {${param}:String}`
+ }
continue
}
@@ -1039,11 +1046,19 @@ export class AnalyticsService {
continue
}
- query += isArrayDataset
- ? `indexOf(${sqlColumn}, {${param}:String}) ${
- isExclusive ? '=' : '>'
- } 0`
- : `${isExclusive ? 'NOT ' : ''}${sqlColumn} = {${param}:String}`
+ if (isContains) {
+ if (isArrayDataset) {
+ query += `${isExclusive ? 'NOT ' : ''}arrayExists(v -> positionCaseInsensitive(v, {${param}:String}) > 0, ${sqlColumn})`
+ } else {
+ query += `${isExclusive ? 'NOT ' : ''}${sqlColumn} ILIKE concat('%', {${param}:String}, '%')`
+ }
+ } else {
+ query += isArrayDataset
+ ? `indexOf(${sqlColumn}, {${param}:String}) ${
+ isExclusive ? '=' : '>'
+ } 0`
+ : `${isExclusive ? 'NOT ' : ''}${sqlColumn} = {${param}:String}`
+ }
}
query += ')'
diff --git a/web/app/pages/Project/View/ViewProject.tsx b/web/app/pages/Project/View/ViewProject.tsx
index d468fa7a1..ae87d289d 100644
--- a/web/app/pages/Project/View/ViewProject.tsx
+++ b/web/app/pages/Project/View/ViewProject.tsx
@@ -2403,7 +2403,11 @@ const ViewProjectContent = () => {
let searchString = ''
if (isFilterActive) {
- newSearchParams.delete(column)
+ // Remove any operator variant for this column/value
+ newSearchParams.delete(column, value)
+ newSearchParams.delete(`!${column}`, value)
+ newSearchParams.delete(`~${column}`, value)
+ newSearchParams.delete(`^${column}`, value)
searchString = newSearchParams.toString()
} else {
newSearchParams.append(column, value)
diff --git a/web/app/pages/Project/View/components/AddAViewModal.tsx b/web/app/pages/Project/View/components/AddAViewModal.tsx
index 1726763d9..ccc034371 100644
--- a/web/app/pages/Project/View/components/AddAViewModal.tsx
+++ b/web/app/pages/Project/View/components/AddAViewModal.tsx
@@ -265,6 +265,7 @@ const AddAViewModal = ({ onSubmit, showModal, setShowModal, tnMapping, defaultVi
column: filterType,
filter: processedItem,
isExclusive: false,
+ isContains: false,
},
])
}
@@ -391,7 +392,7 @@ const AddAViewModal = ({ onSubmit, showModal, setShowModal, tnMapping, defaultVi
</>
) : null}
<div className='mt-2'>
- {_map(activeFilters, ({ filter, column, isExclusive }) => (
+ {_map(activeFilters, ({ filter, column, isExclusive, isContains }) => (
<Filter
key={`${column}-${filter}`}
onRemoveFilter={(e) => {
@@ -410,11 +411,23 @@ const AddAViewModal = ({ onSubmit, showModal, setShowModal, tnMapping, defaultVi
setActiveFilters((prevFilters: any) => {
return _map(prevFilters, (item) => {
if (item.column === column && item.filter === filter) {
- return {
- column,
- filter,
- isExclusive: !isExclusive,
+ let nextContains = isContains
+ let nextExclusive = isExclusive
+ if (!isContains && !isExclusive) {
+ nextContains = false
+ nextExclusive = true
+ } else if (!isContains && isExclusive) {
+ nextContains = true
+ nextExclusive = false
+ } else if (isContains && !isExclusive) {
+ nextContains = true
+ nextExclusive = true
+ } else {
+ nextContains = false
+ nextExclusive = false
}
+
+ return { column, filter, isExclusive: nextExclusive, isContains: nextContains }
}
return item
@@ -425,6 +438,7 @@ const AddAViewModal = ({ onSubmit, showModal, setShowModal, tnMapping, defaultVi
column={column}
filter={filter}
tnMapping={tnMapping}
+ isContains={isContains}
canChangeExclusive
removable
/>
diff --git a/web/app/pages/Project/View/components/Filters.tsx b/web/app/pages/Project/View/components/Filters.tsx
index beeab67d2..7de977a59 100644
--- a/web/app/pages/Project/View/components/Filters.tsx
+++ b/web/app/pages/Project/View/components/Filters.tsx
@@ -25,6 +25,7 @@ interface FilterProps {
removable?: boolean
onChangeExclusive?: (e: MouseEvent) => void
onRemoveFilter?: (e: MouseEvent) => void
+ isContains?: boolean
}
export const Filter = ({
@@ -36,6 +37,7 @@ export const Filter = ({
removable,
onChangeExclusive,
onRemoveFilter,
+ isContains,
}: FilterProps) => {
const { dataLoading } = useViewProjectContext()
const [searchParams] = useSearchParams()
@@ -88,15 +90,44 @@ export const Filter = ({
const createRemoveFilterPath = () => {
const newSearchParams = new URLSearchParams(searchParams.toString())
- const paramKeyInUrl = isExclusive ? `!${column}` : column
+ const paramKeyInUrl = isContains ? (isExclusive ? `^${column}` : `~${column}`) : isExclusive ? `!${column}` : column
newSearchParams.delete(paramKeyInUrl, filter)
return { search: newSearchParams.toString() }
}
const createToggleExclusivePath = () => {
const newSearchParams = new URLSearchParams(searchParams.toString())
- const oldParamKey = isExclusive ? `!${column}` : column
- const newParamKey = isExclusive ? column : `!${column}`
+
+ const getKey = (contains: boolean, exclusive: boolean) =>
+ contains ? (exclusive ? `^${column}` : `~${column}`) : exclusive ? `!${column}` : column
+
+ const c = Boolean(isContains)
+ const e = Boolean(isExclusive)
+
+ const oldParamKey = getKey(c, e)
+
+ let nextContains = c
+ let nextExclusive = e
+
+ if (!c && !e) {
+ // is -> is not
+ nextContains = false
+ nextExclusive = true
+ } else if (!c && e) {
+ // is not -> contains
+ nextContains = true
+ nextExclusive = false
+ } else if (c && !e) {
+ // contains -> not contains
+ nextContains = true
+ nextExclusive = true
+ } else {
+ // not contains -> is
+ nextContains = false
+ nextExclusive = false
+ }
+
+ const newParamKey = getKey(nextContains, nextExclusive)
newSearchParams.delete(oldParamKey, filter)
newSearchParams.append(newParamKey, filter)
@@ -132,21 +163,43 @@ export const Filter = ({
}
onChangeExclusive?.(e)
}}
- title={
- isExclusive
- ? t('project.toggleFilterToIs', { column: displayColumn, filter: truncatedFilter })
- : t('project.toggleFilterToIsNot', { column: displayColumn, filter: truncatedFilter })
- }
- aria-label={
- isExclusive
- ? t('project.toggleFilterToIs', { column: displayColumn, filter: truncatedFilter })
- : t('project.toggleFilterToIsNot', { column: displayColumn, filter: truncatedFilter })
- }
+ title={(() => {
+ const c = Boolean(isContains)
+ const e = Boolean(isExclusive)
+ const nextKey =
+ !c && !e
+ ? 'toggleFilterToIsNot'
+ : !c && e
+ ? 'toggleFilterToContains'
+ : c && !e
+ ? 'toggleFilterToNotContains'
+ : 'toggleFilterToIs'
+ return t(`project.${nextKey}`, { column: displayColumn, filter: truncatedFilter })
+ })()}
+ aria-label={(() => {
+ const c = Boolean(isContains)
+ const e = Boolean(isExclusive)
+ const nextKey =
+ !c && !e
+ ? 'toggleFilterToIsNot'
+ : !c && e
+ ? 'toggleFilterToContains'
+ : c && !e
+ ? 'toggleFilterToNotContains'
+ : 'toggleFilterToIs'
+ return t(`project.${nextKey}`, { column: displayColumn, filter: truncatedFilter })
+ })()}
>
- {t(`common.${isExclusive ? 'isNot' : 'is'}`)}
+ {isContains
+ ? t(`project.contains.${isExclusive ? 'not' : 'is'}`)
+ : t(`common.${isExclusive ? 'isNot' : 'is'}`)}
</Link>
) : (
- <span>{t(`common.${isExclusive ? 'isNot' : 'is'}`)}</span>
+ <span>
+ {isContains
+ ? t(`project.contains.${isExclusive ? 'not' : 'is'}`)
+ : t(`common.${isExclusive ? 'isNot' : 'is'}`)}
+ </span>
)}
&nbsp;&quot;
{truncatedFilter}
@@ -195,7 +248,7 @@ const Filters = ({ tnMapping }: FiltersProps) => {
for (const [key] of entries) {
let processedKey = key
- if (key.startsWith('!')) {
+ if (key.startsWith('!') || key.startsWith('~') || key.startsWith('^')) {
processedKey = key.substring(1)
}
diff --git a/web/app/pages/Project/View/components/SearchFilters.tsx b/web/app/pages/Project/View/components/SearchFilters.tsx
index 65d2706eb..f7a74e318 100644
--- a/web/app/pages/Project/View/components/SearchFilters.tsx
+++ b/web/app/pages/Project/View/components/SearchFilters.tsx
@@ -53,7 +53,7 @@ export const getFiltersUrlParams = (
// Preserve non-filter parameters from the current URL
for (const [key, value] of currentUrlParams.entries()) {
let processedKey = key
- if (key.startsWith('!')) {
+ if (key.startsWith('!') || key.startsWith('~') || key.startsWith('^')) {
processedKey = key.substring(1)
}
if (!isFilterValid(processedKey, true)) {
@@ -62,7 +62,10 @@ export const getFiltersUrlParams = (
}
resultingFilters.forEach((f) => {
- const filterKey = f.isExclusive ? `!${f.column}` : f.column
+ let filterKey = f.isExclusive ? `!${f.column}` : f.column
+ if (f.isContains) {
+ filterKey = f.isExclusive ? `^${f.column}` : `~${f.column}`
+ }
newUrlParams.append(filterKey, f.filter)
})
@@ -133,6 +136,7 @@ const SearchFilters = ({ showModal, setShowModal, tnMapping, filters, type }: Se
column: filterType,
filter: processedItem,
isExclusive: false,
+ isContains: false,
},
])
}
@@ -207,7 +211,7 @@ const SearchFilters = ({ showModal, setShowModal, tnMapping, filters, type }: Se
</>
) : null}
<div className='mt-2'>
- {_map(activeFilters, ({ filter, column, isExclusive }) => (
+ {_map(activeFilters, ({ filter, column, isExclusive, isContains }) => (
<Filter
key={`${column}-${filter}`}
onRemoveFilter={(e) => {
@@ -226,11 +230,24 @@ const SearchFilters = ({ showModal, setShowModal, tnMapping, filters, type }: Se
setActiveFilters((prevFilters: any) => {
return _map(prevFilters, (item) => {
if (item.column === column && item.filter === filter) {
- return {
- column,
- filter,
- isExclusive: !isExclusive,
+ // Cycle: is -> is not -> contains -> not contains -> is
+ let nextContains = isContains
+ let nextExclusive = isExclusive
+ if (!isContains && !isExclusive) {
+ nextContains = false
+ nextExclusive = true
+ } else if (!isContains && isExclusive) {
+ nextContains = true
+ nextExclusive = false
+ } else if (isContains && !isExclusive) {
+ nextContains = true
+ nextExclusive = true
+ } else {
+ nextContains = false
+ nextExclusive = false
}
+
+ return { column, filter, isExclusive: nextExclusive, isContains: nextContains }
}
return item
@@ -241,6 +258,7 @@ const SearchFilters = ({ showModal, setShowModal, tnMapping, filters, type }: Se
column={column}
filter={filter}
tnMapping={tnMapping}
+ isContains={isContains}
canChangeExclusive
removable
/>
diff --git a/web/app/pages/Project/View/interfaces/traffic.ts b/web/app/pages/Project/View/interfaces/traffic.ts
index aa1321064..e4da7a052 100644
--- a/web/app/pages/Project/View/interfaces/traffic.ts
+++ b/web/app/pages/Project/View/interfaces/traffic.ts
@@ -4,6 +4,8 @@ export interface Filter {
column: string
filter: string
isExclusive: boolean
+ // When true, this filter uses substring match (contains/not contains) instead of equality
+ isContains?: boolean
}
export enum ProjectViewCustomEventMetaValueType {
diff --git a/web/app/pages/Project/View/utils/filters.tsx b/web/app/pages/Project/View/utils/filters.tsx
index 2b8b47534..bb59dbbc6 100644
--- a/web/app/pages/Project/View/utils/filters.tsx
+++ b/web/app/pages/Project/View/utils/filters.tsx
@@ -74,11 +74,17 @@ export const filterInvalidPreferences = (
}
export const isFilterValid = (filter: string, checkDynamicFilters = false) => {
- if (_includes(validFilters, filter)) {
+ // normalise by removing operator prefixes from the URL key
+ let normalised = filter
+ if (_startsWith(normalised, '!') || _startsWith(normalised, '~') || _startsWith(normalised, '^')) {
+ normalised = normalised.substring(1)
+ }
+
+ if (_includes(validFilters, normalised)) {
return true
}
- if (checkDynamicFilters && _some(validDynamicFilters, (prefix) => _startsWith(filter, prefix))) {
+ if (checkDynamicFilters && _some(validDynamicFilters, (prefix) => _startsWith(normalised, prefix))) {
return true
}
@@ -94,10 +100,18 @@ export const parseFilters = (searchParams: URLSearchParams): Filter[] => {
let actualColumn = key
let isExclusive = false
+ let isContains = false
if (key.startsWith('!')) {
isExclusive = true
actualColumn = key.substring(1)
+ } else if (key.startsWith('~')) {
+ isContains = true
+ actualColumn = key.substring(1)
+ } else if (key.startsWith('^')) {
+ isExclusive = true
+ isContains = true
+ actualColumn = key.substring(1)
}
if (!isFilterValid(actualColumn, true)) {
@@ -108,6 +122,7 @@ export const parseFilters = (searchParams: URLSearchParams): Filter[] => {
column: actualColumn,
filter: value,
isExclusive: isExclusive,
+ isContains,
})
}
diff --git a/web/public/locales/en.json b/web/public/locales/en.json
index 323f09901..2a40b67b1 100644
--- a/web/public/locales/en.json
+++ b/web/public/locales/en.json
@@ -925,8 +925,10 @@
"addAMetric": "Add a metric",
"customEvent": "Custom event",
"removeFilter": "Remove filter",
- "toggleFilterToIs": "Change filter from '{{column}} is not \"{{filter}}\"' to '{{column}} is \"{{filter}}\"'",
- "toggleFilterToIsNot": "Change filter from '{{column}} is \"{{filter}}\"' to '{{column}} is not \"{{filter}}\"'",
+ "toggleFilterToIs": "Change filter to '{{column}} is \"{{filter}}\"'",
+ "toggleFilterToIsNot": "Change filter to '{{column}} is not \"{{filter}}\"'",
+ "toggleFilterToContains": "Change filter to '{{column}} contains \"{{filter}}\"'",
+ "toggleFilterToNotContains": "Change filter to '{{column}} not contains \"{{filter}}\"'",
"location": "Location",
"pages": "Pages",
"devices": "Devices",
@@ -1102,6 +1104,10 @@
"codeExample": "Or insert the following code into your website to track your events:",
"metricVis": "Metrics visualisation",
"filters": "Filters",
+ "contains": {
+ "is": "contains",
+ "not": "not contains"
+ },
"showResolved": "Show resolved",
"resolve": "Resolve",
"markAsActive": "Mark as active",