Skip to content

Commit 01e215c

Browse files
committed
Send UTM params when logging into dashboard
1 parent 040e478 commit 01e215c

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,18 @@ 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(
53+
(acc, cookie) => {
54+
acc[cookie.name] = cookie.value;
55+
return acc;
56+
},
57+
{} as Record<string, string>,
58+
);
4759

4860
// forward the request to the API server
4961
const res = await fetch(`${API_SERVER_URL}/v2/siwe/login`, {
@@ -53,7 +65,7 @@ export async function doLogin(payload: VerifyLoginPayloadParams) {
5365
"x-service-api-key": THIRDWEB_API_SECRET,
5466
},
5567
// set the createAccount flag to true to create a new account if it does not exist
56-
body: JSON.stringify({ ...payload, createAccount: true }),
68+
body: JSON.stringify({ ...payload, createAccount: true, utm: utmCookies }),
5769
});
5870

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

apps/dashboard/src/middleware.ts

Lines changed: 29 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";
@@ -22,12 +23,38 @@ export const config = {
2223
};
2324

2425
export async function middleware(request: NextRequest) {
26+
const defaultResponse = NextResponse.next();
2527
const { pathname } = request.nextUrl;
2628
const activeAccount = request.cookies.get(COOKIE_ACTIVE_ACCOUNT)?.value;
2729
const authCookie = activeAccount
2830
? request.cookies.get(COOKIE_PREFIX_TOKEN + getAddress(activeAccount))
2931
: null;
3032

33+
// utm collection
34+
// NOTE: this is not working for pages with rewrites in next.config.js - (framer pages)
35+
// if user is already signed in - don't bother capturing utm params
36+
if (!authCookie) {
37+
const searchParamsEntires = request.nextUrl.searchParams.entries();
38+
const utmParams: Map<string, string> = new Map();
39+
for (const param of searchParamsEntires) {
40+
if (param[0].startsWith("utm_")) {
41+
utmParams.set(param[0], param[1]);
42+
}
43+
}
44+
45+
// if we have utm params, set them as cookies
46+
if (utmParams.size) {
47+
const currentCookies = await cookies();
48+
for (const [key, value] of utmParams.entries()) {
49+
// if its already set - don't set it again
50+
if (!currentCookies.get(key)) {
51+
currentCookies.set(key, value);
52+
defaultResponse.cookies.set(key, value);
53+
}
54+
}
55+
}
56+
}
57+
3158
// logged in paths
3259
if (isLoginRequired(pathname)) {
3360
// check if the user is logged in (has a valid auth cookie)
@@ -117,6 +144,8 @@ export async function middleware(request: NextRequest) {
117144
}
118145
// END /<address>/... case
119146
// all other cases are handled by the file system router so we just fall through
147+
148+
return defaultResponse;
120149
}
121150

122151
function isPossibleEVMAddress(address: string) {

0 commit comments

Comments
 (0)