Skip to content

Segment

Open in ChatGPT Open in Claude

The Foldspace SDK forwards agent lifecycle and interaction events to Segment when the Segment analytics.js library is already loaded on the page. Segment then fans those events out to every destination you have connected, so agent activity flows into the rest of your product analytics.

Forwarding is opt-in: no events are sent until you call enableAnalyticsForwarding. All forwarding is wrapped in try/catch and never blocks or fails the agent.

The Segment analytics.js library must be loaded and accessible as window.analytics. If it is not found, a warning is logged and no error is thrown.

Forward all events to Segment:

const agent = foldspace.agent('YOUR-AGENT-KEY');
agent.enableAnalyticsForwarding({
providers: ["segment"]
});

Forward specific events only:

agent.enableAnalyticsForwarding({
providers: ["segment"],
events: ["agent.ready", "conversation.created", "action.called"]
});

Each event is sent with analytics.track(), named with the foldspace. prefix (e.g. analytics.track("foldspace.conversation.created", { agentName })).

Enables forwarding of agent events to analytics providers found on window.

Parameters:

ParameterTypeRequiredDescription
configAnalyticsForwardingConfig | nullNoProvider and event selection. If omitted, all supported providers and all events are enabled by default.

AnalyticsForwardingConfig:

FieldTypeDefaultDescription
providers("mixpanel" | "amplitude" | "segment")[]["mixpanel", "amplitude", "segment"]Which analytics providers to forward events to. Set to ["segment"] to forward to Segment only. The provider must be available on window.
eventsstring[]All eventsWhich events to forward. If omitted, all supported events are forwarded. Invalid event names are silently skipped.

Returns: The agent instance, enabling method chaining.

All forwarded events are prefixed with foldspace. (e.g. foldspace.agent.ready).

EventDescription
agent.readyAgent has initialized and is ready
agent.removedAgent has been removed from the page
agent.openAgent UI was opened
agent.closedAgent UI was closed
conversation.createdA new conversation was started
conversation.historyAn existing conversation was loaded
action.calledAn action was executed
action.responseAn action returned a response

Every event payload is automatically enriched with agentName.

  • If called before the agent is ready, the call is queued and replayed once the agent initializes.
  • Only the payload properties listed above are forwarded per event. No sensitive or unrelated data is sent.
  • To forward to other providers as well, add them to the providers array or see Amplitude and Mixpanel.