Skip to main content

POST /api/components/track

Records a user interaction with a UI component. This endpoint is used to track how users engage with different components in the T1 Component Library, supporting both anonymous and registered users.

Endpoint

POST /api/components/track
Authentication: None (Public endpoint)

Request Body

nombre
string
required
Name of the component being tracked
  • Examples: “Button”, “Modal”, “Card”, “Tooltip”
  • Will be trimmed of whitespace
  • Used to aggregate statistics by component
accion
string
required
Action performed on the component
  • Examples: “click”, “hover”, “open”, “close”, “focus”
  • Will be trimmed of whitespace
  • Used to track specific user interactions
tipo_usuario
string
default:"anonymous"
Type of user performing the action
  • anonymous: User is not logged in (default)
  • registered: User is authenticated
  • Optional field, defaults to “anonymous” if not provided
usuario
string
MongoDB ObjectId of the registered user
  • Required only when tipo_usuario is “registered”
  • Must be a valid MongoDB ObjectId
  • Links the interaction to a specific user account

Response Fields

success
boolean
required
Indicates if the interaction was recorded successfully
message
string
required
Success message: “Interacción registrada”
data
object
required
Created tracking record

Response Examples

{
  "success": true,
  "message": "Interacción registrada",
  "data": {
    "_id": "507f1f77bcf86cd799439012",
    "nombre": "Button",
    "accion": "click",
    "timestamp": "2025-11-27T12:00:00.000Z",
    "tipo_usuario": "anonymous",
    "__v": 0
  }
}

Example Requests

curl -X POST http://localhost:3001/api/components/track \
  -H "Content-Type: application/json" \
  -d '{
    "nombre": "Button",
    "accion": "click",
    "tipo_usuario": "anonymous"
  }'

Error Codes

CodeMessageCause
400Errores de validaciónRequired fields missing or invalid format
500Error interno del servidorDatabase error or other unexpected error

Common Use Cases

  • Component Analytics: Track which components are most frequently used
  • Interaction Patterns: Understand how users interact with specific components
  • A/B Testing: Compare interaction rates between different component variants
  • User Behavior: Analyze differences between anonymous and registered user interactions

Implementation Notes

  • The timestamp is automatically set to the current time when the record is created
  • If tipo_usuario is omitted, it defaults to “anonymous” (see tracking.service.ts:41)
  • The usuario field is optional and should only be provided for registered users
  • All tracking data can be viewed and exported using the export endpoints (requires authentication)

Build docs developers (and LLMs) love