I’m pretty stuck on something that seems like it s...
# support
d
I’m pretty stuck on something that seems like it should be easy: I’m trying to use Rudderstack in a next.js app and getting an error because window isn’t defined. I followed the next instructions in the docs, but they include window. Any idea how to solve it? Running latest version of next
h
The example code uses useEffect to init the code that uses window - useEffect will only fire clientside
image.png
d
Right, but then I have to call page and track with window.rudder
IMG_1387.png
In this example, for instance, how would I just call page on load? This is calling it onClick
h
call it after the init
d
right, but it doesn’t have rudderanalytics on window. using TS and doing exactly this guide: https://www.rudderstack.com/docs/user-guides/how-to-guides/rudderstack-jamstack-integration/rudderstack-nextjs-integration/
h
ah sorry - yeah - perhaps pass the page call func into init and invoke in rudderanalytics.ready
not clear from that example when search is called but I am assuming long after rudder has loaded onto the window object
d
nothing, i’m stumped. maybe i’m doing something stupid, but i’m following the docs and they’re fundamentally not working.
h
can you see the rudder js being called in the network tab
d
no, just error screens
h
as in next errors or?? what errors in the console?
d
CleanShot 2023-03-15 at 18.28.26@2x.png
h
yeah so that looks like a server side next error
d
right, i agree. but that’s just trying to make the code from the site work.
h
if you can paste what you have
d
sure, it doesn’t work on many levels … first i needed to define window globally … so this
Copy code
declare global {
  interface Window {
    rudderanalytics: any
  }
}

window.rudderanalytics = window.rudderanalytics || {}

export async function rudderInitialize() {
  window.rudderanalytics = await import('rudder-sdk-js')
  rudderanalytics.load(
    process.env.NEXT_PUBLIC_RUDDERSTACK_KEY ?? '',
    process.env.NEXT_PUBLIC_RUDDERSTACK_DATA_PLANE_URL ?? '',
    {
      integrations: { All: true }, // load call options
    }
  )

  rudderanalytics.ready(() => {
    console.log('All set!')
  })
}
but that doesn’t work, because it doesn’t know rudderanalytics, so i tried this:
Copy code
import * as rudderanalytics from 'rudder-sdk-js'

declare global {
  interface Window {
    rudderanalytics: any
  }
}

window.rudderanalytics = window.rudderanalytics || {}

export async function rudderInitialize() {
  window.rudderanalytics = rudderanalytics
  rudderanalytics.load(
    process.env.NEXT_PUBLIC_RUDDERSTACK_KEY ?? '',
    process.env.NEXT_PUBLIC_RUDDERSTACK_DATA_PLANE_URL ?? '',
    {
      integrations: { All: true }, // load call options
    }
  )

  rudderanalytics.ready(() => {
    console.log('All set!')
  })
}
in index.ts:
Copy code
useEffect(() => {
    rudderInitialize()
  }, [])
h
can you try adding typeof window === "undefined"
before the window.rudderanalytics line
sorry typescript/next (or js) not really my thing:)
d
it’s ok, i appreciate it, but it’s the same error.
h
Hi @dry-artist-19502, Have you checked the sample app? https://github.com/rudderlabs/rudder-analytics-next/tree/main/sample-next-app-npm
d
Yes, that’s the same as the docs with the same window error.
I figured out how to get it working last night, but it requires a fundamentally different approach than what’s shared in docs and repo.
h
Hi @dry-artist-19502, Please let us know approach that solved your problem.
d
A bunch of stuff, I needed to use handle route change, dynamic imports and other stuff. I outlined a bit here and if I have some time I will try and share, but it was nothing near what’s in the docs https://brxnd.substack.com/p/i-know-i-know-more-content-about
🙌 1