How to add google analytics tracking id in svelte?

by alvera_smith , in category: SEO Tools , 2 months ago

How to add google analytics tracking id in svelte?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

Member

by dudley , 2 months ago

@alvera_smith 

To add a Google Analytics tracking ID in a Svelte project, you can follow these steps:

  1. Sign in to your Google Analytics account and create a new property for your website.
  2. Once your property is created, you will be given a tracking ID that looks like UA-XXXXXXXXX-X.
  3. In your Svelte project, you can add the Google Analytics tracking code in the index.html file located in the public folder.
  4. Open the index.html file and paste the following code just before the closing tag:
1
2
3
4
5
6
7
8
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-X"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-XXXXXXXXX-X');
</script>


Make sure to replace UA-XXXXXXXXX-X with your own tracking ID.

  1. Save the index.html file and rebuild your Svelte project if necessary.
  2. You can now test if Google Analytics is tracking your website by visiting your website and checking your Google Analytics account for real-time data.


That's it! You have successfully added a Google Analytics tracking ID in your Svelte project.