Skip to content

Commit

Permalink
Add signals enums (#1110)
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky authored Jul 18, 2024
1 parent 2d89b1d commit 8d06af2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-plums-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@segment/analytics-signals': minor
---

Add enums
5 changes: 3 additions & 2 deletions packages/signals/signals/src/core/processor/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import { logger } from '../../lib/logger'
import createWorkerBox from 'workerboxjs'

import { AnalyticsRuntimePublicApi, Signal } from '../../types'
import { AnalyticsRuntimePublicApi, Signal, AnalyticsEnums } from '../../types'
import { createSignalsRuntime } from './signals-runtime'
import { replaceBaseUrl } from '../../lib/replace-base-url'

Expand Down Expand Up @@ -183,6 +183,7 @@ export class Sandbox {
const analytics = new AnalyticsRuntime()
const scope = {
analytics,
...AnalyticsEnums,
}
logger.debug('processing signal', { signal, scope, signals })
const code = [
Expand All @@ -191,7 +192,7 @@ export class Sandbox {
`const signals = createSignalsRuntime(${JSON.stringify(signals)})`,
'try { processSignal(' +
JSON.stringify(signal) +
', { analytics, signals }); } catch(err) { console.error("Process signal failed.", err); }',
', { analytics, signals, SignalType, EventType, NavigationAction }); } catch(err) { console.error("Process signal failed.", err); }',
].join('\n')
await this.jsSandbox.run(code, scope)

Expand Down
26 changes: 25 additions & 1 deletion packages/signals/signals/src/types/process-signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,31 @@ export interface AnalyticsRuntimePublicApi {
export type ProcessSignalScope = {
analytics: AnalyticsRuntimePublicApi
signals: SignalsRuntime
}
} & typeof AnalyticsEnums

export interface ProcessSignal {
(signal: Signal, ctx: ProcessSignalScope): void
}

export const AnalyticsEnums = {
SignalType: Object.freeze({
Interaction: 'interaction',
Navigation: 'navigation',
Network: 'network',
LocalData: 'localData',
Instrumentation: 'instrumentation',
UserDefined: 'userDefined',
}),
EventType: Object.freeze({
Track: 'track',
Page: 'page',
Screen: 'screen',
Identify: 'identify',
Group: 'group',
Alias: 'alias',
}),
NavigationAction: Object.freeze({
URLChange: 'urlChange',
PageLoad: 'pageLoad',
}),
}

0 comments on commit 8d06af2

Please sign in to comment.