Google Tag Manager
Integrate Databuddy with Google Tag Manager for centralized tag management while maintaining privacy-first analytics. Perfect for teams managing multiple tracking tools.
TL;DR: In GTM, create a Custom HTML tag with an inline loader that appends
https://cdn.databuddy.cc/databuddy.jsand setsdata-client-id. Enable optional flags such asdata-track-attributesordata-track-errors. Page views and sessions are automatic. Trigger on All Pages and publish.
Why Use GTM with Databuddy?
Basic Setup
Step 1: Create Databuddy Tag
Important: GTM sanitizes HTML and removes non-standard attributes like
data-client-id. Always usecreateElement()andsetAttribute()as shown below:
<script>
(function () {
var el = document.createElement("script");
el.src = "https://cdn.databuddy.cc/databuddy.js";
el.async = true;
el.crossOrigin = "anonymous";
el.setAttribute("data-client-id", "{{Databuddy Client ID}}");
el.setAttribute("data-track-attributes", "true");
el.setAttribute("data-track-errors", "true");
document.head.appendChild(el);
})();
</script>Step 2: Create Variables
Create a Constant variable for your Client ID:
Step 3: Set Trigger
Step 4: Publish
Advanced Configuration
Environment-Based Loading
Load different Databuddy configurations based on environment:
<script>
(function() {
var isProduction = '{{Page Hostname}}' === 'yourdomain.com';
var clientId = isProduction ? '{{Databuddy Prod Client ID}}' : '{{Databuddy Dev Client ID}}';
var el = document.createElement("script");
el.src = "https://cdn.databuddy.cc/databuddy.js";
el.async = true;
el.crossOrigin = "anonymous";
el.setAttribute("data-client-id", clientId);
el.setAttribute("data-track-attributes", "true");
el.setAttribute("data-track-errors", "true");
document.head.appendChild(el);
})();
</script>Conditional Loading
Load Databuddy only for specific conditions:
<script>
(function() {
// Only load for non-internal traffic
if ('{{Internal Traffic}}' === 'false') {
var el = document.createElement("script");
el.src = "https://cdn.databuddy.cc/databuddy.js";
el.async = true;
el.crossOrigin = "anonymous";
el.setAttribute("data-client-id", "{{Databuddy Client ID}}");
el.setAttribute("data-track-attributes", "true");
el.setAttribute("data-track-errors", "true");
document.head.appendChild(el);
}
})();
</script>Event Tracking Setup
Custom Event Tags
Create tags for specific events:
E-commerce Purchase Tag:
Create Data Layer Variables for the values you need (e.g. Transaction ID, Purchase Revenue, Currency, Items Count), then fire this tag on your purchase event trigger:
<script>
if (window.databuddy) {
databuddy.track('purchase', {
transaction_id: '{{Transaction ID}}',
value: {{Purchase Revenue}},
currency: '{{Currency}}',
item_count: {{Items Count}}
});
}
</script>Form Submission Tag:
<script>
if (window.databuddy) {
databuddy.track('form_submit', {
form_id: '{{Form ID}}',
form_name: '{{Form Name}}',
form_location: '{{Page Path}}'
});
}
</script>Data Layer Integration
Send GTM data layer events to Databuddy:
<script>
if (window.databuddy) {
databuddy.track('{{Event}}', {
category: '{{Event Category}}',
action: '{{Event Action}}',
label: '{{Event Label}}',
value: {{Event Value}},
custom_parameter: '{{Custom Parameter}}'
});
}
</script>Triggers Configuration
The analytics script records pageviews automatically, including SPA navigation. Use the All Pages trigger only to load the script; do not add a second pageview tag.
Scroll Tracking
Databuddy's built-in tracking already covers scroll depth, so you do not need a separate GTM scroll depth tag.
Click Tracking
Track specific button clicks:
<script>
if (window.databuddy) {
databuddy.track('button_click', {
button_text: '{{Click Text}}',
button_classes: '{{Click Classes}}',
page_path: '{{Page Path}}'
});
}
</script>E-commerce Integration
Enhanced E-commerce Setup
Track the complete customer journey. Create Data Layer Variables for the product values your site pushes to the data layer, and fire each tag on the matching data layer event trigger.
Product View Tag:
<script>
if (window.databuddy) {
databuddy.track('product_view', {
product_id: '{{Product ID}}',
product_name: '{{Product Name}}',
product_category: '{{Product Category}}',
product_price: {{Product Price}},
currency: '{{Currency}}'
});
}
</script>Add to Cart Tag:
<script>
if (window.databuddy) {
databuddy.track('add_to_cart', {
product_id: '{{Product ID}}',
product_name: '{{Product Name}}',
quantity: {{Quantity}},
value: {{Item Revenue}},
currency: '{{Currency}}'
});
}
</script>Begin Checkout Tag:
<script>
if (window.databuddy) {
databuddy.track('begin_checkout', {
value: {{Cart Value}},
currency: '{{Currency}}',
item_count: {{Items Count}}
});
}
</script>User Privacy and Consent
GDPR Compliance
Respect user privacy choices:
<script>
(function() {
// Check consent status
var hasConsent = '{{Cookie Consent Status}}' === 'granted';
if (hasConsent) {
var el = document.createElement("script");
el.src = "https://cdn.databuddy.cc/databuddy.js";
el.async = true;
el.crossOrigin = "anonymous";
el.setAttribute("data-client-id", "{{Databuddy Client ID}}");
el.setAttribute("data-track-attributes", "true");
el.setAttribute("data-track-errors", "true");
document.head.appendChild(el);
}
})();
</script>Consent Mode Integration
Integrate with Google Consent Mode:
<script>
gtag("consent", "default", {
analytics_storage: "denied",
ad_storage: "denied",
});
// When user grants consent
gtag("consent", "update", {
analytics_storage: "granted",
});
// Initialize Databuddy after consent
if (window.databuddy) {
databuddy.track("consent_granted", {
consent_type: "analytics",
timestamp: new Date().toISOString(),
});
}
</script>Debugging and Testing
Debug Mode Setup
Enable debug mode for testing:
<script>
var isDebugMode = '{{Debug Mode}}' === 'true';
if (window.databuddy && isDebugMode) {
console.log('Databuddy Debug Mode Enabled');
databuddy.track('debug_event', {
page: '{{Page Path}}',
timestamp: new Date().toISOString()
});
}
</script>Preview Mode Testing
Variable Testing
Create test variables for debugging:
Debug Info Variable:
function() {
return {
page_path: '{{Page Path}}',
page_title: '{{Page Title}}',
user_agent: navigator.userAgent,
timestamp: new Date().toISOString()
};
}Best Practices
Performance Optimization
<script>
(function () {
try {
var el = document.createElement("script");
el.src = "https://cdn.databuddy.cc/databuddy.js";
el.async = true;
el.crossOrigin = "anonymous";
el.setAttribute("data-client-id", "{{Databuddy Client ID}}");
el.onerror = function () {
console.warn("Databuddy script failed to load");
};
document.head.appendChild(el);
} catch (e) {
console.error("Error initializing Databuddy:", e);
}
})();
</script>Tag Organization
Version Management
Migration from Google Analytics
Replacing GA4 with Databuddy
Data Mapping
Map GA4 events to Databuddy equivalents:
Troubleshooting
Common Issues
Tag Not Firing:
Events Not Tracking:
Performance Issues:
Debug Checklist
Related Integrations
Need help with your GTM integration? Contact us at support@databuddy.cc.
How is this guide?