:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'
import { dayjs } from 'lib/dayjs'
import { dateMapping } from 'lib/utils/dateFilters'
import { formatDateRange } from 'lib/utils/datetime'
import { AnyPropertyFilter, DateMappingOption, PropertyFilterType, PropertyOperator } from '~/types'
export const WEB_ANALYTICS_PRE_AGGREGATED_ALLOWED_EVENT_PROPERTIES = [
'$host',
'$device_type',
'$browser',
'$os',
'$referring_domain',
'$geoip_country_code',
'$geoip_city_name',
'$geoip_subdivision_1_code',
'$geoip_subdivision_1_name',
'$geoip_time_zone',
'$pathname',
'metadata.loggedIn',
'metadata.backend',
]
export const WEB_ANALYTICS_PRE_AGGREGATED_ALLOWED_SESSION_PROPERTIES = [
'$entry_pathname',
'$end_pathname',
'$entry_utm_source',
'$entry_utm_medium',
'$entry_utm_campaign',
'$entry_utm_term',
'$entry_utm_content',
'$channel_type',
]
export const WEB_ANALYTICS_PRE_AGGREGATED_PROPERTY_ALLOW_LIST = {
[TaxonomicFilterGroupType.EventProperties]: WEB_ANALYTICS_PRE_AGGREGATED_ALLOWED_EVENT_PROPERTIES,
[TaxonomicFilterGroupType.SessionProperties]: WEB_ANALYTICS_PRE_AGGREGATED_ALLOWED_SESSION_PROPERTIES,
}
const last28Days: DateMappingOption = {
key: 'Last 28 days',
values: ['-28d'],
getFormattedDate: (date: dayjs.Dayjs): string => formatDateRange(date.subtract(28, 'd'), date.endOf('d')),
defaultInterval: 'day',
}
const last30DaysIndex = dateMapping.findIndex((option) => option.key === 'Last 30 days')
export const webAnalyticsDateMapping: DateMappingOption[] = [
...dateMapping.slice(0, last30DaysIndex),
last28Days,
...dateMapping.slice(last30DaysIndex),
]
export const PROPERTY_CURRENT_URL = '$current_url' as const
export const PROPERTY_HOST = '$host' as const
export const PROPERTY_PATHNAME = '$pathname' as const
export const hasURLSearchParams = (filter: AnyPropertyFilter): boolean => {
if (filter.key !== PROPERTY_CURRENT_URL || filter.type !== PropertyFilterType.Event) {
return false
}
try {
const urlValue = Array.isArray(filter.value) ? filter.value[0] : filter.value
if (typeof urlValue === 'string') {
const url = new URL(urlValue)
return !!(url.search && url.search !== '?')
}
} catch {
return true
}
return false
}
export const convertCurrentURLFilter = (
filter: AnyPropertyFilter
): { key: string; value: string; operator: PropertyOperator; type: PropertyFilterType.Event }[] | null => {
if (filter.key !== PROPERTY_CURRENT_URL || filter.type !== PropertyFilterType.Event) {
return null
}
try {
const urlValue = Array.isArray(filter.value) ? filter.value[0] : filter.value
if (typeof urlValue === 'string') {
const url = new URL(urlValue)
if (!url.search || url.search === '?') {
return [
{
key: PROPERTY_HOST,
value: url.host,
operator: filter.operator,
type: PropertyFilterType.Event,
},
{
key: PROPERTY_PATHNAME,
value: url.pathname,
operator: filter.operator,
type: PropertyFilterType.Event,
},
]
}
}
} catch {
// URL parsing failed
}
return null
}