posthog-js icon indicating copy to clipboard operation
posthog-js copied to clipboard

Misleading 201 Insight Created Status Response

Open chaytanc opened this issue 3 months ago • 5 comments

Bug description

Creating insights via POST to the api endpoint /api/projects/:project_id/insights/ will often return a 201 created status but I cannot see the insight on the Dashboard or in the project files anywhere. When I do a fetch(${POSTHOG_HOST}/api/environments/${POSTHOG_PROJECT_ID}/insights/) query, I see that they were created, but they are not visible anywhere in the UI. I think this is because the wrong parameters were used to create them, but if so it should throw an error instead.

How to reproduce

const insights: any[] = [
  {
    _create_in_folder: "Unfiled/Insights",
    derived_name: "cart_page_viewed count",
    description: "Percentage of users who view cart and proceed to checkout",
    name: "🛒 Cart to Checkout Conversion",
    filters: {},
    query: {
      kind: "InsightVizNode",
      source: {
        kind: "TrendsQuery",
        series: [
          {
            kind: "EventsNode",
            event: "cart_page_viewed",
            name: "cart_page_viewed",
            math: "total"
          },
          {
            kind: "EventsNode",
            event: "checkout_page_viewed",
            name: "checkout_page_viewed",
            math: "total"
          }
        ],
        version: 2,
        trendsFilter: {}
      }
    },
    saved: true,
    tags: [],
  },]
for (const insight of insights) {
    const response = await fetch(`${POSTHOG_HOST}/api/projects/${POSTHOG_PROJECT_ID}/insights/`, {
      method: 'POST',
      headers: {
        'Authorization': `Bearer ${POSTHOG_API_KEY}`,
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(insight)
    })

    console.log(`📊 Response Status: ${response.status} ${response.statusText}`)
}

Related sub-libraries

  • [ ] All of them
  • [x ] posthog-js (web)
  • [ ] posthog-js-lite (web lite)
  • [ ] posthog-node
  • [ ] posthog-react-native
  • [ ] @posthog/react
  • [ ] @posthog/ai
  • [ ] @posthog/nextjs-config

Additional context

Thank you for your bug report – we love squashing them!

chaytanc avatar Sep 30 '25 17:09 chaytanc

It seems that you must have a dashboards: [{some valid id}] field filled in order for the created insight to actually show up in product analytics https://us.posthog.com/project/{project_id}/insights

This should not be required for creating an insight and if it is, it should be documented

chaytanc avatar Oct 01 '25 18:10 chaytanc

Hi @chaytanc, may I ask how you are reproducing the bug mentioned?

I tried to reproduce but it is showing up for me.

My steps:

  1. Use postman 2 to make a post request to localhost:8010/api/projects/1/insights. My insight is the same as yours
  2. Visit http://localhost:8010/project/1/insights
Image

I have also tried it on production and it seems to work!

lshaowei18 avatar Oct 06 '25 03:10 lshaowei18

@PostHog/team-product-analytics

marandaneto avatar Oct 06 '25 11:10 marandaneto

This happens if you create the insights with the mcp server.

HarrisonJackson avatar Oct 07 '25 05:10 HarrisonJackson

This happens if you create the insights with the mcp server.

@joshsny

marandaneto avatar Oct 07 '25 11:10 marandaneto