-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tracking): add custom events with distinct id (#485)
* add custom events with distinct posthog * add step.meta.customEventName * unify posthogClient * refactor flowController.getMeta
- Loading branch information
Showing
6 changed files
with
63 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { hasTrackingConsent } from "./gdprCookie.server"; | ||
import { config } from "../env/web"; | ||
import { parse } from "cookie"; | ||
import { getPosthogClient } from "./posthogClient.server"; | ||
|
||
function idFromCookie(request: Request) { | ||
// Note: can't use cookie.parse(): https://github.com/remix-run/remix/discussions/5198 | ||
// Returns ENVIRONMENT if posthog's distinct_id can't be extracted | ||
const { POSTHOG_API_KEY, ENVIRONMENT } = config(); | ||
if (!POSTHOG_API_KEY) return ENVIRONMENT; | ||
const parsedCookie = parse(request.headers.get("Cookie") ?? ""); | ||
const phCookieString = parsedCookie[`ph_${POSTHOG_API_KEY}_posthog`] ?? "{}"; | ||
const phCookieObject = JSON.parse(phCookieString) as Record<string, string>; | ||
return phCookieObject["distinct_id"] ?? ENVIRONMENT; | ||
} | ||
|
||
export async function sendCustomEvent( | ||
eventName: string, | ||
context: Record<string, any>, | ||
request: Request, | ||
) { | ||
if (!(await hasTrackingConsent({ request }))) return; | ||
|
||
getPosthogClient()?.capture({ | ||
distinctId: idFromCookie(request), | ||
event: eventName, | ||
properties: { | ||
// eslint-disable-next-line camelcase | ||
$current_url: new URL(request.url).pathname, | ||
...context, | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { PostHog } from "posthog-node"; | ||
import { config } from "../env/web"; | ||
|
||
const { POSTHOG_API_KEY, POSTHOG_API_HOST } = config(); | ||
const posthogClient = POSTHOG_API_KEY | ||
? new PostHog(POSTHOG_API_KEY, { host: POSTHOG_API_HOST }) | ||
: undefined; | ||
|
||
export const getPosthogClient = () => posthogClient; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters