Skip to content

Commit 96d9014

Browse files
committed
Send UTM params when logging into dashboard
1 parent 512eeb5 commit 96d9014

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

apps/dashboard/src/app/login/auth-actions.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ export async function doLogin(payload: VerifyLoginPayloadParams) {
4444
}
4545

4646
const cookieStore = await cookies();
47+
const utmCookies = cookieStore
48+
.getAll()
49+
.filter((cookie) => {
50+
return cookie.name.startsWith("utm_");
51+
})
52+
.reduce((acc, cookie) => {
53+
acc[cookie.name] = cookie.value;
54+
return acc;
55+
}, {});
4756

4857
// forward the request to the API server
4958
const res = await fetch(`${API_SERVER_URL}/v2/siwe/login`, {
@@ -53,7 +62,7 @@ export async function doLogin(payload: VerifyLoginPayloadParams) {
5362
"x-service-api-key": THIRDWEB_API_SECRET,
5463
},
5564
// set the createAccount flag to true to create a new account if it does not exist
56-
body: JSON.stringify({ ...payload, createAccount: true }),
65+
body: JSON.stringify({ ...payload, createAccount: true, utm: utmCookies }),
5766
});
5867

5968
// if the request failed, log the error and throw an error

apps/dashboard/src/middleware.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getTeams } from "@/api/team";
22
import { isLoginRequired } from "@/constants/auth";
33
import { COOKIE_ACTIVE_ACCOUNT, COOKIE_PREFIX_TOKEN } from "@/constants/cookie";
4+
import { cookies } from "next/headers";
45
import { type NextRequest, NextResponse } from "next/server";
56
import { getAddress } from "thirdweb";
67
import { getChainMetadata } from "thirdweb/chains";
@@ -28,6 +29,30 @@ export async function middleware(request: NextRequest) {
2829
? request.cookies.get(COOKIE_PREFIX_TOKEN + getAddress(activeAccount))
2930
: null;
3031

32+
// utm collection
33+
// NOTE: this is not working for pages with rewrites in next.config.js - (framer pages)
34+
// if user is already signed in - don't bother capturing utm params
35+
if (!authCookie) {
36+
const searchParamsEntires = request.nextUrl.searchParams.entries();
37+
const utmParams: Map<string, string> = new Map();
38+
for (const param of searchParamsEntires) {
39+
if (param[0].startsWith("utm_")) {
40+
utmParams.set(param[0], param[1]);
41+
}
42+
}
43+
44+
// if we have utm params, set them as cookies
45+
if (utmParams.size) {
46+
const currentCookies = await cookies();
47+
for (const [key, value] of utmParams.entries()) {
48+
// if its already set - don't set it again
49+
if (!currentCookies.get(key)) {
50+
currentCookies.set(key, value);
51+
}
52+
}
53+
}
54+
}
55+
3156
// logged in paths
3257
if (isLoginRequired(pathname)) {
3358
// check if the user is logged in (has a valid auth cookie)

0 commit comments

Comments
 (0)