Skip to content

Commit d9950a6

Browse files
authored
New Components - posthog (#16560)
* new components * pnpm-lock.yaml * updates
1 parent b9f6a17 commit d9950a6

File tree

9 files changed

+290
-6
lines changed

9 files changed

+290
-6
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.1",
7+
version: "0.0.2",
88
type: "action",
99
props: {
1010
posthog,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import posthog from "../../posthog.app.mjs";
2+
3+
export default {
4+
key: "posthog-create-query",
5+
name: "Create Query",
6+
description: "Create a HogQLQuery and return the results. [See the documentation](https://posthog.com/docs/api/queries#creating-a-query)",
7+
version: "0.0.1",
8+
type: "action",
9+
props: {
10+
posthog,
11+
organizationId: {
12+
propDefinition: [
13+
posthog,
14+
"organizationId",
15+
],
16+
},
17+
projectId: {
18+
propDefinition: [
19+
posthog,
20+
"projectId",
21+
(c) => ({
22+
organizationId: c.organizationId,
23+
}),
24+
],
25+
},
26+
query: {
27+
type: "string",
28+
label: "Query",
29+
description: "The query specifying what data to retrieve. Example: `select properties.email from persons where properties.email is not null`",
30+
},
31+
name: {
32+
type: "string",
33+
label: "Name",
34+
description: "A name for the query to better identify it in the query_log table",
35+
optional: true,
36+
},
37+
},
38+
async run({ $ }) {
39+
const response = await this.posthog.createQuery({
40+
$,
41+
projectId: this.projectId,
42+
data: {
43+
query: {
44+
kind: "HogQLQuery",
45+
query: this.query,
46+
name: this.name,
47+
},
48+
},
49+
});
50+
$.export("$summary", "Successfully created and executed query");
51+
return response;
52+
},
53+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import posthog from "../../posthog.app.mjs";
2+
3+
export default {
4+
key: "posthog-get-cohorts",
5+
name: "Get Cohorts",
6+
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.1",
8+
type: "action",
9+
props: {
10+
posthog,
11+
organizationId: {
12+
propDefinition: [
13+
posthog,
14+
"organizationId",
15+
],
16+
},
17+
projectId: {
18+
propDefinition: [
19+
posthog,
20+
"projectId",
21+
(c) => ({
22+
organizationId: c.organizationId,
23+
}),
24+
],
25+
},
26+
maxResults: {
27+
propDefinition: [
28+
posthog,
29+
"maxResults",
30+
],
31+
},
32+
},
33+
async run({ $ }) {
34+
const cohorts = await this.posthog.iterateResults({
35+
fn: this.posthog.listCohorts,
36+
args: {
37+
$,
38+
projectId: this.projectId,
39+
max: this.maxResults,
40+
},
41+
});
42+
$.export("$summary", `Successfully retrieved ${cohorts.length} cohort${cohorts.length === 1
43+
? ""
44+
: "s"}`);
45+
return cohorts;
46+
},
47+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import posthog from "../../posthog.app.mjs";
2+
3+
export default {
4+
key: "posthog-get-persons",
5+
name: "Get Persons",
6+
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.1",
8+
type: "action",
9+
props: {
10+
posthog,
11+
organizationId: {
12+
propDefinition: [
13+
posthog,
14+
"organizationId",
15+
],
16+
},
17+
projectId: {
18+
propDefinition: [
19+
posthog,
20+
"projectId",
21+
(c) => ({
22+
organizationId: c.organizationId,
23+
}),
24+
],
25+
},
26+
email: {
27+
type: "string",
28+
label: "Email",
29+
description: "Filter persons by email (exact match)",
30+
optional: true,
31+
},
32+
search: {
33+
type: "string",
34+
label: "Search",
35+
description: "Search persons, either by email (full text search) or distinct_id (exact match)",
36+
optional: true,
37+
},
38+
maxResults: {
39+
propDefinition: [
40+
posthog,
41+
"maxResults",
42+
],
43+
},
44+
},
45+
async run({ $ }) {
46+
const persons = await this.posthog.iterateResults({
47+
fn: this.posthog.listPersons,
48+
args: {
49+
$,
50+
projectId: this.projectId,
51+
params: {
52+
email: this.email,
53+
search: this.search,
54+
},
55+
max: this.maxResults,
56+
},
57+
});
58+
$.export("$summary", `Successfully retrieved ${persons.length} person${persons.length === 1
59+
? ""
60+
: "s"}`);
61+
return persons;
62+
},
63+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import posthog from "../../posthog.app.mjs";
2+
3+
export default {
4+
key: "posthog-get-surveys",
5+
name: "Get Surveys",
6+
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.1",
8+
type: "action",
9+
props: {
10+
posthog,
11+
organizationId: {
12+
propDefinition: [
13+
posthog,
14+
"organizationId",
15+
],
16+
},
17+
projectId: {
18+
propDefinition: [
19+
posthog,
20+
"projectId",
21+
(c) => ({
22+
organizationId: c.organizationId,
23+
}),
24+
],
25+
},
26+
search: {
27+
type: "string",
28+
label: "Search",
29+
description: "Enter a search term to filter results",
30+
optional: true,
31+
},
32+
maxResults: {
33+
propDefinition: [
34+
posthog,
35+
"maxResults",
36+
],
37+
},
38+
},
39+
async run({ $ }) {
40+
const surveys = await this.posthog.iterateResults({
41+
fn: this.posthog.listSurveys,
42+
args: {
43+
$,
44+
projectId: this.projectId,
45+
params: {
46+
search: this.search,
47+
},
48+
max: this.maxResults,
49+
},
50+
});
51+
$.export("$summary", `Successfully retrieved ${surveys.length} survey${surveys.length === 1
52+
? ""
53+
: "s"}`);
54+
return surveys;
55+
},
56+
};

components/posthog/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/posthog",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"description": "Pipedream PostHog Components",
55
"main": "posthog.app.mjs",
66
"keywords": [
@@ -13,6 +13,6 @@
1313
"access": "public"
1414
},
1515
"dependencies": {
16-
"@pipedream/platform": "^1.6.4"
16+
"@pipedream/platform": "^3.0.3"
1717
}
1818
}

components/posthog/posthog.app.mjs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ export default {
6060
return results?.map(({ name }) => name ) || [];
6161
},
6262
},
63+
maxResults: {
64+
type: "integer",
65+
label: "Max Results",
66+
description: "The maximum number of results to return",
67+
default: 100,
68+
optional: true,
69+
},
6370
},
6471
methods: {
6572
_baseUrl() {
@@ -101,6 +108,30 @@ export default {
101108
...opts,
102109
});
103110
},
111+
listPersons({
112+
projectId, ...opts
113+
}) {
114+
return this._makeRequest({
115+
path: `/api/projects/${projectId}/persons`,
116+
...opts,
117+
});
118+
},
119+
listCohorts({
120+
projectId, ...opts
121+
}) {
122+
return this._makeRequest({
123+
path: `/api/projects/${projectId}/cohorts`,
124+
...opts,
125+
});
126+
},
127+
listSurveys({
128+
projectId, ...opts
129+
}) {
130+
return this._makeRequest({
131+
path: `/api/projects/${projectId}/surveys`,
132+
...opts,
133+
});
134+
},
104135
createQuery({
105136
projectId, ...opts
106137
}) {
@@ -117,5 +148,39 @@ export default {
117148
...opts,
118149
});
119150
},
151+
async *paginate({
152+
fn, args, max,
153+
}) {
154+
args = {
155+
...args,
156+
params: {
157+
...args?.params,
158+
limit: constants.DEFAULT_LIMIT,
159+
offset: 0,
160+
},
161+
};
162+
let hasMore, count = 0;
163+
do {
164+
const {
165+
results, next,
166+
} = await fn(args);
167+
for (const result of results) {
168+
yield result;
169+
if (max && ++count >= max) {
170+
return;
171+
}
172+
}
173+
hasMore = next;
174+
args.params.offset += args.params.limit;
175+
} while (hasMore);
176+
},
177+
async iterateResults(opts = {}) {
178+
const results = [];
179+
const items = this.paginate(opts);
180+
for await (const item of items) {
181+
results.push(item);
182+
}
183+
return results;
184+
},
120185
},
121186
};

components/posthog/sources/new-action-performed/new-action-performed.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default {
55
key: "posthog-new-action-performed",
66
name: "New Action Performed",
77
description: "Emit new event when an action is performed in a project. [See the documentation](https://posthog.com/docs/api/query#post-api-projects-project_id-query)",
8-
version: "0.0.1",
8+
version: "0.0.2",
99
type: "source",
1010
dedupe: "unique",
1111
props: {

pnpm-lock.yaml

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)