Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4d0e9c7

Browse files
authoredAug 26, 2021
Add new fields to analytics event context: status, page_type, page_document_type (github#21115)
* Add new fields to analytics event context: status, page-type, page-document-type * Update schema-event.js * Typescript * Add status meta to error pages * Update DefaultLayout.tsx * Update DefaultLayout.tsx * Update building-and-testing-nodejs-or-python.tsx
1 parent 5cf83e5 commit 4d0e9c7

File tree

12 files changed

+55
-30
lines changed

12 files changed

+55
-30
lines changed
 

‎components/DefaultLayout.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { useTranslation } from './hooks/useTranslation'
1111

1212
type Props = { children?: React.ReactNode }
1313
export const DefaultLayout = (props: Props) => {
14-
const { page, error, isHomepageVersion, currentPathWithoutLanguage, fullUrl } = useMainContext()
14+
const { page, error, isHomepageVersion, currentPathWithoutLanguage, fullUrl, status } = useMainContext()
1515
const { t } = useTranslation('errors')
1616
return (
1717
<div className="d-lg-flex">
@@ -26,6 +26,7 @@ export const DefaultLayout = (props: Props) => {
2626
{/* For Google and Bots */}
2727
{page.introPlainText && <meta name="description" content={page.introPlainText} />}
2828

29+
{/* For local site search indexing */}
2930
{page.topics.length > 0 && <meta name="keywords" content={page.topics.join(',')} />}
3031

3132
{page.hidden && <meta name="robots" content="noindex" />}
@@ -41,6 +42,11 @@ export const DefaultLayout = (props: Props) => {
4142
)
4243
})}
4344

45+
{/* For analytics events */}
46+
{status && <meta name="status" content={status.toString()} />}
47+
{page.type && <meta name="page-type" content={page.type} />}
48+
{page.documentType && <meta name="page-document-type" content={page.documentType} />}
49+
4450
{page.fullTitle && (
4551
<>
4652
<meta property="og:site_name" content="GitHub Docs" />

‎components/GenericError.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export function GenericError() {
1212
<div className="min-h-screen d-flex flex-column">
1313
<Head>
1414
<title>GitHub Documentation</title>
15+
<meta name="status" content="500" />
1516
</Head>
1617

1718
<SimpleHeader />

‎components/context/MainContext.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ export type MainContextT = {
8484
featureFlags: FeatureFlags
8585
page: {
8686
documentType: string
87+
type?: string
8788
languageVariants: Array<{ name: string; code: string; hreflang: string; href: string }>
8889
topics: Array<string>
8990
title: string
@@ -104,10 +105,12 @@ export type MainContextT = {
104105

105106
searchVersions: Record<string, string>
106107
nonEnterpriseDefaultVersion: string
108+
109+
status: number
107110
fullUrl: string
108111
}
109112

110-
export const getMainContextFromRequest = (req: any): MainContextT => {
113+
export const getMainContext = (req: any, res: any): MainContextT => {
111114
return {
112115
breadcrumbs: req.context.breadcrumbs || {},
113116
activeProducts: req.context.activeProducts,
@@ -134,6 +137,7 @@ export const getMainContextFromRequest = (req: any): MainContextT => {
134137
page: {
135138
languageVariants: req.context.page.languageVariants,
136139
documentType: req.context.page.documentType,
140+
type: req.context.page.type || null,
137141
title: req.context.page.title,
138142
fullTitle: req.context.page.fullTitle,
139143
topics: req.context.page.topics || [],
@@ -166,6 +170,7 @@ export const getMainContextFromRequest = (req: any): MainContextT => {
166170
featureFlags: {},
167171
searchVersions: req.context.searchVersions,
168172
nonEnterpriseDefaultVersion: req.context.nonEnterpriseDefaultVersion,
173+
status: res.statusCode,
169174
fullUrl: req.protocol + '://' + req.get('host') + req.originalUrl,
170175
}
171176
}

‎components/lib/events.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ type SendEventProps = {
6565
preference_value?: string
6666
}
6767

68+
function getMetaContent(name: string) {
69+
const metaTag = document.querySelector(`meta[name="${name}"]`) as HTMLMetaElement
70+
return metaTag?.content
71+
}
72+
6873
export function sendEvent({ type, version = '1.0.0', ...props }: SendEventProps) {
6974
let site_language = location.pathname.split('/')[1]
7075
if (location.pathname.startsWith('/playground')) {
@@ -91,6 +96,9 @@ export function sendEvent({ type, version = '1.0.0', ...props }: SendEventProps)
9196
search: location.search,
9297
href: location.href,
9398
site_language,
99+
page_document_type: getMetaContent('page-document-type'),
100+
page_type: getMetaContent('page-type'),
101+
status: Number(getMetaContent('status') || 0),
94102

95103
// Device information
96104
// os, os_version, browser, browser_version:

‎lib/schema-event.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ const context = {
6363
description: 'The language the user is viewing.',
6464
enum: Object.keys(languages),
6565
},
66+
page_document_type: {
67+
type: 'string',
68+
description: 'The generic page document type based on URL path.',
69+
enum: ['homepage', 'early-access', 'product', 'category', 'mapTopic', 'article'], // get-document-type.js
70+
},
71+
page_type: {
72+
type: 'string',
73+
description: 'Optional page type from the content frontmatter.',
74+
enum: ['overview', 'quick_start', 'tutorial', 'how_to', 'reference'], // frontmatter.js
75+
},
76+
status: {
77+
type: 'number',
78+
description: 'The HTTP response status code of the main page HTML.',
79+
minimum: 0,
80+
maximum: 999,
81+
},
6682

6783
// Device information
6884
os: {

‎middleware/events.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ router.post('/', async function postEvents(req, res, next) {
1616
const fields = omit(req.body, '_csrf')
1717

1818
if (!ajv.validate(schema, fields)) {
19-
if (isDev) console.log(ajv.errorsText())
20-
return res.status(400).json({})
19+
return res.status(400).json(isDev ? ajv.errorsText() : {})
2120
}
2221

2322
if (req.hydro.maySend()) {

‎pages/404.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Custom404 = () => {
99
<div className="min-h-screen d-flex flex-column">
1010
<Head>
1111
<title>404 - Page not found</title>
12+
<meta name="status" content="404" />
1213
</Head>
1314

1415
<SimpleHeader />

‎pages/[versionId]/[productId]/index.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import displayToolSpecificContent from 'components/lib/display-tool-specific-con
88
import localization from 'components/lib/localization'
99
import wrapCodeTerms from 'components/lib/wrap-code-terms'
1010

11-
import {
12-
MainContextT,
13-
MainContext,
14-
getMainContextFromRequest,
15-
} from 'components/context/MainContext'
11+
import { MainContextT, MainContext, getMainContext } from 'components/context/MainContext'
1612

1713
import {
1814
getProductLandingContextFromRequest,
@@ -100,10 +96,11 @@ export default GlobalPage
10096

10197
export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
10298
const req = context.req as any
99+
const res = context.res as any
103100

104101
return {
105102
props: {
106-
mainContext: getMainContextFromRequest(req),
103+
mainContext: getMainContext(req, res),
107104
productLandingContext: getProductLandingContextFromRequest(req),
108105
productSubLandingContext: getProductSubLandingContextFromRequest(req),
109106
tocLandingContext: getTocLandingContextFromRequest(req),

‎pages/[versionId]/actions/guides/building-and-testing-nodejs-or-python.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { GetServerSideProps } from 'next'
22
import { BeakerIcon, ZapIcon } from '@primer/octicons-react'
33

4-
import {
5-
MainContextT,
6-
MainContext,
7-
getMainContextFromRequest,
8-
} from 'components/context/MainContext'
4+
import { MainContextT, MainContext, getMainContext } from 'components/context/MainContext'
95

106
import {
117
PlaygroundContextProvider,
@@ -88,10 +84,11 @@ function PageInner() {
8884

8985
export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
9086
const req = context.req as any
87+
const res = context.res as any
9188

9289
return {
9390
props: {
94-
mainContext: getMainContextFromRequest(req),
91+
mainContext: getMainContext(req, res),
9592
},
9693
}
9794
}

‎pages/[versionId]/admin/release-notes.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { GetServerSideProps } from 'next'
22
import { Liquid } from 'liquidjs'
3-
import {
4-
MainContextT,
5-
MainContext,
6-
getMainContextFromRequest,
7-
} from 'components/context/MainContext'
3+
import { MainContextT, MainContext, getMainContext } from 'components/context/MainContext'
84
import { DefaultLayout } from 'components/DefaultLayout'
95
import { GHAEReleaseNotes } from 'components/release-notes/GHAEReleaseNotes'
106
import { GHESReleaseNotes } from 'components/release-notes/GHESReleaseNotes'
@@ -40,11 +36,12 @@ export default function ReleaseNotes({
4036

4137
export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
4238
const req = context.req as any
39+
const res = context.res as any
4340
const currentVersion = req.context.allVersions[req.context.currentVersion]
4441
const { latestPatch = '', latestRelease = '' } = req.context
4542
return {
4643
props: {
47-
mainContext: getMainContextFromRequest(req),
44+
mainContext: getMainContext(req, res),
4845
currentVersion,
4946
ghesContext: {
5047
currentVersion,

‎pages/[versionId]/graphql/overview/explorer.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { GetServerSideProps } from 'next'
22

3-
import {
4-
MainContextT,
5-
MainContext,
6-
getMainContextFromRequest,
7-
} from 'components/context/MainContext'
3+
import { MainContextT, MainContext, getMainContext } from 'components/context/MainContext'
84
import { Breadcrumbs } from 'components/Breadcrumbs'
95
import { DefaultLayout } from 'components/DefaultLayout'
106
import { useEffect, useRef } from 'react'
@@ -55,10 +51,11 @@ export default function GQLExplorer({ mainContext, graphqlExplorerUrl }: Props)
5551

5652
export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
5753
const req = context.req as any
54+
const res = context.res as any
5855

5956
return {
6057
props: {
61-
mainContext: getMainContextFromRequest(req),
58+
mainContext: getMainContext(req, res),
6259
graphqlExplorerUrl: req.context.graphql.explorerUrl,
6360
},
6461
}

‎pages/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { GetServerSideProps } from 'next'
33
import {
44
MainContextT,
55
MainContext,
6-
getMainContextFromRequest,
6+
getMainContext,
77
useMainContext,
88
} from 'components/context/MainContext'
99

@@ -134,10 +134,11 @@ function LandingPage(props: LandingPageProps) {
134134

135135
export const getServerSideProps: GetServerSideProps<Props> = async (context) => {
136136
const req = context.req as any
137+
const res = context.res as any
137138

138139
return {
139140
props: {
140-
mainContext: getMainContextFromRequest(req),
141+
mainContext: getMainContext(req, res),
141142
gettingStartedLinks: req.context.featuredLinks.gettingStarted.map(
142143
({ title, href, intro }: any) => ({ title, href, intro })
143144
),

0 commit comments

Comments
 (0)
Please sign in to comment.