π 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
Add ThriveCart and Drupal integrations
Blue Mouse960504696683
diff --git a/docs/docs/integrations/drupal.md b/docs/docs/integrations/drupal.mdnew file mode 100644index 000000000..d0597079c--- /dev/null+++ b/docs/docs/integrations/drupal.md@@ -0,0 +1,196 @@+---+title: Drupal+slug: /drupal-integration+---++Integrate Swetrix with your Drupal site to get privacy-friendly, GDPR-compliant analytics. Works with Drupal 9, 10, and 11.++## Installation++There are two ways to add Swetrix to Drupal: by editing your **theme's Twig template** (recommended if you maintain a custom theme) or by using the **Asset Injector** module (no code editing required).++### Method 1: Theme template (recommended)++This approach adds the tracking script directly to the HTML output through your theme's layout template.++#### 1. Locate the template file++In your active custom theme, find the file responsible for the page's HTML shell:++```+themes/custom/your_theme/templates/layout/html.html.twig+```++If your theme doesn't override this template yet, copy it from Drupal core as a starting point:++```+core/themes/stable9/templates/layout/html.html.twig+```++:::tip+Always use a **custom or sub-theme** for template overrides. Editing a contributed theme directly means your changes will be lost on the next theme update.+:::++#### 2. Add the tracking snippet++Open `html.html.twig` and paste the following just before the closing `</body>` tag:++```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>+```++The template usually ends with something like this:++```twig+ <js-bottom-placeholder token="{{ placeholder_token }}">+ </body>+</html>+```++Place the Swetrix snippet between the `<js-bottom-placeholder>` line and the `</body>` tag.++:::caution+Don't forget to replace `YOUR_PROJECT_ID` with your actual Project ID from the Swetrix dashboard, otherwise tracking won't work.+:::++#### 3. Clear Drupal's cache++Drupal caches rendered templates aggressively, so you need to rebuild the cache before your changes take effect.++**From the admin UI:**+Go to **Administration > Configuration > Development > Performance** (`/admin/config/development/performance`) and click **Clear all caches**.++**From the command line (Drush):**++```bash+drush cr+```++### Method 2: Asset Injector module++If you'd rather not touch theme files, the [Asset Injector](https://www.drupal.org/project/asset_injector) module lets you add JavaScript through the admin UI.++#### 1. Install and enable the module++```bash+composer require drupal/asset_injector+drush en asset_injector -y+drush cr+```++Or download it from [drupal.org/project/asset_injector](https://www.drupal.org/project/asset_injector) and enable it under **Administration > Extend** (`/admin/modules`).++#### 2. Create a JS injector++1. Go to **Administration > Configuration > Development > Asset Injector** (`/admin/config/development/asset-injector`).+2. Click **JS Injector**, then **+ Add JS Injector**.+3. Fill in the following:+ - **Label:** Swetrix Analytics+ - **Code:**++```javascript+(function () {+ var s = document.createElement('script')+ s.src = 'https://swetrix.org/swetrix.js'+ s.defer = true+ s.onload = function () {+ swetrix.init('YOUR_PROJECT_ID')+ swetrix.trackViews()+ }+ document.head.appendChild(s)+})()+```++4. Under **Conditions**, leave the pages field empty so the script runs on every page (or restrict it to specific paths if needed).+5. Click **Save**.++:::caution+Replace `YOUR_PROJECT_ID` with your actual Project ID from the Swetrix dashboard.+:::++#### 3. Clear caches++Clear Drupal's cache as described above so the injected script is served to visitors.++#### Adding the noscript fallback++Asset Injector handles JavaScript only. To include the `<noscript>` image fallback for visitors without JavaScript, add a small `html.html.twig` override or use a module like [Add to Head](https://www.drupal.org/project/add_to_head) to insert the following into your pages:++```html+<noscript>+ <img+ src="https://api.swetrix.com/log/noscript?pid=YOUR_PROJECT_ID"+ alt=""+ referrerpolicy="no-referrer-when-downgrade"+ />+</noscript>+```++This step is optional β the vast majority of visitors will have JavaScript enabled.++## Tracking custom events++Once basic page tracking is running, you can use `swetrix.track()` to record interactions like form submissions, button clicks, or downloads. Refer to the [tracking script reference](/swetrix-js-reference) for the full API.++Example β tracking a Webform submission:++```html+<script>+ document.addEventListener('DOMContentLoaded', function () {+ var form = document.querySelector('.webform-submission-form')+ if (!form || typeof swetrix === 'undefined') return++ form.addEventListener('submit', function () {+ swetrix.track({ ev: 'webform_submitted' })+ })+ })+</script>+```++## Troubleshooting++### Script not appearing on the site++- If you used the theme template method, make sure you edited the correct theme β check which theme is active under **Administration > Appearance**.+- Clear all Drupal caches after every change.+- View the page source in your browser and search for `swetrix` to confirm the snippet is present.++### Twig template not picked up++Drupal uses a strict template naming and directory convention. If your template override isn't loading:++- Verify the file is in `templates/layout/` within your custom theme directory.+- Ensure Twig debugging is enabled during development so you can see which templates are being used. In `sites/default/services.yml`, set `twig.config.debug: true`, then clear caches.++### Caching and CDN++If your site uses Drupal's Internal Page Cache, Varnish, or a CDN:++- Clear those caches as well after adding the snippet.+- The Swetrix script loads client-side, so page caching works fine once the HTML contains the tracking snippet.++### Content Security Policy (CSP)++If your site sends a strict Content Security Policy header, you'll need to allow the Swetrix domains:++- **script-src:** `https://swetrix.org`+- **connect-src:** `https://api.swetrix.com`+- **img-src:** `https://api.swetrix.com` (for the noscript fallback)++## Check your installation++Visit your live Drupal site and navigate through a few pages. Within a minute you should see new pageviews appearing in your Swetrix dashboard.diff --git a/docs/docs/integrations/thrivecart.md b/docs/docs/integrations/thrivecart.mdnew file mode 100644index 000000000..5f828a53b--- /dev/null+++ b/docs/docs/integrations/thrivecart.md@@ -0,0 +1,167 @@+---+title: ThriveCart+slug: /thrivecart-integration+---++Integrate Swetrix with your ThriveCart checkout pages to track cart views, purchases, and other conversion events.++## Important: ThriveCart strips script attributes++ThriveCart processes custom scripts and may strip attributes like `defer` from `<script>` tags. To ensure Swetrix loads reliably, use the dynamic script approach shown below instead of a plain `<script src="...">` tag.++## Installation++### 1. Open your checkout settings++1. In your ThriveCart dashboard, navigate to the checkout page you want to track.+2. Go to **Settings > Tracking** (or the custom scripts section, depending on your ThriveCart version).++### 2. Add the tracking script++Paste the following into the **header scripts** section:++```html+<script>+ (function () {+ var s = document.createElement('script')+ s.src = 'https://swetrix.org/swetrix.js'+ s.defer = true+ s.onload = function () {+ swetrix.init('YOUR_PROJECT_ID')+ swetrix.trackViews()+ }+ document.head.appendChild(s)+ })()+</script>++<noscript>+ <img+ src="https://api.swetrix.com/log/noscript?pid=YOUR_PROJECT_ID"+ alt=""+ referrerpolicy="no-referrer-when-downgrade"+ />+</noscript>+```++### 3. Save and test++Save your changes and visit your checkout page. Within a minute you should see pageviews appearing in your Swetrix dashboard.++:::caution+Don't forget to replace `YOUR_PROJECT_ID` with your actual Project ID from the Swetrix dashboard, otherwise tracking won't work.+:::++## Tracking purchase events++To track completed purchases, add a separate script to your ThriveCart **success page** (the page customers see after payment). Because the success page is a different page from the checkout, the Swetrix script needs to be loaded again there.++The script below includes session storage protection to prevent duplicate events if the customer refreshes the page.++### 1. Open your success page settings++In ThriveCart, navigate to the success page configuration for your product. Look for the custom scripts or tracking code section.++### 2. Add the purchase tracking script++Paste the following into the success page header scripts:++```html+<script>+ (function () {+ var EVENT_NAME = 'purchase'+ var EVENT_META = {+ product: 'Your Product Name',+ value: '97.00',+ currency: 'USD'+ }+ var PROJECT_ID = 'YOUR_PROJECT_ID'++ // Prevent duplicate events on page refresh+ var storageKey = 'swx_' + EVENT_NAME + '_sent'+ try {+ if (sessionStorage.getItem(storageKey) === '1') return+ } catch (e) {}++ function sendEvent() {+ swetrix.track({ ev: EVENT_NAME, meta: EVENT_META })+ try {+ sessionStorage.setItem(storageKey, '1')+ } catch (e) {}+ }++ function waitForSwetrix(timeout) {+ var start = Date.now()+ ;(function poll() {+ if (typeof swetrix !== 'undefined' && typeof swetrix.track === 'function') {+ return sendEvent()+ }+ if (Date.now() - start < timeout) {+ return setTimeout(poll, 150)+ }+ })()+ }++ // Load the Swetrix script+ var s = document.createElement('script')+ s.src = 'https://swetrix.org/swetrix.js'+ s.defer = true+ s.onload = function () {+ swetrix.init(PROJECT_ID)+ waitForSwetrix(5000)+ }+ document.head.appendChild(s)+ })()+</script>+```++### 3. Configure the event++Update the variables at the top of the script:++| Variable | Description |+| --- | --- |+| `EVENT_NAME` | The custom event name (e.g. `purchase`, `checkout_complete`). Must contain only letters, numbers, underscores, and dots. |+| `EVENT_META` | An object with event metadata. All values must be strings. |+| `PROJECT_ID` | Your Swetrix Project ID. |++The `EVENT_META` object can contain any key-value pairs relevant to your product. For example:++```javascript+var EVENT_META = {+ product: 'Online Course β SEO Masterclass',+ value: '197.00',+ currency: 'USD',+ plan: 'lifetime'+}+```++Refer to the [tracking script reference](/swetrix-js-reference) for details on `swetrix.track()` and the metadata constraints (max 20 keys, values must be primitives, total value length under 1000 characters).++## Tracking multiple products++If you sell multiple products through ThriveCart, each product has its own checkout and success page. Add the tracking script to each success page with the appropriate product name, price, and other metadata.++## Using with subdomains++If your ThriveCart checkout is hosted on a subdomain (e.g. `checkout.yourdomain.com`) and your main site is on `yourdomain.com`, Swetrix will track both under the same project as long as you use the same Project ID. This lets you build funnels that follow the full user journey from your marketing site through to checkout and purchase.++## Troubleshooting++### Pageviews not appearing++- **Check the script is loading.** Open your browser's Developer Tools (F12), go to the Network tab, and look for `swetrix.js` being loaded.+- **Check for JavaScript errors.** In the Console tab, look for any red error messages related to Swetrix.+- **Verify your Project ID.** Make sure you've replaced `YOUR_PROJECT_ID` with your actual ID from the Swetrix dashboard.+- **Try the footer instead.** If ThriveCart is blocking the script in the header, try adding it to the footer scripts section instead.++### Duplicate purchase events++The script uses `sessionStorage` to fire the purchase event only once per browser session. If you're still seeing duplicates:++- Clear your browser's session storage and test again.+- Make sure you only have one copy of the purchase tracking script on the success page.++### Script not loading at all++- Confirm that custom scripts are enabled for your ThriveCart account. Some plans or configurations may restrict them.+- Contact ThriveCart support if scripts are being stripped entirely.diff --git a/docs/sidebars.js b/docs/sidebars.jsindex 70f16fa2f..46600f058 100644--- a/docs/sidebars.js+++ b/docs/sidebars.js@@ -28,6 +28,8 @@ module.exports = {'integrations/laravel','integrations/gitbook','integrations/prestashop',+ 'integrations/drupal',+ 'integrations/thrivecart',],},'troubleshooting',