Files
firecrawl/apps/api/src/services/posthog.ts
T

28 lines
851 B
TypeScript
Raw Normal View History

2024-12-11 19:46:11 -03:00
import { PostHog } from "posthog-node";
2024-05-02 15:30:22 -04:00
import "dotenv/config";
2024-12-11 19:46:11 -03:00
import { logger } from "../../src/lib/logger";
2024-05-02 15:30:22 -04:00
2024-11-07 20:57:33 +01:00
export default function PostHogClient(apiKey: string) {
const posthogClient = new PostHog(apiKey, {
2024-05-02 15:30:22 -04:00
host: process.env.POSTHOG_HOST,
flushAt: 1,
2024-12-11 19:51:08 -03:00
flushInterval: 0,
2024-05-02 15:30:22 -04:00
});
return posthogClient;
}
class MockPostHog {
capture() {}
}
// Using the actual PostHog class if POSTHOG_API_KEY exists, otherwise using the mock class
// Additionally, print a warning to the terminal if POSTHOG_API_KEY is not provided
export const posthog = process.env.POSTHOG_API_KEY
2024-11-07 20:57:33 +01:00
? PostHogClient(process.env.POSTHOG_API_KEY)
2024-05-02 15:30:22 -04:00
: (() => {
2024-11-07 20:57:33 +01:00
logger.warn(
2024-12-11 19:51:08 -03:00
"POSTHOG_API_KEY is not provided - your events will not be logged. Using MockPostHog as a fallback. See posthog.ts for more.",
2024-05-02 15:30:22 -04:00
);
return new MockPostHog();
2024-12-11 19:46:11 -03:00
})();