Skip to content

Commit 8673252

Browse files
authored
[ACTION] PostHog - Insights API (#19346)
1 parent e008ffb commit 8673252

File tree

14 files changed

+523
-7
lines changed

14 files changed

+523
-7
lines changed

components/posthog/actions/capture-event/capture-event.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "posthog-capture-event",
55
name: "Capture Event",
66
description: "Captures a given event within the PostHog system. [See the documentation](https://posthog.com/docs/api/post-only-endpoints#single-event)",
7-
version: "0.0.5",
7+
version: "0.0.6",
88
annotations: {
99
destructiveHint: false,
1010
openWorldHint: true,
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import posthog from "../../posthog.app.mjs";
2+
import utils from "../../common/utils.mjs";
3+
4+
export default {
5+
key: "posthog-create-project-insight",
6+
name: "Create Project Insight",
7+
description: "Create a new insight in a project. [See the documentation](https://posthog.com/docs/api/insights#post-api-projects-project_id-insights)",
8+
version: "0.0.1",
9+
annotations: {
10+
destructiveHint: false,
11+
openWorldHint: true,
12+
readOnlyHint: false,
13+
idempotentHint: false,
14+
},
15+
type: "action",
16+
props: {
17+
posthog,
18+
organizationId: {
19+
propDefinition: [
20+
posthog,
21+
"organizationId",
22+
],
23+
},
24+
projectId: {
25+
propDefinition: [
26+
posthog,
27+
"projectId",
28+
(c) => ({
29+
organizationId: c.organizationId,
30+
}),
31+
],
32+
},
33+
name: {
34+
type: "string",
35+
label: "Name",
36+
description: "Name of the insight",
37+
optional: true,
38+
},
39+
derivedName: {
40+
type: "string",
41+
label: "Derived Name",
42+
description: "Derived name of the insight",
43+
optional: true,
44+
},
45+
query: {
46+
propDefinition: [
47+
posthog,
48+
"query",
49+
],
50+
},
51+
description: {
52+
type: "string",
53+
label: "Description",
54+
description: "Description of the insight",
55+
optional: true,
56+
},
57+
tags: {
58+
type: "string[]",
59+
label: "Tags",
60+
description: "Tags for the insight",
61+
optional: true,
62+
},
63+
favorited: {
64+
type: "boolean",
65+
label: "Favorited",
66+
description: "Whether the insight is favorited",
67+
optional: true,
68+
},
69+
deleted: {
70+
type: "boolean",
71+
label: "Deleted",
72+
description: "Whether the insight is deleted",
73+
optional: true,
74+
},
75+
},
76+
async run({ $ }) {
77+
const {
78+
posthog,
79+
projectId,
80+
name,
81+
derivedName,
82+
query,
83+
description,
84+
tags,
85+
favorited,
86+
deleted,
87+
} = this;
88+
89+
const insight = await posthog.createInsight({
90+
$,
91+
projectId,
92+
data: {
93+
name,
94+
derived_name: derivedName,
95+
query: utils.parseJson(query),
96+
description,
97+
tags: utils.parseJson(tags),
98+
favorited,
99+
deleted,
100+
},
101+
});
102+
103+
$.export("$summary", `Successfully created insight${name
104+
? ` "${name}"`
105+
: ""} with ID ${insight.id}`);
106+
return insight;
107+
},
108+
};

components/posthog/actions/create-query/create-query.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "posthog-create-query",
55
name: "Create Query",
66
description: "Create a HogQLQuery and return the results. [See the documentation](https://posthog.com/docs/api/queries#creating-a-query)",
7-
version: "0.0.4",
7+
version: "0.0.5",
88
annotations: {
99
destructiveHint: false,
1010
openWorldHint: true,

components/posthog/actions/get-cohorts/get-cohorts.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "posthog-get-cohorts",
55
name: "Get Cohorts",
66
description: "Retrieve a list of cohorts. [See the documentation](https://posthog.com/docs/api/cohorts#get-api-projects-project_id-cohorts)",
7-
version: "0.0.4",
7+
version: "0.0.5",
88
annotations: {
99
destructiveHint: false,
1010
openWorldHint: true,

components/posthog/actions/get-persons/get-persons.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "posthog-get-persons",
55
name: "Get Persons",
66
description: "Retrieve a list of persons. [See the documentation](https://posthog.com/docs/api/persons#get-api-projects-project_id-persons)",
7-
version: "0.0.4",
7+
version: "0.0.5",
88
annotations: {
99
destructiveHint: false,
1010
openWorldHint: true,
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import posthog from "../../posthog.app.mjs";
2+
3+
export default {
4+
key: "posthog-get-project-insight",
5+
name: "Get Project Insight",
6+
description: "Retrieve a specific insight from a project. [See the documentation](https://posthog.com/docs/api/insights#get-api-projects-project_id-insights-id)",
7+
version: "0.0.1",
8+
annotations: {
9+
destructiveHint: false,
10+
openWorldHint: true,
11+
readOnlyHint: true,
12+
idempotentHint: true,
13+
},
14+
type: "action",
15+
props: {
16+
posthog,
17+
organizationId: {
18+
propDefinition: [
19+
posthog,
20+
"organizationId",
21+
],
22+
},
23+
projectId: {
24+
propDefinition: [
25+
posthog,
26+
"projectId",
27+
(c) => ({
28+
organizationId: c.organizationId,
29+
}),
30+
],
31+
},
32+
insightId: {
33+
propDefinition: [
34+
posthog,
35+
"insightId",
36+
(c) => ({
37+
projectId: c.projectId,
38+
}),
39+
],
40+
},
41+
fromDashboard: {
42+
type: "string",
43+
label: "From Dashboard",
44+
description: "Only if loading an insight in the context of a dashboard: The relevant dashboard's ID. When set, the specified dashboard's filters and date range override will be applied.",
45+
optional: true,
46+
},
47+
refresh: {
48+
type: "string",
49+
label: "Refresh",
50+
description: "Whether to refresh the insight. Options: `force_cache`, `blocking`, `async`, `lazy_async`, `force_blocking`, `force_async`",
51+
optional: true,
52+
default: "force_cache",
53+
options: [
54+
"force_cache",
55+
"blocking",
56+
"async",
57+
"lazy_async",
58+
"force_blocking",
59+
"force_async",
60+
],
61+
},
62+
},
63+
async run({ $ }) {
64+
const {
65+
posthog,
66+
projectId,
67+
insightId,
68+
fromDashboard,
69+
refresh,
70+
} = this;
71+
72+
const insight = await posthog.getInsight({
73+
$,
74+
projectId,
75+
insightId,
76+
params: {
77+
from_dashboard: fromDashboard,
78+
refresh,
79+
},
80+
});
81+
82+
$.export("$summary", `Successfully retrieved insight with ID ${insightId}`);
83+
return insight;
84+
},
85+
};

components/posthog/actions/get-surveys/get-surveys.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default {
44
key: "posthog-get-surveys",
55
name: "Get Surveys",
66
description: "Retrieve a list of surveys. [See the documentation](https://posthog.com/docs/api/surveys#get-api-projects-project_id-surveys)",
7-
version: "0.0.4",
7+
version: "0.0.5",
88
annotations: {
99
destructiveHint: false,
1010
openWorldHint: true,
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import posthog from "../../posthog.app.mjs";
2+
import constants from "../../common/constants.mjs";
3+
4+
export default {
5+
key: "posthog-list-project-insights",
6+
name: "List Project Insights",
7+
description: "Retrieve a list of insights for a project. [See the documentation](https://posthog.com/docs/api/insights#get-api-projects-project_id-insights)",
8+
version: "0.0.1",
9+
annotations: {
10+
destructiveHint: false,
11+
openWorldHint: true,
12+
readOnlyHint: true,
13+
idempotentHint: true,
14+
},
15+
type: "action",
16+
props: {
17+
posthog,
18+
organizationId: {
19+
propDefinition: [
20+
posthog,
21+
"organizationId",
22+
],
23+
},
24+
projectId: {
25+
propDefinition: [
26+
posthog,
27+
"projectId",
28+
(c) => ({
29+
organizationId: c.organizationId,
30+
}),
31+
],
32+
},
33+
basic: {
34+
type: "boolean",
35+
label: "Basic",
36+
description: "Return basic insight metadata only (no results, faster).",
37+
optional: true,
38+
},
39+
refresh: {
40+
type: "string",
41+
label: "Refresh",
42+
description: "Whether to refresh the retrieved insights. Options: `force_cache`, `blocking`, `async`, `lazy_async`, `force_blocking`, `force_async`. Default: `force_cache`",
43+
optional: true,
44+
default: "force_cache",
45+
options: constants.REFRESH_OPTIONS,
46+
},
47+
maxResults: {
48+
propDefinition: [
49+
posthog,
50+
"maxResults",
51+
],
52+
},
53+
},
54+
async run({ $ }) {
55+
const {
56+
posthog,
57+
projectId,
58+
basic,
59+
refresh,
60+
maxResults,
61+
} = this;
62+
63+
const insights = await posthog.iterateResults({
64+
fn: posthog.listInsights,
65+
args: {
66+
$,
67+
projectId,
68+
params: {
69+
basic,
70+
refresh,
71+
},
72+
},
73+
max: maxResults,
74+
});
75+
76+
$.export("$summary", `Successfully retrieved ${insights.length} insight${insights.length === 1
77+
? ""
78+
: "s"}`);
79+
return insights;
80+
},
81+
};

0 commit comments

Comments
 (0)