question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using Amplitude with SSR (React)

See original GitHub issue

Hi,

I am trying to migrate a codebase from client-side to server-side, and am using Amplitude like so:

import amplitude from 'amplitude-js/amplitude';

It is bundled by Webpack. On the server-side, I have this error:

document is not defined, l.3020, called by global scope block l.3007

I’m a bit lost on this, as there is browser-specific code in global scope and therefore it can’t run on server…

Any insights?

Thanks in advance

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
adrienharnaycommented, Sep 13, 2017

Hi,

Thanks for the answer. Indeed, I worked around it by doing:

if (typeof window !== 'undefined') {
  amplitude = require('amplitude-js/amplitude');
}
2reactions
Vadorequestcommented, Mar 3, 2020

For anyone interested by a real use case with Amplitude and Next.js 9 (universal app), check out https://github.com/UnlyEd/next-right-now

Interesting parts are:

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Amplitude with SSR (React) · Issue #110 - GitHub
Hi, I am trying to migrate a codebase from client-side to server-side, and am using Amplitude like so: import amplitude from ...
Read more >
Server-side Rendering - Amplitude Developer Center
Use the Amplitude Experiment JavaScript Server SDK and JavaScript Client SDK together to create a seamless server-side rendered experience.
Read more >
How to use Amplitude - Next Right Now
Amplitude is used to collect usage metrics (analytics) of the application. Amplitude is used only on the frontend part of the application. It...
Read more >
How to use Server Side Rendering with React SDK?
Question React SDK doc states it works only on client side (browser). Is there a way to use it for SSR (Server Side...
Read more >
How do integrate amplitude analytics with a React app?-Reactjs
You'll be able to turn on Amplitude via our dashboard, and have access to 250+ other destinations. Here is an example of using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found