svelte-google-analytics icon indicating copy to clipboard operation
svelte-google-analytics copied to clipboard

<GoogleAnalytics> is not a valid SSR component

Open mattpilott opened this issue 2 years ago • 2 comments

Seeing this in sveltekit unsure what i need to adjust

<GoogleAnalytics> is not a valid SSR component. You may need to review your build config to ensure that dependencies are compiled, rather than imported as pre-compiled modules

mattpilott avatar Feb 06 '22 13:02 mattpilott

Guessing this component doesn't expect to be rendered server-side where there is no browser.

I decided to add this to my /src/routes/__layout.svelte instead...

onMount(async() => {
  const googleAnalytics = document.createElement('script')
  googleAnalytics.src = 'https://www.googletagmanager.com/gtag/js?id=YOUR-TRACKER-ID'
  document.body.append(googleAnalytics)

 window.dataLayer = window.dataLayer || [];
  function gtag(){
    window.dataLayer.push(arguments)
  }
  gtag('js', new Date());
  gtag('config', 'YOUR-TRACKER-ID');
}

nstuyvesant avatar Apr 04 '22 04:04 nstuyvesant

Interesting - it may need updating for SvelteKit.

It doesn't render server-side - it does exactly what you've done above there :)

antony avatar Apr 27 '22 15:04 antony