-
Notifications
You must be signed in to change notification settings - Fork 20
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
Using SDK with Next.js and making Amplitude global #24
Comments
Hi @bobthebuilder1900 ! I think the pattern you have with having is exported seems solid. I was wondering what issues you were encountering with the JS SDK for server side-rendered react? If the JS SDK also works, you could also consider using our react sdk which wraps the JS SDK and provides useful helper components. You could also consider copying its pattern for the Node SDK and inserting the initialized node client into a top component of the React tree into the context and using context providers as necessary to fetch it within your components. |
The JS SDK does not work with server side rendered applications, as it relies heavily on accessing window. Looking at the events that the node sdk sends, I am not sure I can use it as it sends the platform as Node.js and I won't be able to get devices or "Real" platforms such as Windows, Mac and Linux or browser usage. The react amplitude sdk first paragraph says "react-amplitude is not an officially supported Amplitude SDK or library." |
@bobthebuilder1900 Apologies for the delayed response.
We don't support it officially, but I think the pattern it has (and the community built
Unfortunately this is true, and something we're looking into. It would however align better to your use case as it provides a better API for the web. Things you could consider as workarounds while we work to solve this:
|
Has this been solved yet? I'm running into the same problem using amplitude JS SDK on a NextJS project. |
@hudsonhyunlim As a workaround I conditionally import and init amplitude only on the browser side like this:
Then in my
Then also wrap all other amplitude functions like the init:
|
I personally use react-amplitude for Next.js pages, and @amplitude/node for APIs. |
It works on next js. But somehow I get the following error
After reload automatically it works. Such a weird thing. Is anyone the same as me? |
@MohamedJakkariya
|
@desmondmc Thanks for your reply. By the way, I found my issue. I just try to access the amplitude before loading it into the window. I realized. So I fixed it. |
Don't calls hooks inside conditions. I think it should be like this. useEffect(() => {
if (router.isReady) {
initAmplitude();
}
}, [router.isReady]); See also |
Summary
Hi, I am looking into implementing this SDK on my Next.js project as it is server side rendered.
The documentation isn't clear, so I am unsure on how to actually get started, on my app.tsx I want to init amplitude and have it available on all pages and components.
With the above, it is not initialised, as I do not use client for anything, can I simply do
Amplitude.init(process.env.AMPLITUDE_API_KEY)
if not, how can I make this global?With the client side SDK, I could init on the index.js and use amplitude.getInstance() to log an event
My question is, do I need to create a utils file that does the following
and import it on every page, then log events?
How can I make this globally available within my app so that way data can be logged?
The text was updated successfully, but these errors were encountered: