Website integration via JavaScript SDK
Overview of integrating Maestra via JavaScript pixel
Initialization General Description
JavaScript SDK allows your website to communicate with the Maestra framework asynchronously. It can be integrated just like Google Analytics without affecting page load speed and tracker operation.
To operate the JavaScript SDK, a single "maestra” object is used. API methods are called in the following format: maestra('methodName', methodParameters, ... )
Variable methodParameters can be either a string or an object. The expression above allows you to invoke methods anywhere on the page after the initialization code. If JavaScript SDK code hasn’t loaded yet, API calls will be queued and will start processing as soon as the script is initialized.
Calls made before consentIf you use the
consentblock, calls made while consent is pending — product views or cart updates, for example — are held in the browser. They're sent if the visitor accepts on the same page and discarded if they decline.
Main Script
The Main Script is responsible for initializing the tracking code. It must be deployed on every page and can be installed via tag management layers such as Google Tag Manager or directly into the website.
There are two versions of the script:
- Without consent — tracks every visitor from the first page. Use it if your site doesn't ask visitors for cookie consent.
- With consent — waits for the visitor's choice in your cookie banner before tracking them. Use it if your site asks for consent.
Shopify storesThe Maestra app embed installs the tracker and follows Shopify's cookie banner. Don't manually paste the
maestra('create', …)snippet — it runs before the app embed and overrides consent settings, tracking visitors before they answer the banner.
Without consent
<script>
maestra = window.maestra || function() { maestra.queue.push(arguments); };
maestra.queue = maestra.queue || [];
maestra('create', {
endpointId: '<Endpoint ID>'
});
</script>
<script src="https://api.maestra.io/scripts/v1/tracker.js" async></script>maestra = window.maestra || function() { maestra.queue.push(arguments); };
maestra.queue = maestra.queue || [];
maestra('create', {
endpointId: '<Endpoint ID>'
});
var script = document.createElement("script");
script.src = "https://api.maestra.io/scripts/v1/tracker.js";
document.getElementsByTagName('head')[0].appendChild(script);With consent
Step 1. Add the script
Deploy this version on every page instead of the one without consent:
<script>
maestra = window.maestra || function() { maestra.queue.push(arguments); };
maestra.queue = maestra.queue || [];
maestra('create', {
endpointId: '<Endpoint ID>',
consent: {
mode: 'buffer', // or 'gate'
default: 'pending' // wait for the visitor's choice
}
});
</script>
<script src="https://api.maestra.io/scripts/v1/tracker.js" async></script>The consent block takes two settings:
| Setting | Values | What it does |
|---|---|---|
mode | 'buffer' (default), 'gate' | buffer saves the landing page (URL with UTM parameters and click IDs, referrer, arrival time) in sessionStorage and sends it after Accept, recorded at the original arrival time. gate keeps nothing; tracking starts from the page where the visitor accepted. |
default | 'pending', 'granted', 'denied' | The state before the visitor makes a choice. Use 'pending' when your banner asks for opt-in. Required whenever consent is present. |
Invalid consent block. If the
consentblock is invalid — for example,defaultis missing or misspelled — the tracker tracks no one until the visitor makes an explicit choice. Check the browser console for errors.
Zero requests before consent. In gate mode,
tracker.jsstill loads — it just stores and sends nothing. If you need zero requests to Maestra before consent, load the snippet only after the visitor accepts.
Step 2. Pass the visitor's choice
Call consent.set from your consent tool's callback when the visitor answers the banner or changes their choice later. You can call it on every page with the current choice — repeating the same value does nothing.
maestra('consent.set', 'granted'); // visitor accepted
maestra('consent.set', 'denied'); // visitor declined or withdrew consentStep 3. Read the current state (optional)
// Current state: 'pending', 'granted', or 'denied'
maestra('consent.get', function (state) {
console.log(state);
});
// Called every time the state changes
maestra('consent.subscribe', function (state) {
console.log(state);
});How consent works
Calls in each consent state
| Consent state | Event calls (maestra('async', …)) | consent.set / consent.get / consent.subscribe |
|---|---|---|
pending | Held in the browser. Sent if the visitor accepts on the same page, discarded if they decline. | Work any time. |
granted | Sent right away. | Work any time. |
denied | Not sent. | Work any time — call consent.set('granted') if the visitor changes their mind. |
What happens on each choice
- Accept: identifier cookies are created and tracking starts on the same page, with no reload. In buffer mode, the saved landing page is sent first.
- Decline: nothing is sent. In buffer mode, the saved landing page is deleted.
- Withdraw (Accept → Decline): the tracker deletes
maestraDeviceUUIDanddirectCrm-session, clears its localStorage and IndexedDB data, and stops sending requests. If the visitor accepts again, tracking starts over with a new anonymous identifier.
The choice is saved in the first-party maestraConsent cookie for one year and applied on later pages without waiting for the banner.
For the full behavior, see Consent and Tracking.
Passing frontend events
After initializing the main script, website can send events with customizable payloads. Each API method has to be set up in the Maestra dashboard before it can be used. Once API method is created, it can be called on a website that has the main script installed.
Please see below examples of some frontend API calls.
maestra("async", {
operation: "Website.AuthorizeCustomer",
data: {
customer: {
ids: {
websiteID: "<Customer website ID>"
}
}
}
});maestra("async", {
operation: "Website.SetCart",
data: {
productList: [
{
product: {
ids: {
website: "<Product ID on the website>"
}
},
count: "<Number of items>",
pricePerItem: "<Price per item>"
},
{
product: {
ids: {
website: "<Product ID on the website>"
}
},
count: "<Number of items>",
pricePerItem: "<Price per item>"
}
]
}
});maestra("async", {
operation: "Website.ViewProduct",
data: {
viewProduct: {
product: {
ids: {
website: "<Product ID on the website>"
}
}
}
}
});
maestra("async", {
operation: "Website.ViewCategory",
data: {
viewProductCategory: {
productCategory: {
ids: {
website: "<Product category ID on the website>"
}
}
}
}
});Updated 29 days ago

