Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

3.0 KB
Raw Blame
---
title: Add tracking script to your website
slug: /install-script
icon: Code
description: "Install the Swetrix tracking script on your website, configure your project ID, and verify that pageviews appear in your dashboard."
---

To use Swetrix on your website, you need to add a tracking script to your website and there are several ways to do this.

<Callout type="info" title="Using a CMS or site builder?">

If your website runs on WordPress, Wix, Shopify, Webflow, or another CMS/site builder, use a dedicated integration guide instead of editing HTML manually.

See [CMS & website builder integrations](/integrations#cms--website-builders).

</Callout>

<Tabs defaultValue="html">
<TabsList>
  <TabsTrigger value="html"><CodeIcon /> HTML script tag</TabsTrigger>
  <TabsTrigger value="npm"><NpmIcon /> npm package</TabsTrigger>
</TabsList>
<TabsContent value="html">

The easiest way to add the tracking script to your website is to edit the HTML code of your website.
To do this, paste the following code snippet into the Body section (`<body>` tag). You have to place the script within the `<body>...</body>` tags.

```html
<script src="https://swetrix.org/swetrix.js" defer></script>
<script>
  document.addEventListener("DOMContentLoaded", function () {
    swetrix.init("YOUR_PROJECT_ID");
    swetrix.trackViews();
  });
</script>

<noscript>
  <img
    src="https://api.swetrix.com/log/noscript?pid=YOUR_PROJECT_ID"
    alt=""
    referrerpolicy="no-referrer-when-downgrade"
  />
</noscript>
```

<Callout type="warn">
It's important to remember to replace `YOUR_PROJECT_ID` with your actual Project ID, which you can find in the Dashboard, otherwise tracking will not work!

</Callout>

Done. Now you can start using Swetrix on your website.

To record session replays, start the recorder from your site code after initialising Swetrix. The replay recorder is loaded asynchronously only when you preload or start it. See [`startSessionReplay()`](/swetrix-js-reference#startsessionreplay) for setup and privacy options.

</TabsContent>
<TabsContent value="npm">

If you are using a website based on a JavaScript framework such as React or Vue, you can install Swetrix via npm and use it on your website.

<Steps>
<Step>
### Install the package

```bash
npm install swetrix
```

</Step>
<Step>
### Import Swetrix

Import the module into your project's root file (in React this is usually called `index.js` or `App.js`, and these files are located in the `src` folder):

```js
import * as Swetrix from "swetrix";
```

</Step>
<Step>
### Start tracking

After that you can track anything you want. For example, the following code snippet will work just fine:

```js
Swetrix.init("YOUR_PROJECT_ID");
Swetrix.trackViews();
```

To record session replays, call `Swetrix.startSessionReplay()` after `Swetrix.init()`. The npm package dynamically imports the installed replay recorder only when you preload or start it. See [`startSessionReplay()`](/swetrix-js-reference#startsessionreplay) for setup and privacy options.

</Step>
</Steps>

</TabsContent>
</Tabs>