Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Real-Time Segmentation can be used to enable real-time personalization for people who are visiting your website, using the Profile API. This set up assumes that you have one or more websites where you want to track customer activity. It also assumes that you have already created a database and tables within your Treasure Data instance to store the collected information.

Table of Contents

Prerequisites

Setting Up Website Tracking and Installing the Treasure Data JavaScript SDK

You will need to set up website tracking by installing the TD JS SDK before setting up a realtime segment. 

Expand
titleSetting Up JS SDK

Our JavaScript SDK is available through GitHub. Check the repository, if necessary. The README contains the description of JavaScript SDK API. There are some additional APIs to track custom events and such.

  1. Using the TD Console, obtain your write only API Key. For example:

  2. Locate and open the HTML files of the web pages that you want to have tracked for your real-time segmentation.

  3. Add the following code to the header (<head>) of each of the pages that you want to track.

    Code Block
    languagejs
    linenumberstrue
    <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=["addRecord","blockEvents","fetchServerCookie","fetchGlobalID","fetchUserSegments","resetUUID","ready","setSignedMode","setAnonymousMode","set","trackEvent","trackPageview","trackClicks","unblockEvents"],s=0;s<r.length;s++){var c=r[s];e[t].prototype[c]=function(t){return function(){return this["_"+t]=this["_"+t]||[],this["_"+t].push(Array.prototype.slice.call(arguments)),this}}(c)}var n=document.createElement("script");n.type="text/javascript",n.async=!0,n.src=("https:"===document.location.protocol?"https:":"http:")+"//cdn.treasuredata.com/sdk/3.0/td.min.js";var o=document.getElementsByTagName("script")[0];o.parentNode.insertBefore(n,o)}}("Treasure",this);
    </script>


  4. Add the following code snippet to include your write only API key and indicate the Treasure Data database where you want to have information from the sites saved to. Also enable cross-domain tracking and enable tracking of page views.

    Code Block
    linenumberstrue
    <script type="text/javascript">
      // 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_table>');
    </script


  5. Optionally, set custom parameters.

    Code Block
    linenumberstrue
    td.set('pageviews', {foo: 'foo', bar: 'bar'});
    td.trackPageview('pageviews');


  6. Optionally, track custom events. For example, to track a button click add the following:

    Code Block
    languagejs
    linenumberstrue
    var buttonEvent2 = function () {
      td.trackEvent('button', {
        number: 1
      });
      // doButtonEvent(1);
    };


  7. Optionally, track events by individual. Be sure to comply with all necessary data collection and privacy policies if you choose to do this.

    Code Block
    linenumberstrue
    td.trackClicks()
    
    var successConsentCallback = function (consented) {
      if (consented) {
        td.setSignedMode()
      } else {
        td.setAnonymousMode()
      }
    }
    
    var failureConsentCallback = function () {
      // error occurred, consent unknown
      td.setAnonymousMode()
    }
    
    ConsentManagementVendor.getConsent(userId, successConsentCallback, failureConsentCallback) 



Setting Up a Master Segment to Use for Realtime Segmentation

The behavior table must be set up in the master segment for you to set up a realtime segment.

Expand
titleSetting Up a Master Segment
  1. Open TD Console
  2. Select Data Workbench > Master Segments.

  3. Select New Master Segment to create a new master segment or select an existing master segment.

  4. Optionally, an Attribute table by clicking on the +. 

  5. Add the Behavior table. This is required for real-time segmentation.
    The behavior table must be the table that the event collector put the data into above. And it should include the following columns:
    td_client_id
    td_host
    td_path
    td_ip
    td_user_agent

  6. Define All columns for the Behavior table.


  7. Select Save.

Creating a Realtime Segment

You can choose a behavior and specify the scope of a time window for scanning.

  1. Open TD Console.
  2. Navigate to Audience Studio.
  3. Select Segments & Funnels.

  4. Select the master profile that you defined for real-time segmentation.
  5. Select Create Realtime Segment.


  6. Select Add Attributes (optional) or Add Behaviors to open the Rule Builder.
  7. In the Rule Builder, optionally add rules for Profile Attributes.
    Attributes are limited to 5 and 1000 bytes.

  8. Add rules for Behaviors. Time Window must contain a value higher than 0.

  9. Select Save.
  10. Name the Realtime Segment and select the folder where you want to save and select Create.

Generating a Token

Numbered Headings
start-numbering-with1
start-numbering-ath5
Navigate to Profiles > Profiles API Tokens.
Select New Token.
Enter a description for this token.
In the Attributes drop-down list, select the column names which the Profiles API token will return.
Select Add Segment.

Select Save.

Treasure Data automatically generates the Profile API Token.

Updating Segment Data

Post data using postback to the Behavior table. Using the following code:

  • replace the td_api_write_key

  • replace the database_name and behavior_table_name

  • replace <xxxxxxx> with name

Postback

Code Block
linenumberstrue
curl -D a -X POST \
-H 'Content-Type: application/json' \
-H 'X-TD-Write-Key: TD_API_WRITE_KEY' \
--data-binary '{"item_id": "<td_api_write_key>", "col1str": "<xxxxxxx>", "col2int": 100}' \
https://in.treasuredata.com/postback/v3/event/database_name/behavior_table_name

Segment data will be refreshed.

Getting Segment Data

CURL

Code Block
linenumberstrue
https://cdp.in.treasuredata.com/cdp/lookup/collect/segments?version=2&amp;token=TOKEN&amp;key.item_id=ITEM_ID&amp;callback=TreasureJSONPCallback2

Returns

Code Block
linenumberstrue
TreasureJSONPCallback2([{"values": [SEGMENT_ID1,...],"attributes": {"attr1": ATTR1_VALUE,...},"key": {"item_id": ITEM_ID},"audienceId": AUDIENCE_ID}])