@contractspec/example.integration-posthog 1.57.0 → 1.58.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +34 -33
- package/.turbo/turbo-prebuild.log +1 -0
- package/CHANGELOG.md +13 -0
- package/dist/browser/docs/index.js +50 -0
- package/dist/browser/docs/integration-posthog.docblock.js +50 -0
- package/dist/browser/example.js +33 -0
- package/dist/browser/index.js +358 -0
- package/dist/browser/posthog.js +276 -0
- package/dist/browser/run.js +280 -0
- package/dist/docs/index.d.ts +2 -1
- package/dist/docs/index.d.ts.map +1 -0
- package/dist/docs/index.js +51 -1
- package/dist/docs/integration-posthog.docblock.d.ts +2 -1
- package/dist/docs/integration-posthog.docblock.d.ts.map +1 -0
- package/dist/docs/integration-posthog.docblock.js +25 -29
- package/dist/example.d.ts +2 -6
- package/dist/example.d.ts.map +1 -1
- package/dist/example.js +32 -43
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +358 -4
- package/dist/node/docs/index.js +50 -0
- package/dist/node/docs/integration-posthog.docblock.js +50 -0
- package/dist/node/example.js +33 -0
- package/dist/node/index.js +358 -0
- package/dist/node/posthog.js +276 -0
- package/dist/node/run.js +280 -0
- package/dist/posthog.d.ts +13 -17
- package/dist/posthog.d.ts.map +1 -1
- package/dist/posthog.js +241 -188
- package/dist/run.d.ts +2 -1
- package/dist/run.d.ts.map +1 -0
- package/dist/run.js +277 -8
- package/package.json +69 -26
- package/tsdown.config.js +1 -2
- package/.turbo/turbo-build$colon$bundle.log +0 -33
- package/dist/docs/integration-posthog.docblock.js.map +0 -1
- package/dist/example.js.map +0 -1
- package/dist/posthog.js.map +0 -1
- package/dist/run.js.map +0 -1
- package/tsconfig.tsbuildinfo +0 -1
package/dist/example.d.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
//#region src/example.d.ts
|
|
4
|
-
declare const example: _contractspec_lib_contracts0.ExampleSpec;
|
|
5
|
-
//#endregion
|
|
6
|
-
export { example as default };
|
|
1
|
+
declare const example: import("@contractspec/lib.contracts").ExampleSpec;
|
|
2
|
+
export default example;
|
|
7
3
|
//# sourceMappingURL=example.d.ts.map
|
package/dist/example.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"example.d.ts","
|
|
1
|
+
{"version":3,"file":"example.d.ts","sourceRoot":"","sources":["../src/example.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,OAAO,mDA2BX,CAAC;AAEH,eAAe,OAAO,CAAC"}
|
package/dist/example.js
CHANGED
|
@@ -1,45 +1,34 @@
|
|
|
1
|
+
// @bun
|
|
2
|
+
// src/example.ts
|
|
1
3
|
import { defineExample } from "@contractspec/lib.contracts";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
},
|
|
29
|
-
surfaces: {
|
|
30
|
-
templates: true,
|
|
31
|
-
sandbox: {
|
|
32
|
-
enabled: true,
|
|
33
|
-
modes: ["markdown", "specs"]
|
|
34
|
-
},
|
|
35
|
-
studio: {
|
|
36
|
-
enabled: true,
|
|
37
|
-
installable: true
|
|
38
|
-
},
|
|
39
|
-
mcp: { enabled: true }
|
|
40
|
-
}
|
|
4
|
+
var example = defineExample({
|
|
5
|
+
meta: {
|
|
6
|
+
key: "integration-posthog",
|
|
7
|
+
version: "1.0.0",
|
|
8
|
+
title: "Integration \u2014 PostHog Analytics",
|
|
9
|
+
description: "Capture events, run HogQL, and manage PostHog assets via AnalyticsProvider.",
|
|
10
|
+
kind: "integration",
|
|
11
|
+
visibility: "public",
|
|
12
|
+
stability: "experimental",
|
|
13
|
+
owners: ["@platform.integrations"],
|
|
14
|
+
tags: ["posthog", "analytics", "hogql", "integration"]
|
|
15
|
+
},
|
|
16
|
+
docs: {
|
|
17
|
+
rootDocId: "docs.examples.integration-posthog",
|
|
18
|
+
usageDocId: "docs.examples.integration-posthog.usage"
|
|
19
|
+
},
|
|
20
|
+
entrypoints: {
|
|
21
|
+
packageName: "@contractspec/example.integration-posthog",
|
|
22
|
+
docs: "./docs"
|
|
23
|
+
},
|
|
24
|
+
surfaces: {
|
|
25
|
+
templates: true,
|
|
26
|
+
sandbox: { enabled: true, modes: ["markdown", "specs"] },
|
|
27
|
+
studio: { enabled: true, installable: true },
|
|
28
|
+
mcp: { enabled: true }
|
|
29
|
+
}
|
|
41
30
|
});
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
31
|
+
var example_default = example;
|
|
32
|
+
export {
|
|
33
|
+
example_default as default
|
|
34
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export * from './posthog';
|
|
2
|
+
export { default as example } from './example';
|
|
3
|
+
import './docs';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,WAAW,CAAC;AAC/C,OAAO,QAAQ,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,359 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import "
|
|
1
|
+
// @bun
|
|
2
|
+
// src/docs/integration-posthog.docblock.ts
|
|
3
|
+
import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
|
|
4
|
+
var blocks = [
|
|
5
|
+
{
|
|
6
|
+
id: "docs.examples.integration-posthog",
|
|
7
|
+
title: "Integration Example \u2014 PostHog Analytics",
|
|
8
|
+
summary: "Capture events, run HogQL, and manage feature flags using the PostHog analytics provider.",
|
|
9
|
+
kind: "reference",
|
|
10
|
+
visibility: "public",
|
|
11
|
+
route: "/docs/examples/integration-posthog",
|
|
12
|
+
tags: ["posthog", "analytics", "hogql", "example"],
|
|
13
|
+
body: `## What this example shows
|
|
14
|
+
- Capture product events and identify users.
|
|
15
|
+
- Run HogQL queries through the analytics provider.
|
|
16
|
+
- Perform generic REST API requests (feature flag list/create/delete).
|
|
17
|
+
- Read events, persons, insights, and feature flags using typed reader methods.
|
|
18
|
+
- Optionally call a PostHog MCP tool via JSON-RPC.
|
|
4
19
|
|
|
5
|
-
|
|
20
|
+
## Guardrails
|
|
21
|
+
- Use dry-run while validating credentials.
|
|
22
|
+
- Writes are gated behind CONTRACTSPEC_POSTHOG_ALLOW_WRITES.
|
|
23
|
+
- Keep API keys in secret storage.`
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
id: "docs.examples.integration-posthog.usage",
|
|
27
|
+
title: "PostHog Integration Example \u2014 Usage",
|
|
28
|
+
summary: "How to run the PostHog example with env-driven settings.",
|
|
29
|
+
kind: "usage",
|
|
30
|
+
visibility: "public",
|
|
31
|
+
route: "/docs/examples/integration-posthog/usage",
|
|
32
|
+
tags: ["posthog", "usage"],
|
|
33
|
+
body: `## Usage
|
|
34
|
+
- Set CONTRACTSPEC_POSTHOG_MODE to capture | query | request | read | all.
|
|
35
|
+
- Set CONTRACTSPEC_POSTHOG_DRY_RUN=true for a safe preview.
|
|
36
|
+
- Set CONTRACTSPEC_POSTHOG_ALLOW_WRITES=true to enable capture and create/delete.
|
|
37
|
+
|
|
38
|
+
## Required env vars
|
|
39
|
+
- POSTHOG_PROJECT_ID for queries and REST requests.
|
|
40
|
+
- POSTHOG_PROJECT_API_KEY for capture/identify.
|
|
41
|
+
- POSTHOG_PERSONAL_API_KEY for HogQL queries and REST requests.
|
|
42
|
+
|
|
43
|
+
## MCP
|
|
44
|
+
- Set POSTHOG_MCP_URL to call tools/call.
|
|
45
|
+
- Optionally set POSTHOG_MCP_TOOL_NAME and POSTHOG_MCP_TOOL_ARGS (JSON).
|
|
46
|
+
|
|
47
|
+
## Example
|
|
48
|
+
- Call runPosthogExampleFromEnv() from run.ts to execute the flow.`
|
|
49
|
+
}
|
|
50
|
+
];
|
|
51
|
+
registerDocBlocks(blocks);
|
|
52
|
+
// src/example.ts
|
|
53
|
+
import { defineExample } from "@contractspec/lib.contracts";
|
|
54
|
+
var example = defineExample({
|
|
55
|
+
meta: {
|
|
56
|
+
key: "integration-posthog",
|
|
57
|
+
version: "1.0.0",
|
|
58
|
+
title: "Integration \u2014 PostHog Analytics",
|
|
59
|
+
description: "Capture events, run HogQL, and manage PostHog assets via AnalyticsProvider.",
|
|
60
|
+
kind: "integration",
|
|
61
|
+
visibility: "public",
|
|
62
|
+
stability: "experimental",
|
|
63
|
+
owners: ["@platform.integrations"],
|
|
64
|
+
tags: ["posthog", "analytics", "hogql", "integration"]
|
|
65
|
+
},
|
|
66
|
+
docs: {
|
|
67
|
+
rootDocId: "docs.examples.integration-posthog",
|
|
68
|
+
usageDocId: "docs.examples.integration-posthog.usage"
|
|
69
|
+
},
|
|
70
|
+
entrypoints: {
|
|
71
|
+
packageName: "@contractspec/example.integration-posthog",
|
|
72
|
+
docs: "./docs"
|
|
73
|
+
},
|
|
74
|
+
surfaces: {
|
|
75
|
+
templates: true,
|
|
76
|
+
sandbox: { enabled: true, modes: ["markdown", "specs"] },
|
|
77
|
+
studio: { enabled: true, installable: true },
|
|
78
|
+
mcp: { enabled: true }
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
var example_default = example;
|
|
82
|
+
|
|
83
|
+
// src/posthog.ts
|
|
84
|
+
import { PosthogAnalyticsProvider } from "@contractspec/integration.providers-impls/impls/posthog";
|
|
85
|
+
async function runPosthogExampleFromEnv() {
|
|
86
|
+
const mode = resolvePosthogMode();
|
|
87
|
+
const dryRun = resolveBooleanEnv("CONTRACTSPEC_POSTHOG_DRY_RUN", true);
|
|
88
|
+
const allowWrites = resolveBooleanEnv("CONTRACTSPEC_POSTHOG_ALLOW_WRITES", false);
|
|
89
|
+
const output = { mode, dryRun, allowWrites };
|
|
90
|
+
if (dryRun) {
|
|
91
|
+
if (mode === "capture" || mode === "all") {
|
|
92
|
+
output.capture = buildCapturePreview();
|
|
93
|
+
}
|
|
94
|
+
if (mode === "query" || mode === "all") {
|
|
95
|
+
output.query = { query: buildHogQLQuery() };
|
|
96
|
+
}
|
|
97
|
+
if (mode === "request" || mode === "all") {
|
|
98
|
+
output.request = buildRequestPreview();
|
|
99
|
+
}
|
|
100
|
+
if (mode === "read" || mode === "all") {
|
|
101
|
+
output.read = buildReadPreview();
|
|
102
|
+
}
|
|
103
|
+
if (process.env.POSTHOG_MCP_URL) {
|
|
104
|
+
output.mcp = buildMcpPreview();
|
|
105
|
+
}
|
|
106
|
+
return output;
|
|
107
|
+
}
|
|
108
|
+
const provider = createProviderFromEnv();
|
|
109
|
+
if (mode === "capture" || mode === "all") {
|
|
110
|
+
output.capture = await runCapture(provider, allowWrites);
|
|
111
|
+
}
|
|
112
|
+
if (mode === "query" || mode === "all") {
|
|
113
|
+
output.query = await runHogQLQuery(provider);
|
|
114
|
+
}
|
|
115
|
+
if (mode === "request" || mode === "all") {
|
|
116
|
+
output.request = await runApiRequests(provider, allowWrites);
|
|
117
|
+
}
|
|
118
|
+
if (mode === "read" || mode === "all") {
|
|
119
|
+
output.read = await runReadOperations(provider);
|
|
120
|
+
}
|
|
121
|
+
if (process.env.POSTHOG_MCP_URL) {
|
|
122
|
+
output.mcp = await runMcpToolCall(provider);
|
|
123
|
+
}
|
|
124
|
+
return output;
|
|
125
|
+
}
|
|
126
|
+
function resolvePosthogMode() {
|
|
127
|
+
const raw = (process.env.CONTRACTSPEC_POSTHOG_MODE ?? "all").toLowerCase();
|
|
128
|
+
if (raw === "capture" || raw === "query" || raw === "request" || raw === "read" || raw === "all") {
|
|
129
|
+
return raw;
|
|
130
|
+
}
|
|
131
|
+
throw new Error(`Unsupported CONTRACTSPEC_POSTHOG_MODE: ${raw}. Use capture, query, request, read, or all.`);
|
|
132
|
+
}
|
|
133
|
+
function createProviderFromEnv() {
|
|
134
|
+
return new PosthogAnalyticsProvider({
|
|
135
|
+
host: process.env.POSTHOG_HOST,
|
|
136
|
+
projectId: process.env.POSTHOG_PROJECT_ID,
|
|
137
|
+
projectApiKey: process.env.POSTHOG_PROJECT_API_KEY,
|
|
138
|
+
personalApiKey: process.env.POSTHOG_PERSONAL_API_KEY,
|
|
139
|
+
mcpUrl: process.env.POSTHOG_MCP_URL,
|
|
140
|
+
requestTimeoutMs: 1e4
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
async function runCapture(provider, allowWrites) {
|
|
144
|
+
if (!allowWrites) {
|
|
145
|
+
return {
|
|
146
|
+
skipped: true,
|
|
147
|
+
reason: "Set CONTRACTSPEC_POSTHOG_ALLOW_WRITES=true to enable capture."
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
const event = buildCaptureEvent();
|
|
151
|
+
if (provider.identify) {
|
|
152
|
+
await provider.identify({
|
|
153
|
+
distinctId: event.distinctId,
|
|
154
|
+
properties: { plan: "pro", source: "contractspec-example" }
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
await provider.capture(event);
|
|
158
|
+
return { captured: true, event };
|
|
159
|
+
}
|
|
160
|
+
async function runHogQLQuery(provider) {
|
|
161
|
+
if (!provider.queryHogQL) {
|
|
162
|
+
throw new Error("Analytics provider does not support HogQL queries.");
|
|
163
|
+
}
|
|
164
|
+
const query = buildHogQLQuery();
|
|
165
|
+
return provider.queryHogQL({ query });
|
|
166
|
+
}
|
|
167
|
+
async function runApiRequests(provider, allowWrites) {
|
|
168
|
+
const projectId = requireEnv("POSTHOG_PROJECT_ID");
|
|
169
|
+
const listRequest = {
|
|
170
|
+
method: "GET",
|
|
171
|
+
path: `/api/projects/${projectId}/feature_flags/`,
|
|
172
|
+
query: { limit: 5 }
|
|
173
|
+
};
|
|
174
|
+
const listResponse = await provider.request(listRequest);
|
|
175
|
+
const output = {
|
|
176
|
+
list: listResponse.data
|
|
177
|
+
};
|
|
178
|
+
if (!allowWrites) {
|
|
179
|
+
output.writeGuard = "Set CONTRACTSPEC_POSTHOG_ALLOW_WRITES=true to create/delete feature flags.";
|
|
180
|
+
return output;
|
|
181
|
+
}
|
|
182
|
+
const flagKey = `contractspec-example-${Date.now()}`;
|
|
183
|
+
const createResponse = await provider.request({
|
|
184
|
+
method: "POST",
|
|
185
|
+
path: `/api/projects/${projectId}/feature_flags/`,
|
|
186
|
+
body: {
|
|
187
|
+
name: "ContractSpec Example Flag",
|
|
188
|
+
key: flagKey,
|
|
189
|
+
active: true,
|
|
190
|
+
filters: {
|
|
191
|
+
groups: [
|
|
192
|
+
{
|
|
193
|
+
properties: [],
|
|
194
|
+
rollout_percentage: 100
|
|
195
|
+
}
|
|
196
|
+
]
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
output.created = createResponse.data;
|
|
201
|
+
const createdId = extractId(createResponse);
|
|
202
|
+
if (createdId) {
|
|
203
|
+
const deleteResponse = await provider.request({
|
|
204
|
+
method: "DELETE",
|
|
205
|
+
path: `/api/projects/${projectId}/feature_flags/${createdId}`
|
|
206
|
+
});
|
|
207
|
+
output.deleted = deleteResponse.status;
|
|
208
|
+
} else {
|
|
209
|
+
output.deleted = "Skipped delete: response did not include an id.";
|
|
210
|
+
}
|
|
211
|
+
return output;
|
|
212
|
+
}
|
|
213
|
+
async function runReadOperations(provider) {
|
|
214
|
+
const now = new Date;
|
|
215
|
+
const from = new Date(now.getTime() - 24 * 60 * 60 * 1000);
|
|
216
|
+
const dateRange = { from, to: now };
|
|
217
|
+
const output = {
|
|
218
|
+
window: { from: from.toISOString(), to: now.toISOString() }
|
|
219
|
+
};
|
|
220
|
+
if (provider.getEvents) {
|
|
221
|
+
output.events = await provider.getEvents({ dateRange, limit: 5 });
|
|
222
|
+
} else {
|
|
223
|
+
output.events = "Provider does not support getEvents.";
|
|
224
|
+
}
|
|
225
|
+
if (provider.getPersons) {
|
|
226
|
+
output.persons = await provider.getPersons({ limit: 5 });
|
|
227
|
+
} else {
|
|
228
|
+
output.persons = "Provider does not support getPersons.";
|
|
229
|
+
}
|
|
230
|
+
if (provider.getInsights) {
|
|
231
|
+
output.insights = await provider.getInsights({ limit: 5 });
|
|
232
|
+
} else {
|
|
233
|
+
output.insights = "Provider does not support getInsights.";
|
|
234
|
+
}
|
|
235
|
+
if (provider.getFeatureFlags) {
|
|
236
|
+
output.featureFlags = await provider.getFeatureFlags({ limit: 5 });
|
|
237
|
+
} else {
|
|
238
|
+
output.featureFlags = "Provider does not support getFeatureFlags.";
|
|
239
|
+
}
|
|
240
|
+
return output;
|
|
241
|
+
}
|
|
242
|
+
async function runMcpToolCall(provider) {
|
|
243
|
+
if (!provider.callMcpTool) {
|
|
244
|
+
throw new Error("Analytics provider does not support MCP tool calls.");
|
|
245
|
+
}
|
|
246
|
+
const name = process.env.POSTHOG_MCP_TOOL_NAME ?? "posthog.query";
|
|
247
|
+
const argumentsValue = parseOptionalJsonEnv("POSTHOG_MCP_TOOL_ARGS", {
|
|
248
|
+
query: buildHogQLQuery()
|
|
249
|
+
});
|
|
250
|
+
return provider.callMcpTool({ name, arguments: argumentsValue });
|
|
251
|
+
}
|
|
252
|
+
function buildCaptureEvent() {
|
|
253
|
+
const distinctId = process.env.POSTHOG_DISTINCT_ID ?? "contractspec-demo";
|
|
254
|
+
const event = process.env.POSTHOG_EVENT_NAME ?? "contractspec.example.event";
|
|
255
|
+
return {
|
|
256
|
+
distinctId,
|
|
257
|
+
event,
|
|
258
|
+
properties: {
|
|
259
|
+
source: "contractspec-example",
|
|
260
|
+
environment: "development"
|
|
261
|
+
},
|
|
262
|
+
timestamp: new Date
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
function buildHogQLQuery() {
|
|
266
|
+
return "select event, count() as count " + "from events " + "where timestamp >= now() - interval 1 day " + "group by event " + "order by count desc " + "limit 5";
|
|
267
|
+
}
|
|
268
|
+
function buildCapturePreview() {
|
|
269
|
+
return {
|
|
270
|
+
event: buildCaptureEvent(),
|
|
271
|
+
hint: "Dry run enabled. Set CONTRACTSPEC_POSTHOG_DRY_RUN=false."
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
function buildRequestPreview() {
|
|
275
|
+
const projectId = process.env.POSTHOG_PROJECT_ID ?? "PROJECT_ID";
|
|
276
|
+
return {
|
|
277
|
+
listRequest: {
|
|
278
|
+
method: "GET",
|
|
279
|
+
path: `/api/projects/${projectId}/feature_flags/`,
|
|
280
|
+
query: { limit: 5 }
|
|
281
|
+
},
|
|
282
|
+
writeRequests: {
|
|
283
|
+
create: {
|
|
284
|
+
method: "POST",
|
|
285
|
+
path: `/api/projects/${projectId}/feature_flags/`
|
|
286
|
+
},
|
|
287
|
+
delete: {
|
|
288
|
+
method: "DELETE",
|
|
289
|
+
path: `/api/projects/${projectId}/feature_flags/{id}`
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
function buildReadPreview() {
|
|
295
|
+
return {
|
|
296
|
+
events: {
|
|
297
|
+
dateRange: "last_24_hours",
|
|
298
|
+
limit: 5
|
|
299
|
+
},
|
|
300
|
+
persons: {
|
|
301
|
+
limit: 5
|
|
302
|
+
},
|
|
303
|
+
insights: {
|
|
304
|
+
limit: 5
|
|
305
|
+
},
|
|
306
|
+
featureFlags: {
|
|
307
|
+
limit: 5
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
function buildMcpPreview() {
|
|
312
|
+
return {
|
|
313
|
+
url: process.env.POSTHOG_MCP_URL,
|
|
314
|
+
toolName: process.env.POSTHOG_MCP_TOOL_NAME ?? "posthog.query",
|
|
315
|
+
arguments: parseOptionalJsonEnv("POSTHOG_MCP_TOOL_ARGS", {
|
|
316
|
+
query: buildHogQLQuery()
|
|
317
|
+
})
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
function extractId(response) {
|
|
321
|
+
if (!response || typeof response !== "object")
|
|
322
|
+
return null;
|
|
323
|
+
const data = response.data;
|
|
324
|
+
if (data && (typeof data.id === "string" || typeof data.id === "number")) {
|
|
325
|
+
return data.id;
|
|
326
|
+
}
|
|
327
|
+
return null;
|
|
328
|
+
}
|
|
329
|
+
function resolveBooleanEnv(key, defaultValue) {
|
|
330
|
+
const value = process.env[key];
|
|
331
|
+
if (value === undefined)
|
|
332
|
+
return defaultValue;
|
|
333
|
+
return value.toLowerCase() === "true";
|
|
334
|
+
}
|
|
335
|
+
function requireEnv(key) {
|
|
336
|
+
const value = process.env[key];
|
|
337
|
+
if (!value) {
|
|
338
|
+
throw new Error(`Missing required env var: ${key}`);
|
|
339
|
+
}
|
|
340
|
+
return value;
|
|
341
|
+
}
|
|
342
|
+
function parseOptionalJsonEnv(key, fallback) {
|
|
343
|
+
const raw = process.env[key];
|
|
344
|
+
if (!raw)
|
|
345
|
+
return fallback;
|
|
346
|
+
try {
|
|
347
|
+
const parsed = JSON.parse(raw);
|
|
348
|
+
if (parsed && typeof parsed === "object")
|
|
349
|
+
return parsed;
|
|
350
|
+
} catch {
|
|
351
|
+
throw new Error(`Invalid JSON in ${key}`);
|
|
352
|
+
}
|
|
353
|
+
return fallback;
|
|
354
|
+
}
|
|
355
|
+
export {
|
|
356
|
+
runPosthogExampleFromEnv,
|
|
357
|
+
resolvePosthogMode,
|
|
358
|
+
example_default as example
|
|
359
|
+
};
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// src/docs/integration-posthog.docblock.ts
|
|
2
|
+
import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
|
|
3
|
+
var blocks = [
|
|
4
|
+
{
|
|
5
|
+
id: "docs.examples.integration-posthog",
|
|
6
|
+
title: "Integration Example — PostHog Analytics",
|
|
7
|
+
summary: "Capture events, run HogQL, and manage feature flags using the PostHog analytics provider.",
|
|
8
|
+
kind: "reference",
|
|
9
|
+
visibility: "public",
|
|
10
|
+
route: "/docs/examples/integration-posthog",
|
|
11
|
+
tags: ["posthog", "analytics", "hogql", "example"],
|
|
12
|
+
body: `## What this example shows
|
|
13
|
+
- Capture product events and identify users.
|
|
14
|
+
- Run HogQL queries through the analytics provider.
|
|
15
|
+
- Perform generic REST API requests (feature flag list/create/delete).
|
|
16
|
+
- Read events, persons, insights, and feature flags using typed reader methods.
|
|
17
|
+
- Optionally call a PostHog MCP tool via JSON-RPC.
|
|
18
|
+
|
|
19
|
+
## Guardrails
|
|
20
|
+
- Use dry-run while validating credentials.
|
|
21
|
+
- Writes are gated behind CONTRACTSPEC_POSTHOG_ALLOW_WRITES.
|
|
22
|
+
- Keep API keys in secret storage.`
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
id: "docs.examples.integration-posthog.usage",
|
|
26
|
+
title: "PostHog Integration Example — Usage",
|
|
27
|
+
summary: "How to run the PostHog example with env-driven settings.",
|
|
28
|
+
kind: "usage",
|
|
29
|
+
visibility: "public",
|
|
30
|
+
route: "/docs/examples/integration-posthog/usage",
|
|
31
|
+
tags: ["posthog", "usage"],
|
|
32
|
+
body: `## Usage
|
|
33
|
+
- Set CONTRACTSPEC_POSTHOG_MODE to capture | query | request | read | all.
|
|
34
|
+
- Set CONTRACTSPEC_POSTHOG_DRY_RUN=true for a safe preview.
|
|
35
|
+
- Set CONTRACTSPEC_POSTHOG_ALLOW_WRITES=true to enable capture and create/delete.
|
|
36
|
+
|
|
37
|
+
## Required env vars
|
|
38
|
+
- POSTHOG_PROJECT_ID for queries and REST requests.
|
|
39
|
+
- POSTHOG_PROJECT_API_KEY for capture/identify.
|
|
40
|
+
- POSTHOG_PERSONAL_API_KEY for HogQL queries and REST requests.
|
|
41
|
+
|
|
42
|
+
## MCP
|
|
43
|
+
- Set POSTHOG_MCP_URL to call tools/call.
|
|
44
|
+
- Optionally set POSTHOG_MCP_TOOL_NAME and POSTHOG_MCP_TOOL_ARGS (JSON).
|
|
45
|
+
|
|
46
|
+
## Example
|
|
47
|
+
- Call runPosthogExampleFromEnv() from run.ts to execute the flow.`
|
|
48
|
+
}
|
|
49
|
+
];
|
|
50
|
+
registerDocBlocks(blocks);
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// src/docs/integration-posthog.docblock.ts
|
|
2
|
+
import { registerDocBlocks } from "@contractspec/lib.contracts/docs";
|
|
3
|
+
var blocks = [
|
|
4
|
+
{
|
|
5
|
+
id: "docs.examples.integration-posthog",
|
|
6
|
+
title: "Integration Example — PostHog Analytics",
|
|
7
|
+
summary: "Capture events, run HogQL, and manage feature flags using the PostHog analytics provider.",
|
|
8
|
+
kind: "reference",
|
|
9
|
+
visibility: "public",
|
|
10
|
+
route: "/docs/examples/integration-posthog",
|
|
11
|
+
tags: ["posthog", "analytics", "hogql", "example"],
|
|
12
|
+
body: `## What this example shows
|
|
13
|
+
- Capture product events and identify users.
|
|
14
|
+
- Run HogQL queries through the analytics provider.
|
|
15
|
+
- Perform generic REST API requests (feature flag list/create/delete).
|
|
16
|
+
- Read events, persons, insights, and feature flags using typed reader methods.
|
|
17
|
+
- Optionally call a PostHog MCP tool via JSON-RPC.
|
|
18
|
+
|
|
19
|
+
## Guardrails
|
|
20
|
+
- Use dry-run while validating credentials.
|
|
21
|
+
- Writes are gated behind CONTRACTSPEC_POSTHOG_ALLOW_WRITES.
|
|
22
|
+
- Keep API keys in secret storage.`
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
id: "docs.examples.integration-posthog.usage",
|
|
26
|
+
title: "PostHog Integration Example — Usage",
|
|
27
|
+
summary: "How to run the PostHog example with env-driven settings.",
|
|
28
|
+
kind: "usage",
|
|
29
|
+
visibility: "public",
|
|
30
|
+
route: "/docs/examples/integration-posthog/usage",
|
|
31
|
+
tags: ["posthog", "usage"],
|
|
32
|
+
body: `## Usage
|
|
33
|
+
- Set CONTRACTSPEC_POSTHOG_MODE to capture | query | request | read | all.
|
|
34
|
+
- Set CONTRACTSPEC_POSTHOG_DRY_RUN=true for a safe preview.
|
|
35
|
+
- Set CONTRACTSPEC_POSTHOG_ALLOW_WRITES=true to enable capture and create/delete.
|
|
36
|
+
|
|
37
|
+
## Required env vars
|
|
38
|
+
- POSTHOG_PROJECT_ID for queries and REST requests.
|
|
39
|
+
- POSTHOG_PROJECT_API_KEY for capture/identify.
|
|
40
|
+
- POSTHOG_PERSONAL_API_KEY for HogQL queries and REST requests.
|
|
41
|
+
|
|
42
|
+
## MCP
|
|
43
|
+
- Set POSTHOG_MCP_URL to call tools/call.
|
|
44
|
+
- Optionally set POSTHOG_MCP_TOOL_NAME and POSTHOG_MCP_TOOL_ARGS (JSON).
|
|
45
|
+
|
|
46
|
+
## Example
|
|
47
|
+
- Call runPosthogExampleFromEnv() from run.ts to execute the flow.`
|
|
48
|
+
}
|
|
49
|
+
];
|
|
50
|
+
registerDocBlocks(blocks);
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// src/example.ts
|
|
2
|
+
import { defineExample } from "@contractspec/lib.contracts";
|
|
3
|
+
var example = defineExample({
|
|
4
|
+
meta: {
|
|
5
|
+
key: "integration-posthog",
|
|
6
|
+
version: "1.0.0",
|
|
7
|
+
title: "Integration — PostHog Analytics",
|
|
8
|
+
description: "Capture events, run HogQL, and manage PostHog assets via AnalyticsProvider.",
|
|
9
|
+
kind: "integration",
|
|
10
|
+
visibility: "public",
|
|
11
|
+
stability: "experimental",
|
|
12
|
+
owners: ["@platform.integrations"],
|
|
13
|
+
tags: ["posthog", "analytics", "hogql", "integration"]
|
|
14
|
+
},
|
|
15
|
+
docs: {
|
|
16
|
+
rootDocId: "docs.examples.integration-posthog",
|
|
17
|
+
usageDocId: "docs.examples.integration-posthog.usage"
|
|
18
|
+
},
|
|
19
|
+
entrypoints: {
|
|
20
|
+
packageName: "@contractspec/example.integration-posthog",
|
|
21
|
+
docs: "./docs"
|
|
22
|
+
},
|
|
23
|
+
surfaces: {
|
|
24
|
+
templates: true,
|
|
25
|
+
sandbox: { enabled: true, modes: ["markdown", "specs"] },
|
|
26
|
+
studio: { enabled: true, installable: true },
|
|
27
|
+
mcp: { enabled: true }
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
var example_default = example;
|
|
31
|
+
export {
|
|
32
|
+
example_default as default
|
|
33
|
+
};
|