-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
5e0b26a
commit b5bdc5d
Showing
7 changed files
with
149 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
export { GET, POST } from 'src/auth' | ||
|
||
// export async function GET(request: NextRequest) { | ||
// console.log(request.nextUrl.pathname) | ||
// const res = await authGet(request) | ||
// | ||
// if (!request.nextUrl.pathname.startsWith('/api/auth/callback/')) return res | ||
// | ||
// console.log(res) | ||
// return res | ||
// } |
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,5 @@ | ||
export type TrackableEvents = { | ||
'Opened a modal': { Extra?: Record<string, unknown>; Modal: string } | ||
'Signed In': { Provider?: string; 'First Time': boolean } | ||
'Viewed a page': { Path: string; Ref?: string | null } | ||
} |
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,25 @@ | ||
import { User } from 'next-auth' | ||
|
||
import { token, trackingEnabled } from './utils' | ||
|
||
export const identify = async ({ id, name, email, image }: User) => { | ||
if (!trackingEnabled) return | ||
if (!token) return | ||
|
||
const body = { | ||
$token: token, | ||
$distinct_id: id, | ||
$ip: 0, | ||
$set: { | ||
$name: name, | ||
$email: email, | ||
$avatar: image | ||
} | ||
} | ||
|
||
await fetch(`https://api.mixpanel.com/engage#profile-set?ip=0`, { | ||
method: 'POST', | ||
body: JSON.stringify([body]), | ||
headers: { 'content-type': 'application/json', accept: 'text/plain' } | ||
}) | ||
} |
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,81 @@ | ||
'use server' | ||
|
||
import { cookies, headers } from 'next/headers' | ||
import { NextRequest, NextResponse, userAgent } from 'next/server' | ||
|
||
import { ResponseCookies } from 'next/dist/compiled/@edge-runtime/cookies' | ||
import { TrackableEvents } from './events' | ||
import { token, trackingEnabled } from './utils' | ||
|
||
type CommonEventProperties = { | ||
$browser?: string | ||
$device?: string | ||
$os?: string | ||
$referrer?: string | null | ||
ip?: string | null | ||
} | ||
|
||
export async function track<T extends keyof TrackableEvents>( | ||
userId: string | undefined, | ||
event: T, | ||
properties: TrackableEvents[T], | ||
req?: NextRequest, | ||
res?: NextResponse | ||
) { | ||
if (!trackingEnabled) return | ||
if (!token) return | ||
|
||
const distinct_id = | ||
userId || | ||
req?.cookies.get('baf-session-id')?.value || | ||
generateAnonymousId(res?.cookies) | ||
|
||
const body = { | ||
event, | ||
properties: { | ||
...getCommonProperties(req), | ||
...properties, | ||
distinct_id, | ||
token | ||
} | ||
} | ||
|
||
await fetch(`https://api.mixpanel.com/track?ip=0`, { | ||
method: 'POST', | ||
body: JSON.stringify([body]), | ||
headers: { 'content-type': 'application/json', accept: 'text/plain' } | ||
}) | ||
} | ||
|
||
function getCommonProperties(req?: NextRequest): CommonEventProperties { | ||
try { | ||
const h = req?.headers || headers() | ||
const ua = userAgent({ headers: h }) | ||
|
||
return { | ||
$browser: ua.browser.name, | ||
$device: ua.device.type, | ||
$os: ua.os.name, | ||
$referrer: h.get('referer'), | ||
ip: | ||
h.get('cf-connecting-ip') || | ||
req?.ip || | ||
h.get('x-real-ip') || | ||
h.get('x-forwarded-for') | ||
} | ||
} catch (e) { | ||
return {} | ||
} | ||
} | ||
|
||
function generateAnonymousId(c: ResponseCookies = cookies()) { | ||
const anonId = crypto.randomUUID() | ||
c.set({ | ||
name: 'baf-session-id', | ||
value: anonId, | ||
maxAge: 60 * 60 * 24 * 365, | ||
secure: true, | ||
httpOnly: true | ||
}) | ||
return anonId | ||
} |
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,5 @@ | ||
export const trackingEnabled = | ||
process.env.NODE_ENV !== 'development' || | ||
process.env.ENABLE_TRACKING_IN_DEV === 'true' | ||
|
||
export const token = process.env.MIXPANEL_TOKEN |
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