Javascript SDK
Getting Started
The onsite API is used to interact with Commanders Act features with JavaScript.
There are different commands available within cact()
: config
is used to set general options, trigger
event is used to send data, and other specific get/update/revoke
commands are used to interract with platform features (ex : get user consent)
To use the API, you must have either a web container on the page or the JS SDK library script : https://cdn.tagcommander.com/events/sdk.js
How to use
The onsite API consists of a single function, cact()
, with the following strict signature:
Argument | Descriptions | Required |
---|---|---|
| A string identifier used to select the desired method. | Required |
| A JavaScript object that includes data passed to the method. | Optional |
| A javascript object that is used to override the default settings like | Optional |
| A JavaScript callback function that is used to receive information or events from the onsite API. | Optional |
Onsite API is included in each containers and privacy banners.
Initialize global settings with config
Use the config
command to initialize and configure settings for a particular workspace.
This command is optional, you can also set custom settings directly inside a trigger
command, through the config object parameter.
The config command takes the following format:
Config object accept 3 parameters, they are optional if you use a web container on your page :
siteId
: if not set, the default value is the site id of the last web container loaded (tC.id_site
)sourceKey
: if not set, the default value is derivative from you web container id. If you don't have a web container, the sourceKey is mandatory and correspond to your JS SDK source.collectionDomain
: if not set, the default value iscollect.commander1.com
(or your first party domain, if you setup one and use a web container)
Example :
Send event
To use the API, you must have either a web container on the page or the JS SDK library script : https://cdn.tagcommander.com/events/sdk.js
To send event data to the serverside Commanders Act platform, use this command:
Example : to send a purchase event :
Example : to send a purchase event, overriding the default tracking domain / workspace / sourcekey :
Get information
To get various values from Commanders Act, use this command:
Example : to get consent from TrustCommander, you can call the consent.get
API like this:
The onsite API methods are called asynchronously. In case e.g. you need information synchronous in the <head>
of the document it is recommended to cache and retrieve the result of the API in localStorage
.
Error handling
You can handle errors through error property in the callback object. Example:
Meta Properties
The meta
property includes metadata and context for the consent that was provided on a browser. You can see the list of Meta properties here
API Stub (optional)
For advance usage, we provide also an API stub that can be added when you need to interact with the API before containers or banners have loaded. This stub is already included in containers and privacy banners, so you do not have to add in most use cases. The stub is used to buffer all methods in a JavaScript array until Commanders Act JavaScript is loaded and ready to process the methods. This allows for example to use the onsite API before TrustCommander JavaScript was loaded.
window.caReady
is a JavaScript array that buffers the interactions with the API. window.cact
is a JavaScript function used to interact with the onsite API.
In case you work in a big team and are unsure the stub was already installed it is ok to install the JavaScript stub multiple times.
Last updated