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 desired flags (e.g.,data-track-screen-views,data-track-attributes,data-track-errors). 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.setAttribute("data-client-id", "Databuddy Client ID");
el.setAttribute("data-track-screen-views", "true");
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.setAttribute("data-client-id", clientId);
el.setAttribute("data-track-screen-views", "true");
el.setAttribute("data-track-attributes", "true");
el.setAttribute("data-track-errors", "true");
el.setAttribute("data-debug", isProduction ? "false" : "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.setAttribute("data-client-id", "{{Databuddy Client ID}}");
el.setAttribute("data-track-screen-views", "true");
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:
<script>
window.dataLayer = window.dataLayer || [];
if (window.databuddy && {{ecommerce.purchase}}) {
databuddy.track('purchase', {
transaction_id: {{Transaction ID}},
value: {{Purchase Revenue}},
currency: {{Currency}},
items: {{Enhanced Ecommerce Items}}
});
}
</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
Page View Tracking
Create a trigger for enhanced page views:
Scroll Tracking
Set up scroll depth tracking:
<script>
if (window.databuddy) {
databuddy.track('scroll_depth', {
scroll_depth: {{Scroll Depth Threshold}},
page_path: {{Page Path}},
page_title: {{Page Title}}
});
}
</script>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:
Product View Tag:
<script>
if (window.databuddy && {{ecommerce.detail}}) {
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 && {{ecommerce.add}}) {
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 && {{ecommerce.checkout}}) {
databuddy.track('begin_checkout', {
value: {{Cart Value}},
currency: {{Currency}},
items: {{Enhanced Ecommerce Items}}
});
}
</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.setAttribute("data-client-id", "{{Databuddy Client ID}}");
el.setAttribute("data-track-screen-views", "true");
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.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 help@databuddy.cc.
How is this guide?