Skip to content

Commit

Permalink
feat: integrate posthog for analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
arpitdalal committed Apr 22, 2024
1 parent 1572cda commit 1192651
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
SENTRY_DSN=YOUR_SENTRY_DSN
SENTRY_DSN=YOUR_SENTRY_DSN
POSTHOG_API_KEY=YOUR_POSTHOG_API_KEY
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import "dotenv/config";
import express from "express";
import * as Sentry from "@sentry/node";
import { PostHog } from "posthog-node";

if (!process.env.SENTRY_DSN) {
throw new Error("SENTRY_DSN is not defined in the environment variables.");
}
if (!process.env.POSTHOG_API_KEY) {
throw new Error(
"POSTHOG_API_KEY is not defined in the environment variables."
);
}

const posthogClient = new PostHog(process.env.POSTHOG_API_KEY, {
host: "https://us.i.posthog.com",
});

const MODE = process.env.NODE_ENV;

const app = express();
Expand All @@ -29,6 +40,21 @@ Sentry.init({
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());

app.use((req, _, next) => {
posthogClient.capture({
distinctId: req.ip,
event: "pageview",
properties: {
path: req.path,
url: req.originalUrl,
method: req.method,
headers: req.headers,
},
});

next();
});

const URLS = {
website: "https://arpitdalal.dev/",
blog: "https://blog.arpitdalal.dev/",
Expand Down
91 changes: 90 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"dependencies": {
"@sentry/node": "^7.111.0",
"dotenv": "^16.4.5",
"express": "^4.19.2"
"express": "^4.19.2",
"posthog-node": "^4.0.0"
},
"devDependencies": {
"@flydotio/dockerfile": "^0.5.5",
Expand Down

0 comments on commit 1192651

Please sign in to comment.