JavaScript API Introduction
To start using the JavaScript library, place the following code in the `head` element of your page, it should be placed before the script tags you are using to send data. You don't need to worry about it slowing down your page load time as it's loaded asynchronously.
<script>
var trak=trak||[];trak.io=trak.io||{};trak.io.load=function(e){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=("https:"===document.location.protocol?"https://":"http://")+"d29p64779x43zo.cloudfront.net/v1/trak.io.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);var r=function(e){return function(){trak.push([e].concat(Array.prototype.slice.call(arguments,0)))}},i=["initialize","identify","track","alias","channel","source","host","protocol","page_view","should_track"];for(var s=0;s<i.length;s++) trak.io[i[s]]=r(i[s]);trak.io.initialize.apply(trak.io,arguments)};
trak.io.load("YOUR_API_TOKEN");
</script>
The JavaScript library will automatically start tracking page views for you. To get the most out of trak.io you really need to be sending us more than just page views. The JavaScript library has the following methods to do that:
No More Anonymous People
As of 2014-09-09, we will no longer track anonymous visitors to your website and apps. All anonymous profiles will been removed from your account, and our JavaScript API will now only track data for your known contacts, such as email subscribers or customers.
To track events and page views you will now need to provide a distinct_id via either trak.io.identify() or trak.io.distinct_id() first.
-
trak.io.identify()- send trak.io data about a user -
trak.io.company()- send trak.io data about a company -
trak.io.alias()- merge the current user with one from another session -
trak.io.track()- track an event for the current user -
trak.io.page_view()- track a page view for the current user
There are a few other methods for altering some of the data that is stored in the user's cookie and sent along with requests to the API
-
trak.io.distinct_id()- get or set the current distinct id -
trak.io.company_id()- get or set the current company id -
trak.io.channel()- get or set the current channel -
trak.io.context()- get or set the current context
Options
In addition to your API token you can also give trak.io.load() an options object.
<script>
var trak=trak||[];trak.io=trak.io||{};trak.io.load=function(e){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=("https:"===document.location.protocol?"https://":"http://")+"d29p64779x43zo.cloudfront.net/v1/trak.io.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);var r=function(e){return function(){trak.push([e].concat(Array.prototype.slice.call(arguments,0)))}},i=["initialize","identify","track","alias","channel","source","host","protocol","page_view"];for(var s=0;s<i.length;s++) trak.io[i[s]]=r(i[s]);trak.io.initialize.apply(trak.io,arguments)};
trak.io.load("YOUR_API_TOKEN", { distinct_id: 'matt@trak.io', channel: 'dashboard' });
</script>
| Option | Type | Default | Description |
|---|---|---|---|
| distinct_id | String | Current session's distinct ID | Providing this option will simply pass it to trak.io.distinct_id(). |
| company_id | String | Current session's company ID | Providing this option will simply pass it to trak.io.company_id(). |
| channel | String | Current session's channel | Providing this option will simply pass it to trak.io.channel(). |
| auto_track_page_views | Boolean | 1 | Whether to automatically track page views, for more information see docs.trak.io/page_view.html. |
| root_domain | String | Current root domain | The distinct id cookie is set on the highest domain we can set one at, you can use this option to override this |