Skip to content

Commit fa53c1e

Browse files
authored
Add react_app and page_type to hydro-analytics events (#59437)
1 parent 3425e6b commit fa53c1e

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/events/components/hydro-analytics.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ const AUTO_COLLECTED_FIELDS = new Set([
5757

5858
/**
5959
* Flatten a nested event body into a single-level context object,
60-
* excluding fields that hydro-analytics-client already auto-collects.
60+
* excluding fields that hydro-analytics-client already auto-collects,
61+
* and adding fields required for analytics_v0_page_view compatibility.
6162
*/
6263
export function prepareData(body: Record<string, unknown>): {
6364
type: string
@@ -74,6 +75,16 @@ export function prepareData(body: Record<string, unknown>): {
7475
.filter(([key]) => !AUTO_COLLECTED_FIELDS.has(key))
7576
.map(([key, value]) => [key, String(value)]),
7677
)
78+
79+
// Add fields required for analytics_v0_page_view compatibility
80+
// These are expected by the BI team's dashboards
81+
context.react_app = 'docs'
82+
// Preserve our page_type as docs_page_type, then set page_type to 'marketing' for BI
83+
if (context.page_type) {
84+
context.docs_page_type = context.page_type
85+
}
86+
context.page_type = 'marketing'
87+
7788
return { type: typeof type === 'string' ? type : 'unknown', context }
7889
}
7990

src/events/tests/hydro-analytics.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,33 @@ describe('prepareData', () => {
9999
expect(result.type).toBe('page')
100100
expect(result.context.exit_scroll_length).toBe('0.5')
101101
})
102+
103+
test('adds react_app field for analytics_v0_page_view compatibility', () => {
104+
const body = {
105+
type: 'page',
106+
context: { event_id: '123' },
107+
}
108+
const result = prepareData(body)
109+
expect(result.context.react_app).toBe('docs')
110+
})
111+
112+
test('adds page_type field when not present', () => {
113+
const body = {
114+
type: 'page',
115+
context: { event_id: '123' },
116+
}
117+
const result = prepareData(body)
118+
expect(result.context.page_type).toBe('marketing')
119+
expect(result.context.docs_page_type).toBeUndefined()
120+
})
121+
122+
test('preserves existing page_type as docs_page_type and sets page_type to marketing', () => {
123+
const body = {
124+
type: 'page',
125+
context: { event_id: '123', page_type: 'article' },
126+
}
127+
const result = prepareData(body)
128+
expect(result.context.page_type).toBe('marketing')
129+
expect(result.context.docs_page_type).toBe('article')
130+
})
102131
})

0 commit comments

Comments
 (0)