π 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
Refresh Google Tag Manager docs
Blue Mouseedca85ed9a24
diff --git a/docs/docs/integrations/google-tag-manager.md b/docs/docs/integrations/google-tag-manager.mdindex fe2959c06..abc99f21a 100644--- a/docs/docs/integrations/google-tag-manager.md+++ b/docs/docs/integrations/google-tag-manager.md@@ -5,9 +5,17 @@ slug: /gtm-integrationimport useBaseUrl from '@docusaurus/useBaseUrl';-You can use Google Tag Manager to add the Swetrix tracking script to your website.+Google Tag Manager (GTM) allows you to add tracking scripts to your website without modifying your codebase directly. This guide walks you through integrating Swetrix via GTM.-## Installation (Swetrix Loader tag)+:::note+GTM adds extra complexity and is blocked by many adblockers, which may reduce tracking reliability. The Swetrix tracking script is lightweight and we recommend [adding it directly](/install-script) when possible. You can also [set up a proxy](/adblockers/deal-with-adblockers) to mitigate adblocker interference.+:::++## Why the snippet looks different++Normally you'd add Swetrix to your site with a simple `<script>` tag. However, GTM injects Custom HTML tags **dynamically**, which means the `defer` attribute on a script tag is ignored by browsers β the script loads asynchronously instead. To ensure Swetrix is fully loaded before we initialise it, we use a dynamic script element with an `onload` handler.++## Installation1. Sign in to your Google Tag Manager account, click on **Tags** and then **New**.<img alt="Creating a new tag" src={useBaseUrl('img/integrations/google/gt-1.png')} />@@ -15,77 +23,164 @@ You can use Google Tag Manager to add the Swetrix tracking script to your websit2. Under the **Tag Configuration** section, click on "Choose a tag type to begin set-up...".<img alt="Tag Configuration" src={useBaseUrl('img/integrations/google/gt-2.png')} />-3. Select **Custom HTML**+3. Select **Custom HTML**.<img alt="Custom HTML" src={useBaseUrl('img/integrations/google/gt-3.png')} />4. In the HTML input field, paste the following code:```html-<script src="https://swetrix.org/swetrix.js" defer></script>+<script>+ (function () {+ var el = document.createElement('script')+ el.src = 'https://swetrix.org/swetrix.js'+ el.onload = function () {+ swetrix.init('YOUR_PROJECT_ID')+ swetrix.trackViews()+ }+ document.head.appendChild(el)+ })()+</script>```-This code is responsible for loading the Swetrix script into your website. We will configure the tracking script later in this tutorial.+:::caution+Replace `YOUR_PROJECT_ID` with your actual Project ID from the Swetrix dashboard, otherwise tracking won't work.+You can also refer to the [tracking script reference](/swetrix-js-reference) for more advanced configuration options (e.g. `hash`, `search`, `respectDNT`).+:::-5. 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).+5. Under **Triggering**, click on "Choose a trigger to make this tag fire..." and select **All Pages** (or set up a more specific trigger to limit which pages Swetrix runs on).-6. Give your tag a name, change "Untitled Tag" to something like **Swetrix Loader**. The result should look like this:+6. Give your tag a name β change "Untitled Tag" to something like **Swetrix Analytics**. The result should look similar to this:<img alt="Final result" src={useBaseUrl('img/integrations/google/gt-4.png')} />-7. Click on the **Save** button.+7. Click **Save**. A list of tags will be displayed - you should see your new Swetrix tag in the list. Click the **Submit** button in the top right corner.+ <img alt="Tags list" src={useBaseUrl('img/integrations/google/gt-5.png')} />-## Set up (Swetrix Configuration tag)+8. Add a description for your changes in the pop-up window and click **Publish**.+ <img alt="Publish" src={useBaseUrl('img/integrations/google/gt-6.png')} />-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.+## Check your installation-1. Go to the **Tags** again and click on **New**.-2. Under the **Tag Configuration** section, click on "Choose a tag type to begin set-up...".+After publishing, visit your website and browse a few pages. Within a minute you should see new pageviews appearing in your Swetrix dashboard.-3. Select **Custom HTML**.+If you don't see any pageviews - make sure that your adblocker is not blocking the Google Tag Manager or Swetrix scripts.-4. In the HTML input field, paste the following code:+## Custom event tracking++Once Swetrix is loaded through GTM, you can track custom events using the `swetrix.track()` function. Refer to the [tracking script reference](/swetrix-js-reference#track) for full API details.++### Method 1: Custom HTML tag in GTM++Create an additional **Custom HTML** tag in GTM and set a trigger for the interaction you want to track (e.g. a click on a specific element, a form submission, or a scroll depth threshold).```html<script>- if (document.readyState !== 'loading') {- swetrixInit()- } else {- document.addEventListener('DOMContentLoaded', swetrixInit)+ if (typeof swetrix !== 'undefined') {+ swetrix.track({+ ev: 'signup_cta_clicked',+ meta: {+ page: window.location.pathname+ }+ })}+</script>+```- function swetrixInit() {- // Here you can configure Swetrix for your needs- swetrix.init('YOUR_PROJECT_ID')- swetrix.trackViews()+### Method 2: Using GTM built-in variables++GTM provides built-in variables like `{{Page URL}}`, `{{Click Text}}`, and `{{Referrer}}` that you can use directly in your tracking tags. This lets you send contextual data to Swetrix without writing custom selectors.++Create a **Custom HTML** tag and reference the variables:++```html+<script>+ if (typeof swetrix !== 'undefined') {+ swetrix.track({+ ev: 'element_clicked',+ meta: {+ page: '{{Page URL}}',+ referrer: '{{Referrer}}',+ click_text: '{{Click Text}}',+ click_url: '{{Click URL}}'+ }+ })}</script>```-:::caution-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](/swetrix-js-reference) for more advanced configuration.-:::+Then attach a trigger for the specific interaction β for example, **Click - All Elements** filtered to a particular CSS selector.-5. Give your tag a name, change "Untitled Tag" to something like **Swetrix Configuration**.+### Method 3: From your website's JavaScript-6. 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).- <img alt="Configuration tag setup" src={useBaseUrl('img/integrations/google/gt-5.png')} />+Since Swetrix is available globally once loaded via GTM, you can also call `swetrix.track()` directly from your website code:-7. 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.+```javascript+document.getElementById('cta-button')?.addEventListener('click', function () {+ if (typeof swetrix !== 'undefined') {+ swetrix.track({+ ev: 'cta_clicked',+ meta: {+ button_text: this.textContent,+ page: window.location.pathname+ }+ })+ }+})+```-8. Under **Triggering** section, click on "Choose a trigger to mage this tag fire..." and select **All pages**.+### Example events++```javascript+// Form submission+swetrix.track({+ ev: 'form_submitted',+ meta: { form_id: 'contact_form' }+})++// File download+swetrix.track({+ ev: 'file_downloaded',+ meta: { file_name: 'whitepaper.pdf', file_type: 'pdf' }+})++// Video engagement+swetrix.track({+ ev: 'video_played',+ meta: { video_title: 'Product Demo' }+})+```-9. Click on the **Save** button.+:::tip+Event names must contain only English letters, numbers, underscores, and dots, be under 64 characters, and start with a letter. The `meta` object supports up to 20 keys with a combined value length under 1000 characters.+:::-10. Click on the **Submit** button on the top right corner of the page.+## Error tracking-11. Then click the **Publish** button to apply newly created tags.+You can enable automatic client-side error monitoring by adding `swetrix.trackErrors()` to your initialisation snippet:-## Check your installation+```html+<script>+ (function () {+ var el = document.createElement('script')+ el.src = 'https://swetrix.org/swetrix.js'+ el.onload = function () {+ swetrix.init('YOUR_PROJECT_ID')+ swetrix.trackViews()+ swetrix.trackErrors()+ }+ document.head.appendChild(el)+ })()+</script>+```-After setting up the Swetrix analytics tag, go to your website and visit some pages.+This captures unhandled JavaScript errors and sends them to Swetrix automatically. You can also track errors manually from GTM or your website code:-Within a minute you should be able to see new pageviews being added to your project's dahsboard.+```javascript+if (typeof swetrix !== 'undefined') {+ swetrix.trackError({+ name: 'PaymentError',+ message: 'Card declined',+ })+}+```-:::note-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.-:::+Refer to the [tracking script reference](/swetrix-js-reference#trackerrors) for configuration options like `sampleRate` and `callback`.diff --git a/docs/static/img/integrations/google/gt-1.png b/docs/static/img/integrations/google/gt-1.pngindex 4437cf1a0..5e4839260 100644Binary files a/docs/static/img/integrations/google/gt-1.png and b/docs/static/img/integrations/google/gt-1.png differdiff --git a/docs/static/img/integrations/google/gt-2.png b/docs/static/img/integrations/google/gt-2.pngindex 1338862b7..c4328040b 100644Binary files a/docs/static/img/integrations/google/gt-2.png and b/docs/static/img/integrations/google/gt-2.png differdiff --git a/docs/static/img/integrations/google/gt-3.png b/docs/static/img/integrations/google/gt-3.pngindex 37625717e..8c0199294 100644Binary files a/docs/static/img/integrations/google/gt-3.png and b/docs/static/img/integrations/google/gt-3.png differdiff --git a/docs/static/img/integrations/google/gt-4.png b/docs/static/img/integrations/google/gt-4.pngindex 787740126..c2b5d45ee 100644Binary files a/docs/static/img/integrations/google/gt-4.png and b/docs/static/img/integrations/google/gt-4.png differdiff --git a/docs/static/img/integrations/google/gt-5.png b/docs/static/img/integrations/google/gt-5.pngindex c30bc85f1..f5993913f 100644Binary files a/docs/static/img/integrations/google/gt-5.png and b/docs/static/img/integrations/google/gt-5.png differdiff --git a/docs/static/img/integrations/google/gt-6.png b/docs/static/img/integrations/google/gt-6.pngnew file mode 100644index 000000000..5fbb5c219Binary files /dev/null and b/docs/static/img/integrations/google/gt-6.png differ