Skip to content

Commit e3ca824

Browse files
fix: report empty previousUrl as undefined (#1526)
1 parent 697cf6f commit e3ca824

File tree

8 files changed

+20
-33
lines changed

8 files changed

+20
-33
lines changed

src/common/url/clean-url.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ var patternWithoutHash = /([^?#]*)().*/
88

99
/**
1010
* Cleans a URL by removing the query string and fragment (hash portion).
11-
* @param {string} url - The original URL to be cleaned.
11+
* @param {string} [url] - The original URL to be cleaned.
1212
* @param {boolean} [keepHash=false] - Whether to preserve the hash portion of the URL.
1313
* @returns {string} The cleaned URL.
1414
*/
1515
export function cleanURL (url, keepHash) {
16+
if (!url) return url
1617
return url.replace(keepHash ? patternWithHash : patternWithoutHash, '$1$2')
1718
}

src/features/soft_navigations/aggregate/initial-page-load-interaction.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export class InitialPageLoadInteraction extends Interaction {
1414
super(agentRef, IPL_TRIGGER_NAME, 0, null)
1515
this.queueTime = agentRef.info.queueTime
1616
this.appTime = agentRef.info.applicationTime
17-
this.oldURL = document.referrer
17+
/** @type {string|undefined} we assign as undefined if no referrer value is available so that URL grouping is not applied to an empty string at ingest */
18+
this.oldURL = document.referrer || undefined
1819
}
1920

2021
get firstPaint () { return firstPaint.current.value }

src/features/spa/aggregate/interaction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function Interaction (eventName, timestamp, url, routeName, onFinished, a
3333
attrs.initialPageURL = initialLocation
3434
attrs.oldRoute = routeName
3535
attrs.newURL = url
36-
attrs.oldURL = eventName === 'initialPageLoad' ? document.referrer : url
36+
attrs.oldURL = eventName === 'initialPageLoad' ? document.referrer || undefined : url // document referrer can return '' and flipper url grouping gets weird with empty strings. Pass undefined to skip flipper.
3737
attrs.custom = {}
3838
attrs.store = {}
3939
}

tests/specs/soft_navigations/payloads.e2e.js

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ describe('attribution tests', () => {
2727
await browser.testHandle.assetURL('soft_navigations/action-text.html', configWithDenyList)
2828
).then(() => browser.waitForAgentLoad())
2929
])
30-
const documentReferrer = await browser.execute(() => document.referrer)
3130
const ipl = interactionHarvests[0].request.body[0]
3231

3332
expect(ipl.trigger).toEqual('initialPageLoad')
3433
expect(ipl.isRouteChange).not.toBeTruthy()
35-
if (browserMatch(notIOS)) expect(ipl.oldURL).toEqual(documentReferrer) // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
34+
if (browserMatch(notIOS)) expect(ipl.oldURL).toEqual('') // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
3635

3736
;[interactionHarvests] = await Promise.all([
3837
interactionsCapture.waitForResult({ totalCount: 2 }),
@@ -55,13 +54,12 @@ describe('attribution tests', () => {
5554
await browser.testHandle.assetURL('soft_navigations/action-text.html', configWithDenyList)
5655
).then(() => browser.waitForAgentLoad())
5756
])
58-
const documentReferrer = await browser.execute(() => document.referrer)
5957
const ipl = interactionHarvests[0].request.body[0]
6058

6159
expect(ipl.trigger).toEqual('initialPageLoad')
6260
expect(ipl.children.length).toEqual(0)
6361
expect(ipl.isRouteChange).not.toBeTruthy()
64-
if (browserMatch(notIOS)) expect(ipl.oldURL).toEqual(documentReferrer) // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
62+
if (browserMatch(notIOS)) expect(ipl.oldURL).toEqual('') // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
6563

6664
;[interactionHarvests] = await Promise.all([
6765
interactionsCapture.waitForResult({ totalCount: 2 }),
@@ -84,13 +82,12 @@ describe('attribution tests', () => {
8482
await browser.testHandle.assetURL('soft_navigations/action-text.html', configWithDenyList)
8583
).then(() => browser.waitForAgentLoad())
8684
])
87-
const documentReferrer = await browser.execute(() => document.referrer)
8885
const ipl = interactionHarvests[0].request.body[0]
8986

9087
expect(ipl.trigger).toEqual('initialPageLoad')
9188
expect(ipl.children.length).toEqual(0)
9289
expect(ipl.isRouteChange).not.toBeTruthy()
93-
if (browserMatch(notIOS)) expect(ipl.oldURL).toEqual(documentReferrer) // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
90+
if (browserMatch(notIOS)) expect(ipl.oldURL).toEqual('') // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
9491

9592
;[interactionHarvests] = await Promise.all([
9693
interactionsCapture.waitForResult({ totalCount: 2 }),
@@ -113,13 +110,12 @@ describe('attribution tests', () => {
113110
await browser.testHandle.assetURL('soft_navigations/action-text.html', configWithDenyList)
114111
).then(() => browser.waitForAgentLoad())
115112
])
116-
const documentReferrer = await browser.execute(() => document.referrer)
117113
const ipl = interactionHarvests[0].request.body[0]
118114

119115
expect(ipl.trigger).toEqual('initialPageLoad')
120116
expect(ipl.children.length).toEqual(0)
121117
expect(ipl.isRouteChange).not.toBeTruthy()
122-
if (browserMatch(notIOS)) expect(ipl.oldURL).toEqual(documentReferrer) // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
118+
if (browserMatch(notIOS)) expect(ipl.oldURL).toEqual('') // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
123119

124120
;[interactionHarvests] = await Promise.all([
125121
interactionsCapture.waitForResult({ totalCount: 2 }),
@@ -143,13 +139,11 @@ describe('attribution tests', () => {
143139
.then(() => $('body').click())
144140
])
145141

146-
const documentReferrer = await browser.execute(() => document.referrer)
147-
148142
const [{ request: { body: [ipl] } }, { request: { body: [rc] } }] = interactionHarvests
149143

150144
expect(ipl.trigger).toEqual('initialPageLoad')
151145
expect(ipl.navTiming).toEqual(expect.any(Object))
152-
if (browserMatch(notIOS)) expect(ipl.oldURL).toEqual(documentReferrer) // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
146+
if (browserMatch(notIOS)) expect(ipl.oldURL).toEqual('') // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
153147
if (browserMatch(supportsFirstPaint)) expect(ipl.firstPaint).toBeGreaterThan(0)
154148
else expect(ipl.firstPaint).toBeNull()
155149
expect(ipl.firstContentfulPaint).toBeGreaterThan(0)

tests/specs/spa/payloads.e2e.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@ describe('attribution tests', () => {
2727
.then(() => $('#one').click())
2828
])
2929

30-
const documentReferrer = await browser.execute(() => document.referrer)
3130
const interactionEvents = JSONPath({ path: '$.[*].request.body.[?(!!@ && @.type===\'interaction\')]', json: interactionHarvests })
3231
expect(interactionEvents.length).toEqual(2)
3332

3433
expect(interactionEvents[0].trigger).toEqual('initialPageLoad')
3534
expect(interactionEvents[0].isRouteChange).not.toBeTruthy()
36-
if (browserMatch(notIOS)) expect(interactionEvents[0].oldURL).toEqual(documentReferrer) // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
35+
if (browserMatch(notIOS)) expect(interactionEvents[0].oldURL).toEqual('') // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
3736

3837
expect(interactionEvents[1].children.length).toEqual(1)
3938
expect(interactionEvents[1].children[0]).toMatchObject({
@@ -54,14 +53,13 @@ describe('attribution tests', () => {
5453
.then(() => $('#two').click())
5554
])
5655

57-
const documentReferrer = await browser.execute(() => document.referrer)
5856
const interactionEvents = JSONPath({ path: '$.[*].request.body.[?(!!@ && @.type===\'interaction\')]', json: interactionHarvests })
5957
expect(interactionEvents.length).toEqual(2)
6058

6159
expect(interactionEvents[0].trigger).toEqual('initialPageLoad')
6260
expect(interactionEvents[0].children.length).toEqual(0)
6361
expect(interactionEvents[0].isRouteChange).not.toBeTruthy()
64-
if (browserMatch(notIOS)) expect(interactionEvents[0].oldURL).toEqual(documentReferrer) // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
62+
if (browserMatch(notIOS)) expect(interactionEvents[0].oldURL).toEqual('') // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
6563

6664
expect(interactionEvents[1].children.length).toEqual(1)
6765
expect(interactionEvents[1].children[0]).toMatchObject({
@@ -82,14 +80,13 @@ describe('attribution tests', () => {
8280
.then(() => $('#three').click())
8381
])
8482

85-
const documentReferrer = await browser.execute(() => document.referrer)
8683
const interactionEvents = JSONPath({ path: '$.[*].request.body.[?(!!@ && @.type===\'interaction\')]', json: interactionHarvests })
8784
expect(interactionEvents.length).toEqual(2)
8885

8986
expect(interactionEvents[0].trigger).toEqual('initialPageLoad')
9087
expect(interactionEvents[0].children.length).toEqual(0)
9188
expect(interactionEvents[0].isRouteChange).not.toBeTruthy()
92-
if (browserMatch(notIOS)) expect(interactionEvents[0].oldURL).toEqual(documentReferrer) // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
89+
if (browserMatch(notIOS)) expect(interactionEvents[0].oldURL).toEqual('') // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
9390

9491
expect(interactionEvents[1].children.length).toEqual(1)
9592
expect(interactionEvents[1].children[0]).toMatchObject({
@@ -110,14 +107,13 @@ describe('attribution tests', () => {
110107
.then(() => $('body').click())
111108
])
112109

113-
const documentReferrer = await browser.execute(() => document.referrer)
114110
const interactionEvents = JSONPath({ path: '$.[*].request.body.[?(!!@ && @.type===\'interaction\')]', json: interactionHarvests })
115111
expect(interactionEvents.length).toEqual(2)
116112

117113
expect(interactionEvents[0].trigger).toEqual('initialPageLoad')
118114
expect(interactionEvents[0].children.length).toEqual(0)
119115
expect(interactionEvents[0].isRouteChange).not.toBeTruthy()
120-
if (browserMatch(notIOS)) expect(interactionEvents[0].oldURL).toEqual(documentReferrer) // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
116+
if (browserMatch(notIOS)) expect(interactionEvents[0].oldURL).toEqual('') // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
121117

122118
expect(interactionEvents[1].children.length).toEqual(0)
123119
})
@@ -137,14 +133,13 @@ describe('attribution tests', () => {
137133
])
138134
const receiptTime = Date.now()
139135

140-
const documentReferrer = await browser.execute(() => document.referrer)
141136
const interactionEvents = JSONPath({ path: '$.[*].request.body.[?(!!@ && @.type===\'interaction\')]', json: interactionHarvests })
142137
expect(interactionEvents.length).toEqual(2)
143138

144139
expect(interactionEvents[0].trigger).toEqual('initialPageLoad')
145140
expect(interactionEvents[0].children[0].path.startsWith('/1/')).toEqual(true)
146141
expect(interactionEvents[0].isRouteChange).not.toBeTruthy()
147-
if (browserMatch(notIOS)) expect(interactionEvents[0].oldURL).toEqual(documentReferrer) // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
142+
if (browserMatch(notIOS)) expect(interactionEvents[0].oldURL).toEqual('') // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
148143

149144
expect(interactionEvents[1].id).toBeTruthy()
150145
expect(interactionEvents[1].id.match(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/)).toBeTruthy()
@@ -187,14 +182,13 @@ describe('attribution tests', () => {
187182
])
188183
const receiptTime = Date.now()
189184

190-
const documentReferrer = await browser.execute(() => document.referrer)
191185
const interactionEvents = JSONPath({ path: '$.[*].request.body.[?(!!@ && @.type===\'interaction\')]', json: interactionHarvests })
192186
expect(interactionEvents.length).toEqual(2)
193187

194188
expect(interactionEvents[0].trigger).toEqual('initialPageLoad')
195189
expect(interactionEvents[0].children[0].path.startsWith('/1/')).toEqual(true)
196190
expect(interactionEvents[0].isRouteChange).not.toBeTruthy()
197-
if (browserMatch(notIOS)) expect(interactionEvents[0].oldURL).toEqual(documentReferrer) // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
191+
if (browserMatch(notIOS)) expect(interactionEvents[0].oldURL).toEqual('') // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
198192

199193
expect(interactionEvents[1].id).toBeTruthy()
200194
expect(interactionEvents[1].id.match(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/)).toBeTruthy()
@@ -235,15 +229,14 @@ describe('attribution tests', () => {
235229
.then(() => $('body').click())
236230
])
237231

238-
const documentReferrer = await browser.execute(() => document.referrer)
239232
const [{ request: { body: [ipl] } }, { request: { body: [rc] } }] = interactionHarvests
240233

241234
expect(ipl.trigger).toEqual('initialPageLoad')
242235
expect(ipl.navTiming).toEqual(expect.any(Object))
243236
if (browserMatch(supportsFirstPaint)) expect(ipl.firstPaint).toBeGreaterThan(0)
244237
else expect(ipl.firstPaint).toBeNull()
245238
expect(ipl.firstContentfulPaint).toBeGreaterThan(0)
246-
if (browserMatch(notIOS)) expect(ipl.oldURL).toEqual(documentReferrer) // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
239+
if (browserMatch(notIOS)) expect(ipl.oldURL).toEqual('') // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
247240

248241
expect(rc.category).toEqual('Route change')
249242
expect(rc.navTiming).toBeNull()

tests/specs/spa/zonejs.e2e.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ describe('spa interactions with zonejs', () => {
5050
await browser.url(url).then(() => browser.waitForAgentLoad())
5151
])
5252

53-
const referrer = await browser.execute(() => document.referrer)
54-
5553
const event = interactionHarvests[0].request.body
5654
.find(ev => ev.type === 'interaction' && ev.trigger === 'initialPageLoad')
5755
expect(event).toEqual(expect.objectContaining({
@@ -60,7 +58,7 @@ describe('spa interactions with zonejs', () => {
6058
trigger: 'initialPageLoad',
6159
initialPageURL: url.slice(0, url.indexOf('?')),
6260
newURL: url.slice(0, url.indexOf('?')),
63-
...(browserMatch(notIOS) ? { oldURL: referrer } : {}), // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
61+
...(browserMatch(notIOS) ? { oldURL: '' } : {}), // ios on lambdatest appears to return the wrong value for referrer when using browser.execute, which breaks this test condition. Confirmed referrer behavior works in real env
6462
children: expect.arrayContaining([
6563
expect.objectContaining({
6664
domain: expect.stringContaining('bam-test-1.nr-local.net'),

tests/unit/features/soft_navigations/aggregate/initial-page-load-interaction.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ test('InitialPageLoad has correct oldURL', () => {
3838
runtime: { obfuscator: new Obfuscator({ init: { obfuscate: [] } }) }
3939
})
4040

41-
expect(ipl.oldURL).toBe(document.referrer || '')
41+
expect(ipl.oldURL).toBe(document.referrer || undefined)
4242
expect(ipl.oldURL).not.toEqual(ipl.newURL)
4343
})

tools/wdio/args.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const args = yargs(hideBin(process.argv))
6868
.describe('l', 'default loader to inject into test pages')
6969

7070
.boolean('T')
71-
.default('T', false)
71+
.default('T', true)
7272
.alias('T', 'tunnel')
7373
.describe('T', 'Launch LambdaTest tunnel for this test run using process.env credentials')
7474

0 commit comments

Comments
 (0)