Skip to content

Commit 7e4db83

Browse files
Sarah Edwardsheiskr
andauthored
Add event tracking for application selector (github#19533)
Co-authored-by: Kevin Heis <heiskr@users.noreply.github.com>
1 parent cb87184 commit 7e4db83

File tree

5 files changed

+98
-6
lines changed

5 files changed

+98
-6
lines changed

javascripts/display-tool-specific-content.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import Cookies from 'js-cookie'
2+
3+
import { sendEvent } from './events'
4+
15
const supportedTools = ['cli', 'desktop', 'webui']
26
const detectedTools = new Set()
37

@@ -18,6 +22,16 @@ export default function displayToolSpecificContent () {
1822
event.preventDefault()
1923
showContentForTool(event.target.dataset.tool)
2024
findToolSpecificContent(event.target.dataset.tool)
25+
26+
// Save this preference as a cookie.
27+
Cookies.set('toolPreferred', event.target.dataset.tool, { sameSite: 'strict', secure: true })
28+
29+
// Send event data
30+
sendEvent({
31+
type: 'preference',
32+
preference_name: 'application',
33+
preference_value: event.target.dataset.tool
34+
})
2135
})
2236
})
2337
}
@@ -73,6 +87,8 @@ function detectTools (el) {
7387
}
7488

7589
function getDefaultTool () {
90+
const cookieValue = Cookies.get('toolPreferred')
91+
if (cookieValue) return cookieValue
7692
const el = document.querySelector('[data-default-tool]')
7793
if (el) return el.dataset.defaultTool
7894
}

javascripts/events.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,14 @@ export function sendEvent ({
4646
experiment_name,
4747
experiment_variation,
4848
experiment_success,
49-
clipboard_operation
49+
clipboard_operation,
50+
preference_name,
51+
preference_value
5052
}) {
5153
const body = {
5254
_csrf: getCsrf(),
5355

54-
type, // One of page, exit, link, search, navigate, survey, experiment
56+
type, // One of page, exit, link, search, navigate, survey, experiment, preference
5557

5658
context: {
5759
// Primitives
@@ -77,7 +79,10 @@ export function sendEvent ({
7779

7880
// Location information
7981
timezone: new Date().getTimezoneOffset() / -60,
80-
user_language: navigator.language
82+
user_language: navigator.language,
83+
84+
// Preference information
85+
application_preference: Cookies.get('toolPreferred')
8186
},
8287

8388
// Page event
@@ -112,7 +117,11 @@ export function sendEvent ({
112117
experiment_success,
113118

114119
// Clipboard event
115-
clipboard_operation
120+
clipboard_operation,
121+
122+
// Preference event
123+
preference_name,
124+
preference_value
116125
}
117126
const blob = new Blob([JSON.stringify(body)], { type: 'application/json' })
118127
navigator.sendBeacon('/events', blob)
@@ -230,4 +239,5 @@ export default function initializeEvents () {
230239
// experiment event in ./experiment.js
231240
// search event in ./search.js
232241
// redirect event in middleware/record-redirect.js
242+
// preference event in ./display-tool-specific-content.js
233243
}

lib/hydro.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ const SCHEMAS = {
1313
experiment: 'docs.v0.ExperimentEvent',
1414
redirect: 'docs.v0.RedirectEvent',
1515
clipboard: 'docs.v0.ClipboardEvent',
16-
print: 'docs.v0.PrintEvent'
16+
print: 'docs.v0.PrintEvent',
17+
preference: 'docs.v0.PreferenceEvent'
1718
}
1819

1920
module.exports = class Hydro {

lib/schema-event.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,22 @@ const context = {
109109
user_language: {
110110
type: 'string',
111111
description: 'The browser value of `navigator.language`.'
112+
},
113+
114+
// Preference information
115+
/* os_preference: {
116+
type: 'string',
117+
description: 'The os for examples selected by the user.'
118+
}, */
119+
application_preference: {
120+
type: 'string',
121+
enum: ['webui', 'cli', 'desktop'],
122+
description: 'The application selected by the user.'
112123
}
124+
/* color_mode_preference: {
125+
type: 'string',
126+
description: 'The color mode selected by the user.'
127+
} */
113128
}
114129
}
115130

@@ -376,6 +391,34 @@ const printSchema = {
376391
}
377392
}
378393

394+
const preferenceSchema = {
395+
type: 'object',
396+
additionalProperties: false,
397+
required: [
398+
'type',
399+
'context',
400+
'preference_name',
401+
'preference_value'
402+
],
403+
properties: {
404+
context,
405+
type: {
406+
type: 'string',
407+
pattern: '^preference$'
408+
},
409+
preference_name: {
410+
type: 'string',
411+
enum: ['application'], // os, color_mode
412+
description: 'The preference name, such as os, application, or color_mode'
413+
},
414+
preference_value: {
415+
type: 'string',
416+
enum: ['webui', 'cli', 'desktop'],
417+
description: 'The application selected by the user.'
418+
}
419+
}
420+
}
421+
379422
module.exports = {
380423
oneOf: [
381424
pageSchema,
@@ -387,6 +430,7 @@ module.exports = {
387430
experimentSchema,
388431
redirectSchema,
389432
clipboardSchema,
390-
printSchema
433+
printSchema,
434+
preferenceSchema
391435
]
392436
}

tests/rendering/events.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,4 +474,25 @@ describe('POST /events', () => {
474474
checkEvent(printExample, 200)
475475
)
476476
})
477+
478+
describe('preference', () => {
479+
const preferenceExample = {
480+
...baseExample,
481+
type: 'preference',
482+
preference_name: 'application',
483+
preference_value: 'cli'
484+
}
485+
486+
it('should record an application event', () =>
487+
checkEvent(preferenceExample, 200)
488+
)
489+
490+
it('preference_name is string', () => {
491+
checkEvent({ ...preferenceExample, preference_name: null }, 400)
492+
})
493+
494+
it('preference_value is string', () => {
495+
checkEvent({ ...preferenceExample, preference_value: null }, 400)
496+
})
497+
})
477498
})

0 commit comments

Comments
 (0)