Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web-analytics): Add guess for initial host filter #18818

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion frontend/src/scenes/web-analytics/WebDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ const Filters = (): JSX.Element => {
)
}

const Tiles = (): JSX.Element => {
const Tiles = (): JSX.Element | null => {
const { tiles } = useValues(webAnalyticsLogic)

if (!tiles) {
return null
}

return (
<div className="mt-8 grid grid-cols-1 md:grid-cols-12 gap-x-4 gap-y-10">
{tiles.map((tile, i) => {
Expand Down
57 changes: 55 additions & 2 deletions frontend/src/scenes/web-analytics/webAnalyticsLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import api from 'lib/api'
import { RETENTION_FIRST_TIME, STALE_EVENT_SECONDS } from 'lib/constants'
import { dayjs } from 'lib/dayjs'
import { isNotNil } from 'lib/utils'
import * as tldts from 'tldts'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This potentially adds about 100KB of JS. Might be worth double checking if so... and perhaps just importing { getSubdomain } solves this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that this doesn't help much, as it'll still need the big list of 3LDs etc. Will try it anyway!


import {
NodeKind,
Expand Down Expand Up @@ -128,10 +129,16 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
}),
reducers({
webAnalyticsFilters: [
initialWebAnalyticsFilter,
null as WebAnalyticsPropertyFilters | null,
{
loadInitialFiltersSuccess: (_, { initialFilters }) => {
return initialFilters
},
setWebAnalyticsFilters: (_, { webAnalyticsFilters }) => webAnalyticsFilters,
togglePropertyFilter: (oldPropertyFilters, { key, value, type }): WebAnalyticsPropertyFilters => {
if (!oldPropertyFilters) {
oldPropertyFilters = []
}
const similarFilterExists = oldPropertyFilters.some(
(f) => f.type === type && f.key === key && f.operator === PropertyOperator.Exact
)
Expand Down Expand Up @@ -244,7 +251,11 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
dateTo,
isGreaterThanMd: boolean,
shouldShowGeographyTile
): WebDashboardTile[] => {
): WebDashboardTile[] | null => {
if (!webAnalyticsFilters) {
return null
}

const dateRange = {
date_from: dateFrom,
date_to: dateTo,
Expand Down Expand Up @@ -734,12 +745,54 @@ export const webAnalyticsLogic = kea<webAnalyticsLogicType>([
return !!geoIpPluginConfig && geoIpPluginConfig.enabled
},
},
initialFilters: {
_default: null as boolean | null,
loadInitialFilters: async (): Promise<WebAnalyticsPropertyFilters> => {
// This code guesses which is the marketing domain based on whether it's www or apex. We use this to set
// the $host filter's initial state. Later on we'll have this as config that people can set, but for now
// we try to do the smart thing without people needing to do anything
const params = new URLSearchParams([
['key', '$host'],
['event_name', '$pageview'],
['event_name', '$pageleave'],
['event_name', '$autocapture'],
])
let propertiesResponse: unknown
try {
propertiesResponse = await api.get(`api/event/values/?${params}`)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor remark: we do have some kind of API composition system under api.ts, freeing us from URL constants. Might be worth hooking into it? 🤷 through if you copied this from elsewhere.

} catch {
// ignore
}
if (!propertiesResponse || !Array.isArray(propertiesResponse) || propertiesResponse.length <= 1) {
return []
}
const hosts: string[] = propertiesResponse.map((x) => x.name)
// at this point we have multiple hosts, pick the ones that are www or apex
const wwwOrApex = hosts.filter((host) => {
const subdomain = tldts.getSubdomain(host)
return subdomain === '' || subdomain === 'www'
})
Comment on lines +771 to +774
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps these subdomains on their own hold value? Suggestion that's non blocking: rename this to a subdomains loader, and use a selector to derive the filters.

if (wwwOrApex.length > 0 && wwwOrApex.length !== hosts.length) {
return [
{
type: PropertyFilterType.Event,
key: '$host',
value: wwwOrApex,
operator: PropertyOperator.Exact,
},
]
} else {
return []
}
},
},
})),

// start the loaders after mounting the logic
afterMount(({ actions }) => {
actions.loadStatusCheck()
actions.loadShouldShowGeographyTile()
actions.loadInitialFilters()
}),
windowValues({
isGreaterThanMd: (window: Window) => window.innerWidth > 768,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
"resize-observer-polyfill": "^1.5.1",
"rrweb": "^2.0.0-alpha.11",
"sass": "^1.26.2",
"tldts": "^6.0.21",
"use-debounce": "^9.0.3",
"use-resize-observer": "^8.0.0",
"wildcard-match": "^5.1.2",
Expand Down
16 changes: 15 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.