# How to Set Up TD JS SDK in Tealium Iq Tag Management

You can set up the JS SDK using Tealium IQ tag management.

Learn more about:

* [Prerequisites](#prerequisites)
* [Log in to Tealium Customer Datahub](#log-in-to-tealium-customer-datahub)
* [Create a Draft](#create-a-draft)
* [Publish the Extension](#publish-the-extension)
* [Reference](#reference)


# Prerequisites

* Add [utag.js](https://community.tealiumiq.com/t5/iQ-Tag-Management/JavaScript-Code-Extension/ta-p/11925#toc-hId-474147131) (Tealium’s main tag) v4.38 or higher in the main body of your website.
* Manage JavaScript Code Extensions account-level permission to create drafts of code.
* JavaScript Draft Promotion profile-level permission to publish this extension.


Treasure Data recommends you verify the implementation of any new features or functionality at your site using the Treasure Data JavaScript SDK version 3 before you start using it in production. It manages cookies differently. Be aware when referring to most of these articles that you need to define the suggested event collectors and Treasure Data JavaScript SDK version 3 calls in your solutions.For example, change //cdn.treasuredata.com/sdk/2.5/td.min.js to //cdn.treasuredata.com/sdk/3.0.0/td.min.js.

# Log in to Tealium Customer Datahub

1. Log in to [https://my.tealiumiq.com/](https://my.tealiumiq.com/).
![](/assets/image2020-11-16_10-54-42.d3ba3ebd05668a1f470fdcf76f1f3a11eb994721cd838b84945ad42a1dc3a52a.ad50cbca.png)
2. Go to **Extensions** > **Advanced** > **JavaScript code**.


![](/assets/image2020-11-16_11-0-55.b8d9de13774d1ee5fcae857a32d96b226e3457f66a6544438d3419f4db358b5a.ad50cbca.png)

# Create a Draft

1. Specify the title for the tag and put the code in the editor section.
2. You need the following for the JavaScript code:
  * Library: [https://github.com/treasure-data/td-js-sdk#script-snippet - Connect to preview](https://github.com/treasure-data/td-js-sdk#script-snippet)
  * Initialization:[https://github.com/treasure-data/td-js-sdk#initializing - Connect to preview](https://github.com/treasure-data/td-js-sdk#initializing)
3. The Initialization code.


**Example**

```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');
    
td.trackPageview('clickviews');
```

After you complete the code, you need to specify the scope in which the tag will be triggered. The default scope is All tags; the tag runs everywhere the *utag* defines the default scope.

The tag can be scoped to PreLoader, All Tags, a specific vendor tag (tag-scope), or to the DOM Ready event.

# Publish the Extension

After your code is ready to use, you need to publish the code.

1. Specify an environment and then select the **Save / Publish.**
2. Type in the title as the version and include a note about the version.
3. Select the environment in which you want to publish the extension.
![](/assets/image2020-11-16_11-12-58.7f20abf15a2709ed7247b26dd5b61823d2f7e54058d382d47edd951eac555c43.ad50cbca.png)
4. After the publishing process has completed, the extension is ready to use on the website. Validate the extension using the [Tealium browser plugin](https://community.tealiumiq.com/t5/iQ-Tag-Management/Web-Companion/ta-p/11947).


# Reference

Review the following links as reference documentation:

* Community Q&A about JavaScript code execution with Tealium iQ
  * [https://community.tealiumiq.com/t5/Tealium-iQ-Tag-Management/Is-it-possible-to-load-Custom-JavaScript-Snippet-on-all-the/td-p/7650](https://community.tealiumiq.com/t5/Tealium-iQ-Tag-Management/Is-it-possible-to-load-Custom-JavaScript-Snippet-on-all-the/td-p/7650)
* JavaScript code extension
  * [https://community.tealiumiq.com/t5/iQ-Tag-Management/JavaScript-Code-Extension/ta-p/11925](https://community.tealiumiq.com/t5/iQ-Tag-Management/JavaScript-Code-Extension/ta-p/11925)
* Save and publish
  * [https://community.tealiumiq.com/t5/iQ-Tag-Management/Saving-and-Publishing/ta-p/10929](https://community.tealiumiq.com/t5/iQ-Tag-Management/Saving-and-Publishing/ta-p/10929)
* Web Companion
  * [https://community.tealiumiq.com/t5/iQ-Tag-Management/Web-Companion/ta-p/11947](https://community.tealiumiq.com/t5/iQ-Tag-Management/Web-Companion/ta-p/11947)