Skip to content

Commit 1dbffec

Browse files
authored
First draft of a client for signals using segmentio (#1094)
1 parent 7f45c97 commit 1dbffec

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

packages/browser/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export type { AnalyticsSnippet } from './browser/standalone-interface'
1616
export type { MiddlewareFunction } from './plugins/middleware'
1717
export { getGlobalAnalytics } from './lib/global-analytics-helper'
1818
export { UniversalStorage, Store, StorageObject } from './core/storage'
19+
export { segmentio } from './plugins/segmentio'
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { SignalsClient } from '../client'
2+
3+
import { createSuccess } from '../../../../../../browser/src/test-helpers/factories'
4+
import unfetch from 'unfetch'
5+
jest.mock('unfetch')
6+
jest
7+
.mocked(unfetch)
8+
.mockImplementation(() => createSuccess({ integrations: {} }))
9+
10+
describe(SignalsClient, () => {
11+
let client: SignalsClient
12+
13+
beforeEach(() => {
14+
client = new SignalsClient('Test')
15+
})
16+
17+
it('return a ctx from track call', () => {
18+
expect(client).toBeTruthy()
19+
return expect(client.send('Test', 'Test')).resolves.toMatchObject({
20+
event: {
21+
anonymousId: expect.any(String),
22+
context: expect.any(Object),
23+
event: 'Segment Signal Generated',
24+
integrations: {},
25+
messageId: expect.any(String),
26+
properties: {
27+
data: 'Test',
28+
index: expect.any(Number),
29+
type: 'Test',
30+
},
31+
timestamp: expect.any(Date),
32+
type: 'track',
33+
},
34+
})
35+
})
36+
})
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Analytics, segmentio } from '@segment/analytics-next'
2+
3+
export class SignalsClient {
4+
private index = 0
5+
private analytics: Analytics
6+
7+
constructor(writeKey: any) {
8+
this.analytics = new Analytics({ writeKey })
9+
void this.analytics.register(
10+
segmentio(this.analytics, {
11+
apiHost: 'https://signals.segment.io/v1',
12+
apiKey: writeKey,
13+
deliveryStrategy: {
14+
strategy: 'batching',
15+
config: {
16+
size: 100,
17+
timeout: 30 * 1000,
18+
},
19+
},
20+
})
21+
)
22+
}
23+
24+
public send(type: any, data: any): Promise<any> {
25+
return this.analytics
26+
.track('Segment Signal Generated', {
27+
type,
28+
index: this.index++,
29+
data,
30+
})
31+
.then((ctx) => {
32+
return ctx
33+
})
34+
}
35+
}

0 commit comments

Comments
 (0)