Skip to content

Commit 566d1a7

Browse files
authored
Add analyticsTags to Algolia search queries (#15719)
1 parent 956ed63 commit 566d1a7

File tree

5 files changed

+77
-104
lines changed

5 files changed

+77
-104
lines changed

javascripts/search.js

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const instantsearch = require('instantsearch.js').default
2-
const { searchBox, hits } = require('instantsearch.js/es/widgets')
2+
const { searchBox, hits, configure } = require('instantsearch.js/es/widgets')
33
const algoliasearch = require('algoliasearch')
44
const searchWithYourKeyboard = require('search-with-your-keyboard')
55
const querystring = require('querystring')
@@ -131,34 +131,39 @@ export default function () {
131131

132132
const search = instantsearch(opts)
133133

134-
search.addWidget(
135-
hits({
136-
container: '#search-results-container',
137-
templates: {
138-
empty: 'No results',
139-
item: resultTemplate
140-
},
141-
// useful for debugging template context, if needed
142-
transformItems: items => {
143-
// console.log(`transformItems`, items)
144-
return items
145-
}
146-
})
147-
)
148-
149134
// Find search placeholder text in a <meta> tag, falling back to a default
150135
const placeholderMeta = document.querySelector('meta[name="site.data.ui.search.placeholder"]')
151136
const placeholder = placeholderMeta ? placeholderMeta.content : 'Search topics, products...'
152137

153-
search.addWidget(
154-
searchBox({
155-
container: '#search-input-container',
156-
placeholder,
157-
// only autofocus on the homepage, and only if no #hash is present in the URL
158-
autofocus: (hasStandaloneSearch()) && !window.location.hash.length,
159-
showReset: false,
160-
showSubmit: false
161-
})
138+
search.addWidgets(
139+
[
140+
hits({
141+
container: '#search-results-container',
142+
templates: {
143+
empty: 'No results',
144+
item: resultTemplate
145+
},
146+
// useful for debugging template context, if needed
147+
transformItems: items => {
148+
// console.log(`transformItems`, items)
149+
return items
150+
}
151+
}),
152+
configure({
153+
analyticsTags: [
154+
'site:docs.github.com',
155+
`env:${process.env.NODE_ENV}`
156+
]
157+
}),
158+
searchBox({
159+
container: '#search-input-container',
160+
placeholder,
161+
// only autofocus on the homepage, and only if no #hash is present in the URL
162+
autofocus: (hasStandaloneSearch()) && !window.location.hash.length,
163+
showReset: false,
164+
showSubmit: false
165+
})
166+
]
162167
)
163168

164169
// enable for debugging

package-lock.json

Lines changed: 18 additions & 76 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"html-entities": "^1.2.1",
4141
"html-truncate": "^1.2.2",
4242
"imurmurhash": "^0.1.4",
43-
"instantsearch.js": "^3.6.0",
43+
"instantsearch.js": "^4.8.2",
4444
"is-url": "^1.2.4",
4545
"js-cookie": "^2.2.1",
4646
"js-yaml": "^3.14.0",

tests/browser/browser.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
/* global page */
1+
/* global page, browser */
22
const sleep = require('await-sleep')
3+
const querystring = require('querystring')
34

45
describe('homepage', () => {
56
test('should be titled "GitHub Documentation"', async () => {
@@ -36,6 +37,29 @@ describe('algolia browser search', () => {
3637
expect(hits.length).toBeGreaterThan(5)
3738
})
3839

40+
it('sends the correct data to algolia', async () => {
41+
const newPage = await browser.newPage()
42+
await newPage.goto('http://localhost:4001/ja/enterprise/2.22/admin/installation')
43+
44+
await newPage.setRequestInterception(true)
45+
newPage.on('request', interceptedRequest => {
46+
if (interceptedRequest.method() === 'POST' && /algolia/i.test(interceptedRequest.url())) {
47+
const data = JSON.parse(interceptedRequest.postData())
48+
const { indexName, params } = data.requests[0]
49+
const parsedParams = querystring.parse(params)
50+
const analyticsTags = JSON.parse(parsedParams.analyticsTags)
51+
expect(indexName).toBe('github-docs-2.22-ja')
52+
expect(analyticsTags).toHaveLength(2)
53+
// browser tests are run against production build, so we are expecting env:production
54+
expect(analyticsTags).toEqual(expect.arrayContaining(['site:docs.github.com', 'env:production']))
55+
}
56+
interceptedRequest.continue()
57+
})
58+
59+
await newPage.click('#search-input-container input[type="search"]')
60+
await newPage.type('#search-input-container input[type="search"]', 'test')
61+
})
62+
3963
it('removes `algolia-query` query param after page load', async () => {
4064
await page.goto('http://localhost:4001/en?algolia-query=helpme')
4165

webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path')
22
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
33
const CopyWebpackPlugin = require('copy-webpack-plugin')
4+
const { EnvironmentPlugin } = require('webpack')
45

56
module.exports = {
67
entry: './javascripts/index.js',
@@ -67,6 +68,7 @@ module.exports = {
6768
patterns: [
6869
{ from: 'node_modules/@primer/css/fonts', to: 'fonts' }
6970
]
70-
})
71+
}),
72+
new EnvironmentPlugin(['NODE_ENV'])
7173
]
7274
}

0 commit comments

Comments
 (0)