medusajs/medusa · error · Error

Event name is required when tracking an event with Posthog

Error message

Event name is required when tracking an event with Posthog

What it means

The Posthog provider's `track` method requires an event name. The provider throws when `data.event` is falsy because the underlying PostHog `capture` call cannot send an unnamed event.

Source

Thrown at packages/modules/providers/analytics-posthog/src/services/posthog-analytics.ts:39

    { logger }: InjectedDependencies,
    options: PosthogAnalyticsServiceOptions
  ) {
    super()
    this.config_ = options
    this.logger_ = logger

    if (!options.posthogEventsKey) {
      throw new Error("Posthog API key is not set, but is required")
    }

    this.client_ = new PostHog(options.posthogEventsKey, {
      host: options.posthogHost || "https://eu.i.posthog.com",
    })
  }

  async track(data: ProviderTrackAnalyticsEventDTO): Promise<void> {
    if (!data.event) {
      throw new Error(
        "Event name is required when tracking an event with Posthog"
      )
    }

    if (!data.actor_id) {
      throw new Error(
        "Actor ID is required when tracking an event with Posthog"
      )
    }

    if (data.group?.id && !data.group?.type) {
      throw new Error(
        "Group type is required if passing group id when tracking an event with Posthog"
      )
    }

    this.client_.capture({
      event: data.event,

View on GitHub (pinned to 5e06e544a2)

Solutions

  1. Pass a non-empty `event` string in the track DTO
  2. If the event name is dynamic, default it: `event: name || 'unnamed_action'`

Example fix

// before
await analytics.track({ actor_id: userId })
// after
await analytics.track({ actor_id: userId, event: 'user_login' })
Defensive patterns

Strategy: validation

Validate before calling

function assertTrackDTO(data: ProviderTrackAnalyticsEventDTO) {
  if (!data.event) throw new TypeError('track: event name is required')
}

Type guard

const isTrackable = (d: ProviderTrackAnalyticsEventDTO): boolean => Boolean(d.event)

Try / catch

try { await provider.track(dto) } catch (e) { logger.warn(`analytics track failed: ${e.message}`) } // analytics should not break the request

Prevention

When it happens

Trigger: Calling `analyticsService.track({ actor_id: '...', event: '' })` or building the DTO dynamically where the event field ends up undefined.

Common situations: Dynamic event-name construction (e.g. `event: `${prefix}_${action}``) where one part is undefined, or migrating from an analytics interface that made event optional.

Related errors


AI-assisted analysis of medusajs/medusa@5e06e544a2 (2026-08-27). Data as JSON: /api/errors/645f46044273b5fd. Report an issue: GitHub.