beyonk-group / svelte-google-analytics

Google Analytics component for Svelte
77 stars 12 forks source link

<GoogleAnalytics> is not a valid SSR component #16

Open mattpilott opened 2 years ago

mattpilott commented 2 years ago

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

nstuyvesant commented 2 years ago

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');
}
antony commented 2 years ago

Interesting - it may need updating for SvelteKit.

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