-
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.
First draft of a client for signals using segmentio (#1094)
- Loading branch information
1 parent
7f45c97
commit 1dbffec
Showing
3 changed files
with
72 additions
and
0 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
36 changes: 36 additions & 0 deletions
36
packages/signals/browser-signals/src/core/signals/__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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { SignalsClient } from '../client' | ||
|
||
import { createSuccess } from '../../../../../../browser/src/test-helpers/factories' | ||
import unfetch from 'unfetch' | ||
jest.mock('unfetch') | ||
jest | ||
.mocked(unfetch) | ||
.mockImplementation(() => createSuccess({ integrations: {} })) | ||
|
||
describe(SignalsClient, () => { | ||
let client: SignalsClient | ||
|
||
beforeEach(() => { | ||
client = new SignalsClient('Test') | ||
}) | ||
|
||
it('return a ctx from track call', () => { | ||
expect(client).toBeTruthy() | ||
return expect(client.send('Test', 'Test')).resolves.toMatchObject({ | ||
event: { | ||
anonymousId: expect.any(String), | ||
context: expect.any(Object), | ||
event: 'Segment Signal Generated', | ||
integrations: {}, | ||
messageId: expect.any(String), | ||
properties: { | ||
data: 'Test', | ||
index: expect.any(Number), | ||
type: 'Test', | ||
}, | ||
timestamp: expect.any(Date), | ||
type: 'track', | ||
}, | ||
}) | ||
}) | ||
}) |
35 changes: 35 additions & 0 deletions
35
packages/signals/browser-signals/src/core/signals/client.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,35 @@ | ||
import { Analytics, segmentio } from '@segment/analytics-next' | ||
|
||
export class SignalsClient { | ||
private index = 0 | ||
private analytics: Analytics | ||
|
||
constructor(writeKey: any) { | ||
this.analytics = new Analytics({ writeKey }) | ||
void this.analytics.register( | ||
segmentio(this.analytics, { | ||
apiHost: 'https://signals.segment.io/v1', | ||
apiKey: writeKey, | ||
deliveryStrategy: { | ||
strategy: 'batching', | ||
config: { | ||
size: 100, | ||
timeout: 30 * 1000, | ||
}, | ||
}, | ||
}) | ||
) | ||
} | ||
|
||
public send(type: any, data: any): Promise<any> { | ||
return this.analytics | ||
.track('Segment Signal Generated', { | ||
type, | ||
index: this.index++, | ||
data, | ||
}) | ||
.then((ctx) => { | ||
return ctx | ||
}) | ||
} | ||
} |