-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signals: add support for allow/disallow list, fix network signals bug (…
- Loading branch information
Showing
29 changed files
with
1,001 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@segment/analytics-signals': minor | ||
--- | ||
|
||
Update network signals to add support for allow/disallow |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...ignals/signals-integration-tests/src/tests/signals-vanilla/network-signals-filter.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { test, expect } from '@playwright/test' | ||
import { IndexPage } from './index-page' | ||
import type { SegmentEvent } from '@segment/analytics-next' | ||
|
||
const indexPage = new IndexPage() | ||
|
||
const basicEdgeFn = ` | ||
// this is a process signal function | ||
const processSignal = (signal) => { | ||
if (signal.type === 'interaction') { | ||
const eventName = signal.data.eventType + ' ' + '[' + signal.type + ']' | ||
analytics.track(eventName, signal.data) | ||
} | ||
}` | ||
|
||
test('network signals allow and disallow list', async ({ page }) => { | ||
await indexPage.loadAndWait(page, basicEdgeFn, { | ||
networkSignalsAllowList: ['allowed-api.com'], | ||
networkSignalsDisallowList: ['https://disallowed-api.com/api/foo'], | ||
}) | ||
|
||
// test that the allowed signals were emitted + sent | ||
const ALLOWED_URL = 'https://allowed-api.com/api/bar' | ||
const emittedNetworkSignalsAllowed = indexPage.waitForSignalsEmit( | ||
(el) => el.type === 'network' | ||
) | ||
await indexPage.mockTestRoute(ALLOWED_URL) | ||
await indexPage.makeFetchCall(ALLOWED_URL) | ||
await emittedNetworkSignalsAllowed | ||
|
||
await indexPage.waitForSignalsApiFlush() | ||
const batch = indexPage.lastSignalsApiReq.postDataJSON() | ||
.batch as SegmentEvent[] | ||
const networkEvents = batch.filter( | ||
(el: SegmentEvent) => el.properties!.type === 'network' | ||
) | ||
const allowedRequestsAndResponses = networkEvents.filter( | ||
(el) => el.properties!.data.url === ALLOWED_URL | ||
) | ||
expect(allowedRequestsAndResponses).toHaveLength(2) | ||
const [request, response] = allowedRequestsAndResponses | ||
expect(request.properties!.data.data).toEqual({ | ||
foo: 'bar', | ||
}) | ||
expect(response.properties!.data.data).toEqual({ | ||
someResponse: 'yep', | ||
}) | ||
|
||
// test the disallowed signals were not emitted (using the emitter to test this) | ||
const DISALLOWED_URL = 'https://disallowed-api.com/api/foo' | ||
const emittedNetworkSignalsDisallowed = indexPage.waitForSignalsEmit( | ||
(el) => el.type === 'network', | ||
{ | ||
failOnEmit: true, | ||
} | ||
) | ||
await indexPage.mockTestRoute(DISALLOWED_URL) | ||
await indexPage.makeFetchCall(DISALLOWED_URL) | ||
await emittedNetworkSignalsDisallowed | ||
}) |
24 changes: 12 additions & 12 deletions
24
packages/signals/signals-integration-tests/src/tests/signals-vanilla/signals-bundle.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
import { AnalyticsBrowser } from '@segment/analytics-next' | ||
import { SignalsPlugin } from '@segment/analytics-signals' | ||
|
||
const analytics = new AnalyticsBrowser() | ||
;(window as any).analytics = analytics | ||
declare global { | ||
interface Window { | ||
analytics: AnalyticsBrowser | ||
SignalsPlugin: typeof SignalsPlugin | ||
signalsPlugin: SignalsPlugin | ||
} | ||
} | ||
|
||
const signalsPlugin = new SignalsPlugin({ | ||
disableSignalsRedaction: true, | ||
}) | ||
|
||
;(window as any).signalsPlugin = signalsPlugin | ||
|
||
analytics.load({ | ||
writeKey: '<SOME_WRITE_KEY>', | ||
plugins: [signalsPlugin], | ||
}) | ||
/** | ||
* Not instantiating the analytics object here, as it will be instantiated in the test | ||
*/ | ||
;(window as any).SignalsPlugin = SignalsPlugin | ||
;(window as any).analytics = new AnalyticsBrowser() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/signals/signals/src/core/client/__tests__/client.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.