apps/web/guides/gtm-track-sale.md
Configure Google Tag Manager for sale tracking
The following steps assume that you've already installed the Dub Analytics script through GTM.
To read the dub_id cookie that Dub Analytics sets, you'll need to create a new User-Defined Variable in Google Tag Manager.
In your GTM workspace, navigate to the Variables section and click New to create a new variable.
Configure the variable with the following settings:
dub_idClick Save to create the variable.
There are two ways to track sale events with Google Tag Manager:
This method tracks sales when users land on an order confirmation or success page after completing a purchase. This approach is more reliable as it's less likely to be blocked by ad blockers and provides better data accuracy.
Create a Custom HTML tag with the following code:
<script>
(function () {
// Get query parameters from URL
var params = new URLSearchParams(window.location.search);
var customerId = params.get("customer_id");
var amount = params.get("amount");
var invoiceId = params.get("invoice_id");
// Get dub_id from cookie using GTM variable
var clickId = {{Dub ID Cookie}} || "";
// Only track the sale event if customer ID, amount, and clickId are present
if (customerId && amount && clickId) {
dubAnalytics.trackSale({
eventName: "Purchase",
customerExternalId: customerId,
amount: parseInt(amount), // Amount in cents
invoiceId: invoiceId || undefined,
currency: "usd", // Customize as needed
paymentProcessor: "stripe", // Customize as needed
clickId: clickId,
});
}
})();
</script>
Important: Make sure to pass along the
customer_idandamountquery parameters to the order confirmation page so that the sale event can be attributed to the correct customer and purchase.
Configure this tag to fire on specific pages by creating a Page View trigger with conditions:
/order-confirmation/checkout/successName this tag "Dub Sales Tracking - Order Confirmation" and save it.
This method tracks sales immediately when users complete checkout forms on your website. Note that this approach may be less reliable due to ad blockers and timing issues.
Create a Custom HTML tag with the following code:
<script>
(function () {
// Get checkout data - customize these selectors based on your form
var customerId = document.getElementById("customer_id")
? document.getElementById("customer_id").value
: "";
var amount = document.getElementById("amount")
? document.getElementById("amount").value
: "";
var invoiceId = document.getElementById("invoice_id")
? document.getElementById("invoice_id").value
: "";
// Get dub_id from cookie using GTM variable
var clickId = {{Dub ID Cookie}} || "";
// Only track the sale event if customer ID, amount, and clickId are present
if (customerId && amount && clickId) {
dubAnalytics.trackSale({
eventName: "Purchase",
customerExternalId: customerId,
amount: parseInt(amount), // Amount in cents
invoiceId: invoiceId || undefined,
currency: "usd", // Customize as needed
paymentProcessor: "stripe", // Customize as needed
clickId: clickId,
});
}
})();
</script>
Important: You'll need to customize the DOM selectors (
getElementById('customer_id'),getElementById('amount'), etc.) to match your actual form field IDs or use different methods to capture the checkout data based on your website's structure.
Configure this tag to fire on Form Submission by creating a new trigger:
Name this tag "Dub Sales Tracking - Checkout Form" and save it.
To test your GTM setup, you can use the Preview mode in Google Tag Manager:
?customer_id=123&amount=5000&invoice_id=inv_123)You can also verify that sales are being tracked by: