Google Tag Manager
This guide covers installing the DAP SDK through Google Tag Manager (GTM), including conditional loading and SPA considerations.
Prerequisites
- A Google Tag Manager account
- A GTM container published on your target site
Step-by-Step Setup
1. Create a Custom HTML Tag
- Open your GTM container at tagmanager.google.com.
- Navigate to Tags > New.
- Name the tag (e.g., "Break Ground SDK").
- Click Tag Configuration and select Custom HTML.
- Paste the SDK snippet:
<!-- DAP SDK Snippet -->
<script>
(function (w, d, s, o, f, js, fjs) {
w["DAPObject"] = f;
w[f] =
w[f] ||
function () {
(w[f].q = w[f].q || []).push(arguments);
};
w[f].l = 1 * new Date();
js = d.createElement(s);
fjs = d.getElementsByTagName(s)[0];
js.id = f;
js.src = o;
js.async = 1;
js.crossOrigin = "anonymous";
fjs.parentNode.insertBefore(js, fjs);
})(window, document, "script", "https://cdn.example.com/sdk.js", "DAP");
DAP("init", { tenantKey: "YOUR_TENANT_KEY" });
</script>
2. Set the Trigger
- Click Triggering below the tag configuration.
- Select All Pages (the built-in Page View trigger).
- Save the tag.
3. Publish
- Click Submit in the top-right corner.
- Add a version name (e.g., "Add Break Ground SDK") and publish.
Using GTM Variables
To avoid hardcoding the tenant key, create a GTM variable:
- Go to Variables > User-Defined Variables > New.
- Choose Constant as the variable type.
- Name it "BreakGround Tenant Key" and set the value to your tenant key.
- Save the variable.
- Update the snippet in your tag to reference the variable:
<script>
(function (w, d, s, o, f, js, fjs) {
w["DAPObject"] = f;
w[f] =
w[f] ||
function () {
(w[f].q = w[f].q || []).push(arguments);
};
w[f].l = 1 * new Date();
js = d.createElement(s);
fjs = d.getElementsByTagName(s)[0];
js.id = f;
js.src = o;
js.async = 1;
js.crossOrigin = "anonymous";
fjs.parentNode.insertBefore(js, fjs);
})(window, document, "script", "https://cdn.example.com/sdk.js", "DAP");
DAP("init", { tenantKey: "{{BreakGround Tenant Key}}" });
</script>
Using a GTM variable makes it easy to update the tenant key across all tags in one place, and separates configuration from code.
Conditional Loading
You can control where the SDK loads by configuring trigger conditions:
Load on specific pages only:
- Create a new trigger: Trigger > New > Page View.
- Set it to fire on Some Page Views.
- Add a condition: Page Path contains
/app(or your application path). - Assign this trigger to the BreakGround tag instead of All Pages.
Load based on Data Layer variables:
If your application pushes values to the Data Layer, you can use them as trigger conditions:
// In your application code
dataLayer.push({ userLoggedIn: true });
Then create a Data Layer Variable in GTM and use it as a trigger condition.
SPA Route Tracking
The SDK monitors pushState and popstate events independently. It handles client-side navigation without any GTM configuration.
Do not create additional GTM tags that fire the SDK snippet on History Change triggers. The SDK is designed to load once and track route changes on its own. Firing the tag multiple times will cause errors.
Gotchas
- Do not double-load: If the SDK snippet is already in your page HTML, do not also load it through GTM. This will cause duplicate initialization. Choose one method.
- Tag Sequencing: GTM's Tag Sequencing feature (firing one tag before another) does not guarantee the SDK has fully loaded. If you need to call
DAP()from another GTM tag, use the SDK's ready event instead:<script>DAP("on", "ready", function () {// Safe to call DAP methods hereDAP("identify", { userId: "user-42" });});</script> - Consent mode: If you use a consent management platform, configure the BreakGround tag to fire only after the user has granted consent. Set the trigger to fire after the consent event rather than on All Pages.
Verification
In GTM Preview Mode
- Click Preview in GTM to open Tag Assistant.
- Navigate to your site. The Tag Assistant panel will show which tags fired.
- Confirm the "Break Ground SDK" tag appears under Tags Fired.
On the Live Site
- After publishing, open your site in a browser.
- Open the developer console (
F12orCmd+Option+I). - Type
window.DAPand press Enter. You should see the DAP function object. - Enable debug mode for detailed logs:
DAP("init", { tenantKey: "YOUR_TENANT_KEY", debug: true });
- Check the Console tab for messages prefixed with
[DAP]confirming successful initialization.
Next Steps
- SDK Configuration -- customize SDK behavior
- User Identity -- identify users for targeted content
- SDK Events -- listen to SDK lifecycle events