Skip to content

Commit

Permalink
Fix sample rate implementation (#1188)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljackins authored Nov 19, 2024
1 parent 46e8819 commit de6f86d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-buckets-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-signals': patch
---

Fix sampleRate check
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ export class BasePage {
page: Page,
edgeFn: string,
signalSettings: Partial<SignalsPluginSettingsConfig> = {},
options: { updateURL?: (url: string) => string } = {}
options: { updateURL?: (url: string) => string; sampleRate?: number } = {}
) {
logConsole(page)
this.page = page
this.network = new PageNetworkUtils(page)
this.edgeFn = edgeFn
await this.setupMockedRoutes()
await this.setupMockedRoutes(options.sampleRate)
const url = options.updateURL ? options.updateURL(this.url) : this.url
await this.page.goto(url, { waitUntil: 'domcontentloaded' })
void this.invokeAnalyticsLoad({
Expand Down Expand Up @@ -92,15 +92,15 @@ export class BasePage {
return this
}

private async setupMockedRoutes() {
private async setupMockedRoutes(sampleRate?: number) {
// clear any existing saved requests
this.trackingAPI.clear()
this.signalsAPI.clear()

await Promise.all([
this.mockSignalsApi(),
this.mockTrackingApi(),
this.mockCDNSettings(),
this.mockCDNSettings(sampleRate),
])
}

Expand Down Expand Up @@ -203,7 +203,7 @@ export class BasePage {
})
}

async mockCDNSettings() {
async mockCDNSettings(sampleRate?: number) {
await this.page.route(
'https://cdn.segment.com/v1/projects/*/settings',
(route, request) => {
Expand All @@ -218,6 +218,9 @@ export class BasePage {
downloadURL: this.edgeFnDownloadURL,
version: 1,
},
autoInstrumentationSettings: {
sampleRate: sampleRate ?? 1,
},
},
}).build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,34 @@ const indexPage = new IndexPage()

const basicEdgeFn = `const processSignal = (signal) => {}`

test('ingestion not enabled -> will not send the signal', async ({ page }) => {
await indexPage.loadAndWait(page, basicEdgeFn, {
enableSignalsIngestion: false,
flushAt: 1,
})
test('debug ingestion disabled and sample rate 0 -> will not send the signal', async ({
page,
}) => {
await indexPage.loadAndWait(
page,
basicEdgeFn,
{
enableSignalsIngestion: false,
flushAt: 1,
},
{ sampleRate: 0 }
)
await indexPage.fillNameInput('John Doe')
await page.waitForTimeout(100)
expect(indexPage.signalsAPI.getEvents('interaction')).toHaveLength(0)
})

test('ingestion enabled -> will send the signal', async ({ page }) => {
await indexPage.loadAndWait(page, basicEdgeFn, {
enableSignalsIngestion: true,
})
test('debug ingestion enabled and sample rate 0 -> will send the signal', async ({
page,
}) => {
await indexPage.loadAndWait(
page,
basicEdgeFn,
{
enableSignalsIngestion: true,
},
{ sampleRate: 0 }
)

await Promise.all([
indexPage.fillNameInput('John Doe'),
Expand All @@ -27,3 +41,21 @@ test('ingestion enabled -> will send the signal', async ({ page }) => {

expect(true).toBe(true)
})

test('debug ingestion disabled and sample rate 1 -> will send the signal', async ({
page,
}) => {
await indexPage.loadAndWait(
page,
basicEdgeFn,
{
flushAt: 1,
enableSignalsIngestion: false,
},
{
sampleRate: 1,
}
)
await indexPage.fillNameInput('John Doe')
expect(indexPage.signalsAPI.getEvents('interaction')).toHaveLength(1)
})
19 changes: 16 additions & 3 deletions packages/signals/signals/src/core/signals/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ export class SignalGlobalSettings {
if (this.signalsDebug.getEnableSignalsIngestion()) {
return true
}
if (!this.sampleSuccess) {
return false
if (this.sampleSuccess) {
return true
}
return false
},
Expand Down Expand Up @@ -107,9 +107,22 @@ export class SignalGlobalSettings {
Boolean(val)
)
)
this.sampleSuccess = this.checkSampleRate(sampleRate ?? 0)
}
private checkSampleRate(sampleRate: number): boolean {
const storage = new WebStorage(window.sessionStorage)
const previousSample = storage.getItem<boolean>('segment_sample_success')
if (previousSample !== undefined) {
return previousSample
}

if (sampleRate && Math.random() <= sampleRate) {
this.sampleSuccess = true
storage.setItem('segment_sample_success', true)
return true
}

storage.setItem('segment_sample_success', false)
return false
}
}

Expand Down

0 comments on commit de6f86d

Please sign in to comment.