Skip to content

Commit

Permalink
First draft of a client for signals using segmentio (#1094)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelGHSeg authored Jun 5, 2024
1 parent 7f45c97 commit 1dbffec
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export type { AnalyticsSnippet } from './browser/standalone-interface'
export type { MiddlewareFunction } from './plugins/middleware'
export { getGlobalAnalytics } from './lib/global-analytics-helper'
export { UniversalStorage, Store, StorageObject } from './core/storage'
export { segmentio } from './plugins/segmentio'
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 packages/signals/browser-signals/src/core/signals/client.ts
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
})
}
}

0 comments on commit 1dbffec

Please sign in to comment.