Skip to content

Commit 7eaff32

Browse files
committed
Added signals JS api reference to the readme.
1 parent c9f5901 commit 7eaff32

File tree

1 file changed

+236
-4
lines changed

1 file changed

+236
-4
lines changed

README.md

Lines changed: 236 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ Analytics.main.add(plugin: filters)
107107

108108
## LivePlugins
109109

110-
LivePlugins allows you to write JavaScript-based transformation plugins that process analytics events in your app. These plugins run in a secure, sandboxed JavaScript environment with no network access.
110+
LivePlugins allows you to write JavaScript-based transformation plugins that process analytics events in your app. These plugins run in a secure, sandboxed JavaScript environment with no network access. Also see the [LivePlugins Javascript API Reference](#liveplugins-javascript-api-reference) for more information.
111111

112112
### Plugin Types
113113

@@ -180,7 +180,7 @@ LivePlugins can implement several lifecycle methods:
180180

181181
## Signals
182182

183-
Signals are lightweight data points that capture user interactions and system events in your app. While individual signals provide small pieces of information, they become powerful when combined to generate rich analytics events.
183+
Signals are lightweight data points that capture user interactions and system events in your app. While individual signals provide small pieces of information, they become powerful when combined to generate rich analytics events. Also see the [Signals Javascript API Reference](#signals-javascript-api-reference) for more information.
184184

185185
### Core Concepts
186186

@@ -475,6 +475,8 @@ public protocol SignalJSONBroadcaster: SignalBroadcaster {
475475
476476
The JSON broadcaster is useful when you need to work with the raw dictionary representation of signals before they're converted to JSON.
477477
478+
> Note: Signals is a paid feature that may need to be enabled on your workspace by your Segment representative.
479+
478480
## Destination Filters
479481
480482
Destination Filters allows you to run your Segment workspace's destination filters directly on-device. This feature requires:
@@ -495,9 +497,9 @@ Once configured, your device-mode destinations will automatically respect the sa
495497
496498
> Note: Destination Filters is a paid feature that may need to be enabled on your workspace by your Segment representative.
497499
498-
---
500+
___
499501
500-
## LivePlugins API Reference
502+
## LivePlugins Javascript API Reference
501503
502504
### Utility Functions
503505
@@ -589,3 +591,233 @@ const UpdateType = {
589591
}
590592
```
591593
594+
## Signals JavaScript Runtime API
595+
596+
### Signal Type Constants
597+
598+
```javascript
599+
const SignalType = {
600+
Interaction: "interaction",
601+
Navigation: "navigation",
602+
Network: "network",
603+
LocalData: "localData",
604+
Instrumentation: "instrumentation",
605+
UserDefined: "userDefined"
606+
}
607+
```
608+
609+
### Base Signal Classes
610+
611+
#### RawSignal
612+
Base class for all signals:
613+
614+
```javascript
615+
class RawSignal {
616+
constructor(type, data) {} // Create new signal with type and data
617+
618+
// Properties
619+
anonymousId: string // Anonymous ID from analytics instance
620+
type: SignalType // Type of signal
621+
data: object // Signal-specific data
622+
timestamp: Date // Creation timestamp
623+
index: number // Sequential index (set by signals.add())
624+
}
625+
```
626+
627+
### Navigation Signals
628+
629+
Navigation action constants:
630+
631+
```javascript
632+
const NavigationAction = {
633+
Forward: "forward", // Forward navigation
634+
Backward: "backward", // Backward navigation
635+
Modal: "modal", // Modal presentation
636+
Entering: "entering", // Screen entry
637+
Leaving: "leaving", // Screen exit
638+
Page: "page", // Page change
639+
Popup: "popup" // Popup display
640+
}
641+
```
642+
643+
Navigation signal class:
644+
645+
```javascript
646+
class NavigationSignal extends RawSignal {
647+
constructor(action, screen) {} // Create navigation signal
648+
649+
// Data Properties
650+
data.action: string // NavigationAction value
651+
data.screen: string // Screen identifier
652+
}
653+
```
654+
655+
### Interaction Signals
656+
657+
```javascript
658+
class InteractionSignal extends RawSignal {
659+
constructor(component, info, object) {} // Create interaction signal
660+
661+
// Data Properties
662+
data.component: string // UI component type
663+
data.info: string // Additional information
664+
data.data: object // Custom interaction data
665+
}
666+
```
667+
668+
### Network Signals
669+
670+
Network action constants:
671+
672+
```javascript
673+
const NetworkAction = {
674+
Request: "request", // Outgoing request
675+
Response: "response" // Incoming response
676+
}
677+
```
678+
679+
Network signal class:
680+
681+
```javascript
682+
class NetworkSignal extends RawSignal {
683+
constructor(action, url, object) {} // Create network signal
684+
685+
// Data Properties
686+
data.action: string // NetworkAction value
687+
data.url: string // Request/response URL
688+
data.data: object // Network payload data
689+
}
690+
```
691+
692+
### Local Data Signals
693+
694+
Local data action constants:
695+
696+
```javascript
697+
const LocalDataAction = {
698+
Loaded: "loaded", // Data loaded
699+
Updated: "updated", // Data updated
700+
Saved: "saved", // Data saved
701+
Deleted: "deleted", // Data deleted
702+
Undefined: "undefined" // Other operations
703+
}
704+
```
705+
706+
Local data signal class:
707+
708+
```javascript
709+
class LocalDataSignal extends RawSignal {
710+
constructor(action, identifier, object) {} // Create local data signal
711+
712+
// Data Properties
713+
data.action: string // LocalDataAction value
714+
data.identifier: string // Data identifier
715+
data.data: object // Associated data
716+
}
717+
```
718+
719+
### Instrumentation Signals
720+
721+
Event type constants:
722+
723+
```javascript
724+
const EventType = {
725+
Track: "track", // Track events
726+
Screen: "screen", // Screen events
727+
Identify: "identify", // Identify events
728+
Group: "group", // Group events
729+
Alias: "alias" // Alias events
730+
}
731+
```
732+
733+
Instrumentation signal class:
734+
735+
```javascript
736+
class InstrumentationSignal extends RawSignal {
737+
constructor(rawEvent) {} // Create instrumentation signal
738+
739+
// Data Properties
740+
data.type: string // EventType value
741+
data.rawEvent: object // Original analytics event
742+
}
743+
```
744+
745+
### Signals Buffer Management
746+
747+
The Signals class manages a buffer of recently collected signals:
748+
749+
```javascript
750+
class Signals {
751+
constructor() {} // Create new signals buffer
752+
753+
// Properties
754+
signalBuffer: RawSignal[] // Array of signals
755+
signalCounter: number // Current signal count
756+
maxBufferSize: number // Maximum buffer size (default: 1000)
757+
758+
// Methods
759+
add(signal) {} // Add signal to buffer
760+
getNextIndex() {} // Get next signal index
761+
762+
// Signal Search Methods
763+
find(fromSignal, // Starting signal (optional)
764+
signalType, // Signal type to find (optional)
765+
predicate) {} // Search predicate function
766+
767+
findAndApply(fromSignal, // Starting signal (optional)
768+
signalType, // Signal type to find (optional)
769+
searchPredicate, // Search criteria
770+
applyPredicate) {} // Function to apply to found signal
771+
}
772+
```
773+
774+
A global instance is automatically created and available:
775+
776+
```javascript
777+
let signals = new Signals() // Global signals buffer instance
778+
```
779+
780+
### Usage Examples
781+
782+
Creating and adding signals:
783+
784+
```javascript
785+
// Create navigation signal
786+
let navSignal = new NavigationSignal(
787+
NavigationAction.Entering,
788+
"ProductDetail"
789+
)
790+
signals.add(navSignal)
791+
792+
// Create interaction signal
793+
let buttonSignal = new InteractionSignal(
794+
"button",
795+
"Add to Cart",
796+
{ productId: "123" }
797+
)
798+
signals.add(buttonSignal)
799+
```
800+
801+
Finding related signals:
802+
803+
```javascript
804+
// Find most recent network response
805+
let networkSignal = signals.find(
806+
currentSignal,
807+
SignalType.Network,
808+
(signal) => {
809+
return signal.data.action === NetworkAction.Response
810+
}
811+
)
812+
813+
// Find and process related signals
814+
signals.findAndApply(
815+
currentSignal,
816+
SignalType.Interaction,
817+
(signal) => signal.data.component === "button",
818+
(found) => {
819+
// Process found signal
820+
console.log("Found related interaction:", found)
821+
}
822+
)
823+
```

0 commit comments

Comments
 (0)