-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
114 additions
and
2 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 |
---|---|---|
@@ -1,4 +1,23 @@ | ||
import { usePlausible as _usePlausible } from "next-plausible"; | ||
import { useOpenPanel } from "@openpanel/nextjs"; | ||
import { usePlausible } from "next-plausible"; | ||
import { useCallback } from "react"; | ||
import type { EventOptionsTuple } from "server/common/tracking"; | ||
import type { PlausibleEvents } from "types/plausible-events"; | ||
|
||
export const useTracking = _usePlausible<PlausibleEvents>; | ||
export const useTracking = <const N extends keyof PlausibleEvents>() => { | ||
const trackPlausible = usePlausible<PlausibleEvents>(); | ||
const { track: trackOpenPanel } = useOpenPanel(); | ||
|
||
return useCallback( | ||
( | ||
eventName: N, | ||
...rest: PlausibleEvents[N] extends never | ||
? [] | ||
: EventOptionsTuple<PlausibleEvents[N]> | ||
) => { | ||
trackPlausible(eventName, ...rest); | ||
trackOpenPanel(eventName, ...rest); | ||
}, | ||
[trackPlausible, trackOpenPanel] | ||
); | ||
}; |
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,45 @@ | ||
import { OpenPanel } from "@openpanel/nextjs"; | ||
import { env } from "env/server.mjs"; | ||
import type { ServerEvents } from "types/plausible-events"; | ||
|
||
export type EventProps = Record<string, unknown> | never; | ||
export type EventOptionsTuple<P extends EventProps> = P extends never | ||
? [Omit<EventOptions<P>, "props">?] | ||
: [EventOptions<P>]; | ||
|
||
export type EventOptions<P extends EventProps> = { | ||
props: P; | ||
revenue?: { | ||
currency: string; | ||
amount: number; | ||
}; | ||
u?: string; | ||
callback?: VoidFunction; | ||
}; | ||
|
||
class TrackingService { | ||
private instance: OpenPanel | null = null; | ||
|
||
constructor() { | ||
if (env.OPENPANEL_CLIENT_SECRET && env.NEXT_PUBLIC_OPENPANEL_CLIENT_ID) { | ||
this.instance = new OpenPanel({ | ||
clientId: "your-client-id", | ||
clientSecret: "your-client-secret", | ||
}); | ||
} | ||
} | ||
public trackEvent<const N extends keyof ServerEvents>( | ||
eventName: N, | ||
...eventProperties: ServerEvents[N] extends never | ||
? [] | ||
: [EventOptionsTuple<ServerEvents[N]>[0]["props"]] | ||
) { | ||
if (env.NODE_ENV === "development") { | ||
console.log(`Tracking event: ${eventName}`, eventProperties[0]); | ||
return; | ||
} | ||
this.instance?.track(eventName, eventProperties[0]); | ||
} | ||
} | ||
|
||
export const serverTrackingService = new TrackingService(); |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.