Getting Started
Overview on the Commanders Act onsite API.
The onsite API is used to interact with Commanders Act features with JavaScript.

How to use

The onsite API consists of a single function, cact(), with the following strict signature:
1
cact(command, [options,] [callback])
Copied!
Argument
Descriptions
Required
command
A string identifier used to select the desired method.
Required
options
A JavaScript object that includes data passed to the method.
Optional
callback
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.

Send event

To send event data to the serverside Commanders Act platform, use this command:
1
cact('<event_name>', {<event_params>});
Copied!
Example : to send a purchase event :
1
cact('purchase', {
2
id:'1234',
3
currency: 'EUR',
4
//...
5
});
Copied!

Get information

To get various values from Commanders Act, use this command:
1
cact(get command, [callback])
Copied!
Example : to get consent from TrustCommander, you can call the consent.get API like this:
1
cact('consent.get', function(result) {
2
if (result.consent.status === "all-on") {
3
4
// Consent available for all categories.
5
6
}
7
});
Copied!
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:
1
cact('consent.get', function(result) {
2
3
if (result.error) {
4
5
// Manage the error
6
7
}
8
else if (result.consent.status === "all-on") {
9
10
// Consent available for all categories.
11
12
}
13
});
Copied!

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.
1
window.caReady = window.caReady || [];
2
window.cact = function() { window.caReady.push(arguments); };
Copied!
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 modified 6mo ago