|
1 | 1 | # @segment/analytics-signals-runtime
|
2 | 2 | Encapsulates Signals runtime functionality, in order to share logic between the signals plugins for browser and mobile.
|
3 | 3 |
|
| 4 | +### Installation |
| 5 | +```bash |
| 6 | +# npm |
| 7 | +npm install @segment/analytics-signals-runtime |
| 8 | +# yarn |
| 9 | +yarn add @segment/analytics-signals-runtime |
| 10 | +# pnpm |
| 11 | +pnpm install @segment/analytics-signals-runtime |
| 12 | +``` |
| 13 | + |
| 14 | +### Usage |
| 15 | + |
| 16 | +### Importing the signals runtime as a module (e.g. `signals.find()`) |
| 17 | + |
| 18 | +```ts |
| 19 | +import { WebSignalsRuntime, Signal } from '@segment/analytics-signals-runtime' |
| 20 | +// MobileSignalsRuntime |
| 21 | +// WebRuntimeConstants, |
| 22 | +// MobileRuntimeConstants, |
| 23 | + |
| 24 | +const mockSignalBuffer: Signal[] = [{ |
| 25 | + type: "network", |
| 26 | + data: { |
| 27 | + action: "response", |
| 28 | + url: "https://segment-integrations.github.io/segment-shop-auto/cart", |
| 29 | + data: { foo: "bar" }, |
| 30 | + ok: true, |
| 31 | + status: 201, |
| 32 | + contentType: "application/json", |
| 33 | + page: { |
| 34 | + path: "/segment-shop-auto/", |
| 35 | + referrer: "", |
| 36 | + title: "Segment Shop - Home Collection - Timecraft", |
| 37 | + search: "", |
| 38 | + url: "https://segment-integrations.github.io/segment-shop-auto/#/home-collection/Timecraft", |
| 39 | + hostname: "segment-integrations.github.io", |
| 40 | + hash: "#/home-collection/Timecraft" |
| 41 | + } |
| 42 | + } |
| 43 | +}] |
| 44 | + |
| 45 | +const signals = new WebSignalsRuntime(mockSignalBuffer) |
| 46 | + |
| 47 | +const networkSignal = signals.find(null, 'network') |
| 48 | + |
| 49 | +``` |
| 50 | +### Importing the signals runtime a JS string |
| 51 | +```ts |
| 52 | +import { getRuntimeCode, getMobileRuntimeCode } from '@segment/analytics-signals-runtime' |
| 53 | + |
| 54 | +eval(` |
| 55 | + ${getMobileRuntimeCode()} |
| 56 | + signals.signalBuffer = [....] |
| 57 | + `) |
| 58 | +``` |
| 59 | + |
| 60 | +### API documentation |
| 61 | +For all exports, explore [the index.ts](src/index.ts). |
| 62 | + |
4 | 63 | ### Development
|
5 | 64 | `yarn build` generate the following artifacts:
|
6 | 65 | | Generated File(s) | Description |
|
7 | 66 | |--------|-------------|
|
8 | 67 | | `/dist/runtime/index.[platform].js`, `/[platform]/get-runtime-code.generated.js` | Exposes `globalThis.Signals` and constants (e.g. `SignalType`), either through the script tag or in the mobile JS engine |
|
9 | 68 | | `/dist/editor/[platform]-editor.d.ts.txt` | Type definitions for monaco editor on app.segment.com
|
10 |
| -| `/dist/esm/index.js` | Entry point for `@segment/analytics-signals` and the segment app, for consumers that want to consume the types / runtime code as an npm package. | |
| 69 | +| `/dist/esm/index.js` | Entry point for `@segment/analytics-signals` and the segment app, for consumers that want to consume the types / runtime code as an npm package. | |
0 commit comments