Misleading 201 Insight Created Status Response
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!
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
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:
- Use postman 2 to make a post request to
localhost:8010/api/projects/1/insights. My insight is the same as yours - Visit http://localhost:8010/project/1/insights
I have also tried it on production and it seems to work!
@PostHog/team-product-analytics
This happens if you create the insights with the mcp server.
This happens if you create the insights with the mcp server.
@joshsny