Skip to content

Commit d1afb09

Browse files
committed
Fix sample rate check, add integration tests
1 parent 0a30f38 commit d1afb09

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

packages/signals/signals-integration-tests/src/helpers/base-page-object.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
SignalAPIRequestBuffer,
88
TrackingAPIRequestBuffer,
99
} from './network-utils'
10+
import { CDNSettings } from '@segment/analytics-next'
1011

1112
export class BasePage {
1213
protected page!: Page
@@ -41,13 +42,14 @@ export class BasePage {
4142
page: Page,
4243
edgeFn: string,
4344
signalSettings: Partial<SignalsPluginSettingsConfig> = {},
44-
options: { updateURL?: (url: string) => string } = {}
45+
options: { updateURL?: (url: string) => string } = {},
46+
cdnSettings?: Partial<CDNSettings>
4547
) {
4648
logConsole(page)
4749
this.page = page
4850
this.network = new PageNetworkUtils(page)
4951
this.edgeFn = edgeFn
50-
await this.setupMockedRoutes()
52+
await this.setupMockedRoutes(cdnSettings)
5153
const url = options.updateURL ? options.updateURL(this.url) : this.url
5254
await this.page.goto(url, { waitUntil: 'domcontentloaded' })
5355
void this.invokeAnalyticsLoad({
@@ -92,15 +94,15 @@ export class BasePage {
9294
return this
9395
}
9496

95-
private async setupMockedRoutes() {
97+
private async setupMockedRoutes(settings?: Partial<CDNSettings>) {
9698
// clear any existing saved requests
9799
this.trackingAPI.clear()
98100
this.signalsAPI.clear()
99101

100102
await Promise.all([
101103
this.mockSignalsApi(),
102104
this.mockTrackingApi(),
103-
this.mockCDNSettings(),
105+
this.mockCDNSettings(settings),
104106
])
105107
}
106108

@@ -203,7 +205,7 @@ export class BasePage {
203205
})
204206
}
205207

206-
async mockCDNSettings() {
208+
async mockCDNSettings(settings?: Partial<CDNSettings>) {
207209
await this.page.route(
208210
'https://cdn.segment.com/v1/projects/*/settings',
209211
(route, request) => {
@@ -213,7 +215,7 @@ export class BasePage {
213215

214216
const cdnSettings = new CDNSettingsBuilder({
215217
writeKey: '<SOME_WRITE_KEY>',
216-
baseCDNSettings: {
218+
baseCDNSettings: settings ?? {
217219
edgeFunction: {
218220
downloadURL: this.edgeFnDownloadURL,
219221
version: 1,

packages/signals/signals-integration-tests/src/tests/signals-vanilla/signals-ingestion.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,50 @@ test('ingestion enabled -> will send the signal', async ({ page }) => {
2727

2828
expect(true).toBe(true)
2929
})
30+
31+
test('never sends signal when sample rate is 0', async ({ page }) => {
32+
await indexPage.loadAndWait(
33+
page,
34+
basicEdgeFn,
35+
{
36+
flushAt: 1,
37+
enableSignalsIngestion: false,
38+
},
39+
undefined,
40+
{
41+
edgeFunction: {
42+
downloadURL: 'https://cdn.edgefn.segment.com/MY-WRITEKEY/foo.js',
43+
version: 1,
44+
},
45+
autoInstrumentationSettings: {
46+
sampleRate: 0,
47+
},
48+
}
49+
)
50+
await indexPage.fillNameInput('John Doe')
51+
await page.waitForTimeout(100)
52+
expect(indexPage.signalsAPI.getEvents('interaction')).toHaveLength(0)
53+
})
54+
55+
test('always sends signal when sample rate is 1', async ({ page }) => {
56+
await indexPage.loadAndWait(
57+
page,
58+
basicEdgeFn,
59+
{
60+
flushAt: 1,
61+
enableSignalsIngestion: false,
62+
},
63+
undefined,
64+
{
65+
edgeFunction: {
66+
downloadURL: 'https://cdn.edgefn.segment.com/MY-WRITEKEY/foo.js',
67+
version: 1,
68+
},
69+
autoInstrumentationSettings: {
70+
sampleRate: 1,
71+
},
72+
}
73+
)
74+
await indexPage.fillNameInput('John Doe')
75+
expect(indexPage.signalsAPI.getEvents('interaction')).toHaveLength(1)
76+
})

packages/signals/signals/src/core/signals/settings.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ export class SignalGlobalSettings {
6464
if (this.signalsDebug.getEnableSignalsIngestion()) {
6565
return true
6666
}
67-
if (!this.sampleSuccess) {
68-
return false
67+
if (this.sampleSuccess) {
68+
return true
6969
}
7070
return false
7171
},
@@ -105,9 +105,22 @@ export class SignalGlobalSettings {
105105
Boolean(val)
106106
)
107107
)
108+
this.sampleSuccess = this.checkSampleRate(sampleRate ?? 0)
109+
}
110+
private checkSampleRate(sampleRate: number): boolean {
111+
const storage = new WebStorage(window.sessionStorage)
112+
const previousSample = storage.getItem<boolean>('segment_sample_success')
113+
if (previousSample !== undefined) {
114+
return previousSample
115+
}
116+
108117
if (sampleRate && Math.random() <= sampleRate) {
109-
this.sampleSuccess = true
118+
storage.setItem('segment_sample_success', true)
119+
return true
110120
}
121+
122+
storage.setItem('segment_sample_success', false)
123+
return false
111124
}
112125
}
113126

0 commit comments

Comments
 (0)