Treasure Data works with Google Tag Manager (GTM) to collect your data into a single place. Together, this integration makes Google’s management tool more powerful for your needs.
- Basic knowledge of JavaScript / HTML
- Basic knowledge of Treasure Data
After you have created your container, you can create a new tag within it as a Custom HTML Tag. A tag is a code that is configured to send data to other platforms. Tags are used to combine various third-party tools without the need to edit the website's source code.
- Open Google Tag Manager.
- Select your container.
- Add the following lines into the Configure Tag section, with your write-only API key and database name. The following tag should be associated and fired by All Pages trigger.
<!-- Treasure Data -->
<script type="text/javascript">
!function(t,e){if(void 0===e[t]){e[t]=function(){e[t].clients.push(this),this._init=[Array.prototype.slice.call(arguments)]},e[t].clients=[];for(var r=function(t){return function(){return this["_"+t]=this["_"+t]||[],this["_"+t].push(Array.prototype.slice.call(arguments)),this}},s=["addRecord","blockEvents","fetchServerCookie","fetchGlobalID","fetchUserSegments","resetUUID","ready","setSignedMode","setAnonymousMode","set","trackEvent","trackPageview","trackClicks","unblockEvents"],n=0;n<s.length;n++){var c=s[n];e[t].prototype[c]=r(c)}var o=document.createElement("script");o.type="text/javascript",o.async=!0,o.src=("https:"===document.location.protocol?"https:":"http:")+"//cdn.treasuredata.com/sdk/2.5/td.min.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(o,a)}}("Treasure",this);
// Configure an instance for your database
var td = new Treasure({
host: 'in.treasuredata.com',
writeKey: 'YOUR_WRITE_ONLY_APIKEY_IS_HERE',
database: 'DATABASE_NAME'
});
// Enable cross-domain tracking
td.set('$global', 'td_global_id', 'td_global_id');
// track pageview information to 'pageviews' table
td.trackPageview('pageviews');
</script>Your screen might look like this.

You can track links users click by setting the Click Trigger, which sends an additional JavaScript tag with your data and the {{Click URL}} variable to Treasure Data. The {{Click URL}} variable contains the URL information of the link. You can use the Click Trigger to track when users have clicked links to external sites on which you cannot track page-view. Google Tag Manager variables make your tracking more adaptable. They are like containers that hold changing values, so you are able to take information for your website's context and pass it to your tags, making your tracking and analytics setup more customizable.
- From your workspace, select Variables.
- Select Configure.
- In the Clicks section of the variables panel, select Click URL.

Tags are associated with triggers, which are rules or conditions that determine when and where the tags should be executed on your website. Triggers act as event listeners and monitor website activity. They signal the associated tags to run accordingly. Triggers control when and where your tags are activated or executed.
- From your workspace, select Triggers.
- Select New.
- In the Trigger Configuration Panel, select Click - Just Links. The Clicks tag is selected under References to this Trigger, so that any references to the trigger fire.

- In the References to this Trigger panel, select Clicks.
- Select Custom HTML under Tag Type.
3. In the HTML section, insert the following code.
<script type="text/javascript">
!function(t,e){if(void 0===e[t]){e[t]=function(){e[t].clients.push(this),this._init=[Array.prototype.slice.call(arguments)]},e[t].clients=[];for(var r=function(t){return function(){return this["_"+t]=this["_"+t]||[],this["_"+t].push(Array.prototype.slice.call(arguments)),this}},s=["addRecord","blockEvents","fetchServerCookie","fetchGlobalID","fetchUserSegments","resetUUID","ready","setSignedMode","setAnonymousMode","set","trackEvent","trackPageview","trackClicks","unblockEvents"],n=0;n<s.length;n++){var c=s[n];e[t].prototype[c]=r(c)}var o=document.createElement("script");o.type="text/javascript",o.async=!0,o.src=("https:"===document.location.protocol?"https:":"http:")+"//cdn.treasuredata.com/sdk/2.4/td.min.js";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(o,a)}}("Treasure",this);
// Configure an instance for your database
var td = new Treasure({
host: 'in.treasuredata.com',
writeKey: 'YOUR_WRITE_ONLY_APIKEY_IS_HERE',
database: 'DATABASE_NAME'
});
// Enable cross-domain tracking
td.set('$global', 'td_global_id', 'td_global_id');
// Track click information to 'clickviews' table
var click_url = {{Click URL}};
td.set('clickviews', {click_url: click_url});
td.trackPageview('clickviews');
</script>Then you see an additional click_url column in the pageviews table.

You might have two TD tags registered for All Pages and click_trigger.

You might see records associated with Google Tag Manager. See the following table for examples of the data contained in these records.
| td_title | td_referer | td_host | td_url |
|---|---|---|---|
| gtm-msr | https://gtm-msr.appspot.com/render?id=xxxxxxx | gtm-msr.appspot.com | https://gtm-msr.appspot.com/render2?id=xxxxx |
- To exclude data from your query result, choose one of the following:
- Filter results using td_title or td_host.
- If you do not want to record data in your tables, add the following to your code:
<script type="text/javascript">
if (document.location.href.search('gtm-msr.appspot') == -1) {
/* Attribution code here */
}
</script>