Google Tag Manager
You can use Google Tag Manager to add the Swetrix tracking script to your website.
Installation (Swetrix Loader tag)
Sign in to your Google Tag Manager account, click on Tags and then New.
Under the Tag Configuration section, click on "Choose a tag type to begin set-up...".
Select Custom HTML
In the HTML input field, paste the following code:
<script src="https://swetrix.org/swetrix.js" defer></script>
This code is responsible for loading the Swetrix script into your website. We will configure the tracking script later in this tutorial.
Under Triggering section, click on "Choose a trigger to mage this tag fire..." and select All pages (or alternatively, you can filter the pages you want Swetrix to be enabled on as well).
Give your tag a name, change "Untitled Tag" to something like Swetrix Loader. The result should look like this:
Click on the Save button.
Set up (Swetrix Configuration tag)
After you verified that the loader tag looks fine, you will need to create another tag. The purpose of the second tag is to configure the tracking script you installed before. If you do not create the second tag, Swetrix will not track your analytics events.
Go to the Tags again and click on New.
Under the Tag Configuration section, click on "Choose a tag type to begin set-up...".
Select Custom HTML.
In the HTML input field, paste the following code:
<script>
if (document.readyState !== 'loading') {
swetrixInit();
} else {
document.addEventListener('DOMContentLoaded', swetrixInit);
}
function swetrixInit() {
// Here you can configure Swetrix for your needs
swetrix.init('YOUR_PROJECT_ID')
swetrix.trackViews()
}
</script>
Don't forget to replace YOUR_PROJECT_ID
with your actual Project ID you can find in the Dashboard, otherwise tracking won't work.
You can also refer to our detailed tracking script reference for more advanced configuration.
Give your tag a name, change "Untitled Tag" to something like Swetrix Configuration.
Under the Tag Configuration section, click on Advanced Settings and then Tag Sequencing. Click on the Fire a tag before Swetrix Configuration fires and select Swetrix Loader (the tag we've set up before).
Click on "Don't fire Swetrix Configuration if Swetrix Loader fails or is paused". Setting this up will make sure that Swetrix config will execute after the tracking script has loaded.
Under Triggering section, click on "Choose a trigger to mage this tag fire..." and select All pages.
Click on the Save button.
Click on the Submit button on the top right corner of the page.
Then click the Publish button to apply newly created tags.
Check your installation
After setting up the Swetrix analytics tag, go to your website and visit some pages.
Within a minute you should be able to see new pageviews being added to your project's dahsboard.
Google Tag Manager adds extra complexity to your website. The Swetrix tracking script is lightweight in itself and it's recommended to use it directly. Also, GTM is blocked by many adblockers, so using it may be less reliable.