KosmoKrator

analytics

Segment Lua API for KosmoKrator Agents

Agent-facing Lua documentation and function reference for the Segment KosmoKrator integration.

8 functions 4 read 4 write API token auth

Lua Namespace

Agents call this integration through app.integrations.segment.*. Use lua_read_doc("integrations.segment") inside KosmoKrator to discover the same reference at runtime.

Agent-Facing Lua Docs

This is the rendered version of the full Lua documentation exposed to agents when they inspect the integration namespace.

Segment — Lua API Reference

identify

Identify a user in Segment with their traits. Links metadata about a user to a known userId.

Parameters

NameTypeRequiredDescription
userIdstringyesThe unique identifier for the user in your database
traitsobjectnoKey-value pairs of user traits (e.g., name, email, plan, role, company)

Examples

-- Identify a user with traits
app.integrations.segment.identify({
  userId = "user-42",
  traits = {
    name = "Jane Doe",
    email = "[email protected]",
    plan = "pro",
    role = "admin"
  }
})
-- Update a user's plan
app.integrations.segment.identify({
  userId = "user-42",
  traits = {
    plan = "enterprise",
    upgraded_at = "2026-04-05"
  }
})

track

Track a custom event for a user in Segment.

Parameters

NameTypeRequiredDescription
eventstringyesThe name of the event (e.g., “Order Completed”)
userIdstringyesThe unique identifier for the user
propertiesobjectnoKey-value pairs of event properties

Examples

-- Track a purchase event
app.integrations.segment.track({
  event = "Order Completed",
  userId = "user-42",
  properties = {
    revenue = 99.99,
    currency = "USD",
    productId = "widget-3000",
    quantity = 2
  }
})
-- Track a button click
app.integrations.segment.track({
  event = "CTA Clicked",
  userId = "user-42",
  properties = {
    button = "Upgrade to Pro",
    page = "/pricing"
  }
})

page

Record a page view in Segment.

Parameters

NameTypeRequiredDescription
namestringyesThe name of the page viewed
userIdstringyesThe unique identifier for the user
propertiesobjectnoKey-value pairs of page properties (url, referrer, title, path)

Examples

-- Record a page view
app.integrations.segment.page({
  name = "Product Listing",
  userId = "user-42",
  properties = {
    url = "/products/widgets",
    category = "Widgets",
    referrer = "https://google.com"
  }
})

group

Associate a user with a group (organization, company, account) in Segment.

Parameters

NameTypeRequiredDescription
groupIdstringyesThe unique identifier for the group
userIdstringyesThe unique identifier for the user
traitsobjectnoKey-value pairs of group traits (name, plan, industry)

Examples

-- Add user to an organization
app.integrations.segment.group({
  groupId = "org-123",
  userId = "user-42",
  traits = {
    name = "Acme Corp",
    plan = "enterprise",
    industry = "Technology",
    employee_count = 250
  }
})

get_workspace

Get details of a Segment workspace by its slug. Requires an API token.

Parameters

NameTypeRequiredDescription
slugstringyesThe workspace slug (e.g., “my-workspace”)

Examples

-- Get workspace details
local ws = app.integrations.segment.get_workspace({
  slug = "my-workspace"
})
print(ws.name)
print(ws.id)

list_sources

List all sources in a Segment workspace. Requires an API token.

Parameters

NameTypeRequiredDescription
slugstringyesThe workspace slug

Examples

-- List all sources
local result = app.integrations.segment.list_sources({
  slug = "my-workspace"
})

for _, source in ipairs(result.sources or {}) do
  print(source.name .. " (" .. source.id .. ")")
end

get_source

Get details of a specific Segment source. Requires an API token.

Parameters

NameTypeRequiredDescription
slugstringyesThe workspace slug
idstringyesThe source ID

Examples

-- Get source details
local source = app.integrations.segment.get_source({
  slug = "my-workspace",
  id = "abc123"
})
print(source.name)
print(source.write_key)

get_current_user

Get the currently authenticated Segment user. Requires an API token. Useful for verifying credentials.

Parameters

None.

Examples

-- Verify API token is working
local user = app.integrations.segment.get_current_user({})
print("Authenticated as: " .. user.email)

Multi-Account Usage

If you have multiple Segment accounts configured, use account-specific namespaces:

-- Default account (always works)
app.integrations.segment.track({...})

-- Explicit default (portable across setups)
app.integrations.segment.default.track({...})

-- Named accounts
app.integrations.segment.production.track({...})
app.integrations.segment.staging.track({...})

All functions are identical across accounts — only the credentials differ.

Raw agent markdown
# Segment — Lua API Reference

## identify

Identify a user in Segment with their traits. Links metadata about a user to a known userId.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `userId` | string | yes | The unique identifier for the user in your database |
| `traits` | object | no | Key-value pairs of user traits (e.g., name, email, plan, role, company) |

### Examples

```lua
-- Identify a user with traits
app.integrations.segment.identify({
  userId = "user-42",
  traits = {
    name = "Jane Doe",
    email = "[email protected]",
    plan = "pro",
    role = "admin"
  }
})
```

```lua
-- Update a user's plan
app.integrations.segment.identify({
  userId = "user-42",
  traits = {
    plan = "enterprise",
    upgraded_at = "2026-04-05"
  }
})
```

---

## track

Track a custom event for a user in Segment.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `event` | string | yes | The name of the event (e.g., "Order Completed") |
| `userId` | string | yes | The unique identifier for the user |
| `properties` | object | no | Key-value pairs of event properties |

### Examples

```lua
-- Track a purchase event
app.integrations.segment.track({
  event = "Order Completed",
  userId = "user-42",
  properties = {
    revenue = 99.99,
    currency = "USD",
    productId = "widget-3000",
    quantity = 2
  }
})
```

```lua
-- Track a button click
app.integrations.segment.track({
  event = "CTA Clicked",
  userId = "user-42",
  properties = {
    button = "Upgrade to Pro",
    page = "/pricing"
  }
})
```

---

## page

Record a page view in Segment.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `name` | string | yes | The name of the page viewed |
| `userId` | string | yes | The unique identifier for the user |
| `properties` | object | no | Key-value pairs of page properties (url, referrer, title, path) |

### Examples

```lua
-- Record a page view
app.integrations.segment.page({
  name = "Product Listing",
  userId = "user-42",
  properties = {
    url = "/products/widgets",
    category = "Widgets",
    referrer = "https://google.com"
  }
})
```

---

## group

Associate a user with a group (organization, company, account) in Segment.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `groupId` | string | yes | The unique identifier for the group |
| `userId` | string | yes | The unique identifier for the user |
| `traits` | object | no | Key-value pairs of group traits (name, plan, industry) |

### Examples

```lua
-- Add user to an organization
app.integrations.segment.group({
  groupId = "org-123",
  userId = "user-42",
  traits = {
    name = "Acme Corp",
    plan = "enterprise",
    industry = "Technology",
    employee_count = 250
  }
})
```

---

## get_workspace

Get details of a Segment workspace by its slug. Requires an API token.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `slug` | string | yes | The workspace slug (e.g., "my-workspace") |

### Examples

```lua
-- Get workspace details
local ws = app.integrations.segment.get_workspace({
  slug = "my-workspace"
})
print(ws.name)
print(ws.id)
```

---

## list_sources

List all sources in a Segment workspace. Requires an API token.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `slug` | string | yes | The workspace slug |

### Examples

```lua
-- List all sources
local result = app.integrations.segment.list_sources({
  slug = "my-workspace"
})

for _, source in ipairs(result.sources or {}) do
  print(source.name .. " (" .. source.id .. ")")
end
```

---

## get_source

Get details of a specific Segment source. Requires an API token.

### Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `slug` | string | yes | The workspace slug |
| `id` | string | yes | The source ID |

### Examples

```lua
-- Get source details
local source = app.integrations.segment.get_source({
  slug = "my-workspace",
  id = "abc123"
})
print(source.name)
print(source.write_key)
```

---

## get_current_user

Get the currently authenticated Segment user. Requires an API token. Useful for verifying credentials.

### Parameters

None.

### Examples

```lua
-- Verify API token is working
local user = app.integrations.segment.get_current_user({})
print("Authenticated as: " .. user.email)
```

---

## Multi-Account Usage

If you have multiple Segment accounts configured, use account-specific namespaces:

```lua
-- Default account (always works)
app.integrations.segment.track({...})

-- Explicit default (portable across setups)
app.integrations.segment.default.track({...})

-- Named accounts
app.integrations.segment.production.track({...})
app.integrations.segment.staging.track({...})
```

All functions are identical across accounts — only the credentials differ.

Metadata-Derived Lua Example

local result = app.integrations.segment.segment_identify({
  userId = "example_userId",
  traits = "example_traits"
})
print(result)

Functions

segment_identify

Identify a user in Segment with their traits. Links metadata about a user (name, email, plan, etc.) to a known userId so all their events can be attributed correctly.

Operation
Write write
Full name
segment.segment_identify
ParameterTypeRequiredDescription
userId string yes The unique identifier for the user in your database.
traits object no Key-value pairs of user traits (e.g., name, email, plan, role, company).

segment_track

Track a custom event for a user in Segment. Records actions your users perform along with optional properties describing the action.

Operation
Write write
Full name
segment.segment_track
ParameterTypeRequiredDescription
event string yes The name of the event being tracked (e.g., "Order Completed", "Button Clicked").
userId string yes The unique identifier for the user in your database.
properties object no Key-value pairs of event properties (e.g., revenue, category, productId).

segment_page

Record a page view in Segment. Tracks when a user views a page, along with optional properties about the page.

Operation
Write write
Full name
segment.segment_page
ParameterTypeRequiredDescription
name string yes The name of the page viewed (e.g., "Homepage", "Product Listing").
userId string yes The unique identifier for the user in your database.
properties object no Key-value pairs of page properties (e.g., url, referrer, title, path).

segment_group

Associate a user with a group (organization, company, account) in Segment. Lets you record group membership along with optional group traits.

Operation
Write write
Full name
segment.segment_group
ParameterTypeRequiredDescription
groupId string yes The unique identifier for the group (e.g., company ID, organization ID).
userId string yes The unique identifier for the user in your database.
traits object no Key-value pairs of group traits (e.g., name, plan, industry, employee_count).

segment_get_workspace

Get details of a Segment workspace by its slug. Requires an API token to be configured.

Operation
Read read
Full name
segment.segment_get_workspace
ParameterTypeRequiredDescription
slug string yes The workspace slug (e.g., "my-workspace").

segment_list_sources

List all sources in a Segment workspace. Requires an API token to be configured.

Operation
Read read
Full name
segment.segment_list_sources
ParameterTypeRequiredDescription
slug string yes The workspace slug (e.g., "my-workspace").

segment_get_source

Get details of a specific Segment source by ID. Requires an API token to be configured.

Operation
Read read
Full name
segment.segment_get_source
ParameterTypeRequiredDescription
slug string yes The workspace slug (e.g., "my-workspace").
id string yes The source ID (e.g., "abc123").

segment_get_current_user

Get the currently authenticated Segment user. Useful for verifying API token credentials are correct.

Operation
Read read
Full name
segment.segment_get_current_user
ParameterTypeRequiredDescription
No parameters.