Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextJS Amplitude Cookie Example #38

Open
smol-honk opened this issue Jan 18, 2024 · 1 comment
Open

NextJS Amplitude Cookie Example #38

smol-honk opened this issue Jan 18, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@smol-honk
Copy link

Summary

Noticed that there is a section on accessing amplitude cookies in the docs: https://www.docs.developers.amplitude.com/experiment/sdks/nodejs-sdk/#access-amplitude-cookies

Do we need to add that to the examples NextJS app or does the sdk handle that automatically?

Motivations

Would like to know if it's suggested to add middleware to NextJS apps using SSR for experiments.

@smol-honk smol-honk added the enhancement New feature or request label Jan 18, 2024
@cjcheshire
Copy link

cjcheshire commented Dec 13, 2024

I was trying to do something like this

import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
import { generate } from 'src/middleware/amplitude-utils';

export function middleware(request: NextRequest) {
  const ampValue = request.cookies.get(amplitudeCookieName)?.value;

  const response = NextResponse.next();

  if (!ampValue) {
    const deviceId = randomString(32);
    const newAmpValue = AmplitudeCookie.generate(deviceId, true);
    response.cookies.set(amplitudeCookieName, newAmpValue);
  }

  return response;
}

Sadly nextjs does not like this Error: The edge runtime does not support Node.js 'https' module

I would love to lean on the generate method from the module just in case the api changes. The only workaround I could have is to copy the method directly from the module.

We have multiple apps, so this ideally would be moved to a lambda to share across all of them, again concerns around ensuring consistency with the cookie shape.

@smol-honk - any thoughts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants