Skip to content

Commit

Permalink
web: add a load event for expand/collapse (#5219)
Browse files Browse the repository at this point in the history
intended to mirror the similar load event for star/unstar
  • Loading branch information
nicks committed Nov 24, 2021
1 parent 62eb5bc commit ac711d0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
14 changes: 12 additions & 2 deletions web/src/ResourceGroupsContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,23 @@ describe("ResourceGroupsContext", () => {
<TestConsumer labelName="test" />
</ResourceGroupsContextProvider>
)
let loadIncr = {
name: "ui.web.resourceGroup",
tags: { action: AnalyticsAction.Load, expanded: "1", collapsed: "0" },
}
expectIncrs(loadIncr)
clickButton()
// Expect the "collapse" action value because the test label group is expanded
// when it's clicked on and the "grid" type value because it's hardcoded in the
// test component
expectIncrs({
expectIncrs(loadIncr, {
name: "ui.web.resourceGroup",
tags: { action: AnalyticsAction.Collapse, type: AnalyticsType.Grid },
tags: {
action: AnalyticsAction.Collapse,
type: AnalyticsType.Grid,
expanded: "1",
collapsed: "0",
},
})
})
})
Expand Down
36 changes: 33 additions & 3 deletions web/src/ResourceGroupsContext.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createContext, PropsWithChildren, useContext } from "react"
import { AnalyticsAction, AnalyticsType, incr } from "./analytics"
import { createContext, PropsWithChildren, useContext, useEffect } from "react"
import { AnalyticsAction, AnalyticsType, incr, Tags } from "./analytics"
import { usePersistentState } from "./LocalStorage"

export type GroupState = { expanded: boolean }
Expand Down Expand Up @@ -34,6 +34,22 @@ export function useResourceGroups(): ResourceGroupsContext {
return useContext(resourceGroupsContext)
}

function getTags(groups: GroupsState): Tags {
let expandCount = 0
let collapseCount = 0
Object.values(groups).forEach((group) => {
if (group.expanded) {
expandCount++
} else {
collapseCount++
}
})
return {
expanded: String(expandCount),
collapsed: String(collapseCount),
}
}

export function ResourceGroupsContextProvider(
props: PropsWithChildren<{ initialValuesForTesting?: GroupsState }>
) {
Expand All @@ -42,6 +58,16 @@ export function ResourceGroupsContextProvider(
"resource-groups",
defaultPersistentValue
)
let analyticsTags = getTags(groups)

useEffect(() => {
incr("ui.web.resourceGroup", {
action: AnalyticsAction.Load,
...analyticsTags,
})
// empty deps because we only want to report nce per app load
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

function toggleGroupExpanded(groupLabel: string, page: AnalyticsType) {
const currentGroupState = groups[groupLabel] ?? { ...DEFAULT_GROUP_STATE }
Expand All @@ -53,7 +79,11 @@ export function ResourceGroupsContextProvider(
const action = nextGroupState.expanded
? AnalyticsAction.Expand
: AnalyticsAction.Collapse
incr("ui.web.resourceGroup", { action, type: page })
incr("ui.web.resourceGroup", {
action,
type: page,
...analyticsTags,
})

setGroups((prevState) => {
return {
Expand Down

0 comments on commit ac711d0

Please sign in to comment.