td-javascript-sdk
Treasure Data JavaScript SDK
Browser-only SDK for client-side event tracking. For server-side, use REST API or pytd.
Quick Start: Pageview Tracking
Add to HTML <head>:
<!-- TD JS SDK Loader (v4.4.1) -->
<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=["collectTags","addRecord","blockEvents","fetchServerCookie","fetchGlobalID","fetchUserSegments","fetchPersonalization","resetUUID","ready","setSignedMode","setAnonymousMode","set","trackEvent","trackPageview","trackClicks","unblockEvents"],c=0;c<s.length;c++){var o=s[c];e[t].prototype[o]=r(o)}var n=document.createElement("script");n.type="text/javascript",n.async=!0,n.src="https://cdn.treasuredata.com/sdk/4.4.1/td.min.js";var i=document.getElementsByTagName("script")[0];i.parentNode.insertBefore(n,i)}}("Treasure",this);
</script>
<!-- Initialize and track -->
<script type="text/javascript">
var td = new Treasure({
database: 'your_database',
writeKey: '1234/xxxxxxxx', // write-only API key
host: 'us01.records.in.treasuredata.com'
});
td.trackPageview('pageviews');
</script>
Configuration
var td = new Treasure({
database: 'your_database', // required
writeKey: '1234/xxxxxxxx', // required, write-only API key
host: 'us01.records.in.treasuredata.com', // required for v4.0+
development: true // optional, enables console logging
});
Regional Streaming Endpoints (v4.0+ required):
| Region | Endpoint |
|---|---|
| US | us01.records.in.treasuredata.com |
| Tokyo | ap01.records.in.treasuredata.com |
| EU | eu01.records.in.treasuredata.com |
| AP | ap02.records.in.treasuredata.com |
Core Methods
// Track pageview (auto-collects URL, title, referrer, user agent, etc.)
td.trackPageview('pageviews');
// Track custom events (also auto-collects context like trackPageview)
td.trackEvent('events', { action: 'signup', location: 'header' });
// Send raw records (no auto-collected fields)
td.addRecord('purchases', { order_id: '123', amount: 99.99 });
// Auto-track clicks on links, buttons, inputs (excludes password fields)
td.trackClicks({ tableName: 'clicks' });
// Set global properties (included in all subsequent trackEvent/trackPageview calls)
td.set('$global', { user_id: 'user_123', environment: 'production' });
Auto-Collected Fields
trackPageview() and trackEvent() automatically include:
td_url,td_title,td_referrer,td_host,td_pathtd_screen,td_viewport,td_languagetd_user_agent,td_platformtd_client_id,td_version
Privacy Controls
td.setAnonymousMode(); // Exclude PII from tracking
td.setSignedMode(); // Include PII
td.blockEvents(); // Stop sending events
td.unblockEvents(); // Resume sending
td.resetUUID(); // Generate new client ID
NPM Installation
npm install td-js-sdk
import Treasure from 'td-js-sdk';
Resources
- GitHub: https://github.com/treasure-data/td-js-sdk
- CDN:
https://cdn.treasuredata.com/sdk/4.4.1/td.min.js
More from treasure-data/td-skills
pytd
Expert assistance for using pytd (Python SDK) to query and import data with Treasure Data. Use this skill when users need help with Python-based data analysis, querying Presto/Hive, importing pandas DataFrames, bulk data uploads, or integrating TD with Python analytical workflows.
20tdx-basic
Executes tdx CLI commands for Treasure Data. Covers `tdx databases`, `tdx tables`, `tdx describe`, `tdx query`, `tdx auth setup`, context management with profiles/sessions, and output formats (JSON/TSV/table). Use when users need tdx command syntax, authentication setup, database/table exploration, schema inspection, or query execution.
3workflow
Manages TD workflows using `tdx wf` commands. Covers project sync (pull/push/clone), running workflows, monitoring sessions/attempts, task timeline visualization, retry/kill operations, and secrets management. Use when users need to manage, monitor, or debug Treasure Workflow projects via tdx CLI.
3journey
Load when the client wants to create, edit, or manage a CDP customer journey. Use for building journey YAML with segments, activations, and stage steps, modifying journey stages or flow logic (decision points, condition waits, A/B tests), or pushing journey changes to Treasure Data. Also load when the client wants to analyze journey performance, query journey tables, create journey dashboards, or generate journey action reports.
2engage
Manage Treasure Engage email templates and campaigns using `tdx engage` commands with YAML+HTML configs. Use when creating, editing, previewing, validating, or deploying email templates, email/push campaigns, managing workspaces, or any task involving Engage email content — even if the user only mentions "create an email" or "build an HTML email". Always write YAML definition files alongside HTML — never push raw HTML without a YAML wrapper.
2agent-test
Run automated tests for LLM agents using `tdx agent test`. Covers test.yml format with user_input/criteria, single and multi-round tests, evaluation by judge agent, and criteria development workflow.
2