Skip to content

Commit 4bb3e81

Browse files
feat: Add API: setApplicationVersion (#639)
Co-authored-by: Patrick Housley <[email protected]>
1 parent 0865bf1 commit 4bb3e81

File tree

6 files changed

+86
-15
lines changed

6 files changed

+86
-15
lines changed

package-lock.json

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/loaders/agent-base.js

+12
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ export class AgentBase {
5151
warn('Call to agent api setUserId failed. The js errors feature is not currently initialized.')
5252
}
5353

54+
/**
55+
* Adds a user-defined application version string to subsequent events on the page.
56+
* This decorates all payloads with an attribute of `application.version` which is queryable in NR1.
57+
* {@link https://docs.newrelic.com/docs/browser/new-relic-browser/browser-apis/setapplicationversion/}
58+
* @param {string|null} value A string identifier for the application version, useful for
59+
* tying all browser events to a specific release tag. The value parameter does not
60+
* have to be unique. Passing a null value unsets any existing value.
61+
*/
62+
setApplicationVersion (value) {
63+
warn('Call to agent api setApplicationVersion failed. The agent is not currently initialized.')
64+
}
65+
5466
/**
5567
* Allows selective ignoring and grouping of known errors that the browser agent captures.
5668
* {@link https://docs.newrelic.com/docs/browser/new-relic-browser/browser-apis/seterrorhandler/}

src/loaders/api/api.component-test.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { setAPI } from './api'
2+
import { setInfo, getInfo, setConfiguration } from '../../common/config/config'
3+
4+
setInfo('abcd', { licenseKey: '1234', applicationID: '1234' })
5+
setConfiguration('abcd', {})
6+
let apiInterface
7+
beforeEach(() => {
8+
console.warn = jest.fn()
9+
apiInterface = setAPI('abcd', true)
10+
})
11+
12+
afterEach(() => {
13+
jest.restoreAllMocks()
14+
})
15+
16+
describe('api', () => {
17+
describe('setApplicationVersion', () => {
18+
test('setApplicationVersion sets and unsets ja with valid values', () => {
19+
apiInterface.setApplicationVersion('1.2.3')
20+
expect(getInfo('abcd').jsAttributes).toMatchObject({ 'application.version': '1.2.3' })
21+
22+
apiInterface.setApplicationVersion(null)
23+
expect(getInfo('abcd').jsAttributes).toMatchObject({ })
24+
})
25+
26+
test('setApplicationVersion warns if invalid data is supplied', () => {
27+
apiInterface.setApplicationVersion(1)
28+
expect(console.warn).toHaveBeenCalledWith('New Relic: Failed to execute setApplicationVersion. Expected <String | null>, but got <number>.')
29+
30+
apiInterface.setApplicationVersion(false)
31+
expect(console.warn).toHaveBeenCalledWith('New Relic: Failed to execute setApplicationVersion. Expected <String | null>, but got <boolean>.')
32+
33+
apiInterface.setApplicationVersion({ version: '1.2.3' })
34+
expect(console.warn).toHaveBeenCalledWith('New Relic: Failed to execute setApplicationVersion. Expected <String | null>, but got <object>.')
35+
})
36+
37+
test('setApplicationVersion replaces existing data if called twice', () => {
38+
apiInterface.setApplicationVersion('1.2.3')
39+
expect(getInfo('abcd').jsAttributes).toMatchObject({ 'application.version': '1.2.3' })
40+
41+
apiInterface.setApplicationVersion('4.5.6')
42+
expect(getInfo('abcd').jsAttributes).toMatchObject({ 'application.version': '4.5.6' })
43+
})
44+
})
45+
})

src/loaders/api/api.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function setTopLevelCallers () {
2121
const funcs = [
2222
'setErrorHandler', 'finished', 'addToTrace', 'inlineHit', 'addRelease',
2323
'addPageAction', 'setCurrentRouteName', 'setPageViewName', 'setCustomAttribute',
24-
'interaction', 'noticeError', 'setUserId'
24+
'interaction', 'noticeError', 'setUserId', 'setApplicationVersion'
2525
]
2626
funcs.forEach(f => {
2727
nr[f] = (...args) => caller(f, ...args)
@@ -109,6 +109,19 @@ export function setAPI (agentIdentifier, forceDrain) {
109109
return appendJsAttribute('enduser.id', value, 'setUserId', true)
110110
}
111111

112+
/**
113+
* Attach the 'applcation.version' attribute onto agent payloads. This may be used in NR queries to group all browser events by a specific customer-defined release.
114+
* @param {string|null} value - Application version -- if null, will "unset" the value
115+
* @returns @see apiCall
116+
*/
117+
apiInterface.setApplicationVersion = function (value) {
118+
if (!(typeof value === 'string' || value === null)) {
119+
warn(`Failed to execute setApplicationVersion. Expected <String | null>, but got <${typeof value}>.`)
120+
return
121+
}
122+
return appendJsAttribute('application.version', value, 'setApplicationVersion', false)
123+
}
124+
112125
apiInterface.interaction = function () {
113126
return new InteractionHandle().get()
114127
}

tests/functional/uncat-internal-help.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const asyncApiFns = [
3232
'setPageViewName',
3333
'setCustomAttribute',
3434
'setUserId',
35+
'setApplicationVersion',
3536
'setErrorHandler',
3637
'finished',
3738
'addToTrace',

tools/browsers-lists/browsers-supported.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
"browserName": "MicrosoftEdge",
3535
"platformName": "Windows 11",
3636
"platform": "Windows 11",
37-
"version": "105",
38-
"browserVersion": "105"
37+
"version": "106",
38+
"browserVersion": "106"
3939
},
4040
{
4141
"browserName": "MicrosoftEdge",
@@ -48,8 +48,8 @@
4848
"browserName": "MicrosoftEdge",
4949
"platformName": "Windows 11",
5050
"platform": "Windows 11",
51-
"version": "110",
52-
"browserVersion": "110"
51+
"version": "111",
52+
"browserVersion": "111"
5353
},
5454
{
5555
"browserName": "MicrosoftEdge",
@@ -64,15 +64,15 @@
6464
"browserName": "firefox",
6565
"platformName": "Windows 10",
6666
"platform": "Windows 10",
67-
"version": "106",
68-
"browserVersion": "106"
67+
"version": "107",
68+
"browserVersion": "107"
6969
},
7070
{
7171
"browserName": "firefox",
7272
"platformName": "Windows 10",
7373
"platform": "Windows 10",
74-
"version": "108",
75-
"browserVersion": "108"
74+
"version": "109",
75+
"browserVersion": "109"
7676
},
7777
{
7878
"browserName": "firefox",

0 commit comments

Comments
 (0)