This guide will walk you through the process of integrating Dub Analytics with Google Tag Manager (GTM).
1

Create a New Tag

First, navigate to your Google Tag Manager account and create a new tag:
  • Click on Tags in the left sidebar
  • Click the New button
  • Select Custom HTML as the tag type
Select Custom HTML tag
2

Add the Dub Analytics Script

In the Custom HTML section, you’ll need to add the Dub Analytics script. Copy and paste the following code into the HTML field:
<script>
  var script = document.createElement("script");
  script.defer = true;
  script.src = "https://www.dubcdn.com/analytics/script.js";
  document.getElementsByTagName("head")[0].appendChild(script);
</script>
Add dub analytics script
If you’re using Dub Partners for affiliate management, you will also need to set up the data-domains property to enable client-side click-tracking.
<script>
  ...
  var script = document.createElement("script");
  script.defer = true;
  script.src = "https://www.dubcdn.com/analytics/script.js";
  script.dataset.domains = JSON.stringify({ refer: "refer.yourdomain.com" }); // Add this line to match the short domain you're using for your referral links
  document.getElementsByTagName("head")[0].appendChild(script);
</script>
3

Configure the Trigger

To ensure the analytics script loads on all pages:
  • Click on the Triggering section
  • Select All Pages as the trigger type
  • This will make the tag fire on every page load
4

Save and Publish

  • Name your tag Dub Analytics
  • Click Save to store your changes
  • Click Submit to create a new version
  • Finally, click Publish to activate the tag on your website
You can verify the installation with the following tests:
  1. Open the browser console and type in _dubAnalytics – if the script is installed correctly, you should see the _dubAnalytics object in the console.
  2. Add the ?dub_id=test query parameter to your website URL and make sure that the dub_id cookie is being set in your browser.
If both of these checks pass, the script is installed correctly. Otherwise, please make sure:
  • The analytics script was added to the <head> section of the page
  • If you’re using a content delivery network (CDN), make sure to purge any cached content

Concepts

You can pass the following props to the @dub/analytics script to customize its behavior:
data-api-host
url
default:"https://api.dub.co"
The base URL for the Dub API. This is useful for setting up reverse proxies to avoid adblockers.
data-attribution-model
first-click | last-click
default:"last-click"
The attribution model to use for the analytics event. The following attribution models are available:
  • first-click: The first click model gives all the credit to the first touchpoint in the customer journey.
  • last-click: The last click model gives all the credit to the last touchpoint in the customer journey.
Custom properties to pass to the cookie. Refer to MDN’s Set-Cookie documentation for all available options.
data-domains
JSON-stringified object
Configure the domains that Dub will track. The following properties are available:
data-query-param
string
default:"via"
The query parameter to listen to for client-side click-tracking (e.g. ?via=abc123).