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.


Prerequisites

  • Basic knowledge of JavaScript / HTML

  • Basic knowledge of Treasure Data

Creating the Tag

After you have created your container, you can create a new tag within it as a Custom HTML Tag.

  1. Open Google Tag Manager.

  2. Select your container.

  3. 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.

Tracking the Click URL Variable

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.

Enabling the {{Click URL}} Variable

  1. From your workspace, select Variables.

  2. Select Configure.

  3. In the Clicks section of the variables panel, select Click URL.




Defining the Click Trigger

  1. From your workspace, select Triggers.

  2. Select New.

  3. 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.


Setting the Tag for the Click Trigger

  1. In the References to this Trigger panel, select Clicks.

  2. 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.


Excluding Data from Google Tag Manager

You might see records associated with Google Tag Manager. See the following table for examples of the data contained in these records.

  1. 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>
  • No labels