-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for segment inspector (#413)
This patch adds event reporting to Segment Inspector. The reporting happens through the interface injected by the extension itself.
- Loading branch information
Showing
7 changed files
with
121 additions
and
1 deletion.
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-next': minor | ||
--- | ||
|
||
Add support for Segment Inspector Chrome extension |
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
59 changes: 59 additions & 0 deletions
59
packages/browser/src/core/inspector/__tests__/index.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,59 @@ | ||
import { Analytics } from '../../analytics' | ||
|
||
let analytics: Analytics | ||
|
||
describe('Inspector interface', () => { | ||
beforeEach(() => { | ||
window.__SEGMENT_INSPECTOR__ = { | ||
start: jest.fn(), | ||
trace: jest.fn(), | ||
} | ||
|
||
analytics = new Analytics({ | ||
writeKey: 'abc', | ||
}) | ||
}) | ||
|
||
it('accepts and starts up an inspector client trying to connect', () => { | ||
expect(window.__SEGMENT_INSPECTOR__?.start).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('notifies the connected inspector client about each event API call and delivery', async () => { | ||
expect(window.__SEGMENT_INSPECTOR__?.trace).not.toHaveBeenCalled() | ||
|
||
const timestampMatcher = expect.stringMatching( | ||
/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/ | ||
) | ||
const deliveryPromise = analytics.track('Test event').catch(() => {}) | ||
|
||
// expect 2 calls, triggered report, followed enriched report | ||
expect(window.__SEGMENT_INSPECTOR__?.trace).toHaveBeenCalledTimes(2) | ||
|
||
expect(window.__SEGMENT_INSPECTOR__?.trace).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
stage: 'triggered', | ||
timestamp: timestampMatcher, | ||
event: expect.objectContaining({ | ||
event: 'Test event', | ||
type: 'track', | ||
}), | ||
}) | ||
) | ||
|
||
await deliveryPromise | ||
|
||
// triggered -> enriched -> delivered | ||
expect(window.__SEGMENT_INSPECTOR__?.trace).toHaveBeenCalledTimes(3) | ||
|
||
expect(window.__SEGMENT_INSPECTOR__?.trace).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
stage: 'delivered', | ||
timestamp: timestampMatcher, | ||
event: expect.objectContaining({ | ||
event: 'Test event', | ||
type: 'track', | ||
}), | ||
}) | ||
) | ||
}) | ||
}) |
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,15 @@ | ||
import type { Inspector } from '@segment/inspector-core' | ||
import { getGlobal } from '../../lib/get-global' | ||
|
||
declare global { | ||
interface Window { | ||
__SEGMENT_INSPECTOR__?: Inspector | ||
} | ||
} | ||
|
||
const env = getGlobal() | ||
|
||
export const inspectorHost: Inspector = { | ||
start: (...args) => (env as any)?.['__SEGMENT_INSPECTOR__']?.start(...args), | ||
trace: (...args) => (env as any)?.['__SEGMENT_INSPECTOR__']?.trace(...args), | ||
} |
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