Kameleoon Command Queue
The most efficient way to interact with Kameleoon’s API (whether for converting goals, setting custom data, or triggering events) is to leverage existing triggers in your Tag Manager (for example, Google Tag Manager) alongside the Kameleoon Command Queue. Combining Tag Manager triggers with the Command Queue should be your primary approach when adding JavaScript to Kameleoon. Instead of writing lengthy JavaScript code directly in Kameleoon, you can achieve the same results with just two lines of code within your Tag Manager.Examples
- Convert a revenue goal In your Tag Manager, create a trigger on the confirmation page to capture the revenue, and add the following code:kameleoonQueue, see the specific recommendations for each available script detailed below.
Global Script & variation scripts
Optimize JavaScript execution with the Activation API
Utilize Kameleoon’s Activation API to apply variations at the appropriate time. This approach helps prevent performance issues and ensures a seamless experience for visitors. Below are some essential functions of the Activation API that you can use to improve visitor experience:runWhenConditionTrue: Executes the code when a specific condition returnstrue. By default, it runs every 200 milliseconds. However, be careful with conditions that may never resolve, as this can lead to unnecessary looping. Note that the loop will continue running until the function explicitly returnstrue. Returningfalseorundefinedwon’t stop the loop or trigger the callback.
true on all pages other than the product page, causing the loop to run indefinitely:
It’s preferable to retrieve information using the URL or cookies/localStorage instead of depending on
runWhenConditionTrue to wait for the dataLayer or other late-loading objects, like when trying to get information about the page type. This approach ensures faster execution and better performance.runWhenElementPresent: This method ensures that your code runs only when a specific element is present in the DOM, avoiding unnecessary delays caused by dynamically loaded elements.
Key considerations
- Uses Mutation Observers by default (recommended for performance).
- If you turn off Mutation Observers on your site, you can specify a polling interval as the third argument of this method.
- If the element is dynamically inserted into the DOM, set
isDynamicElementtotrueas the fourth argument of this method:
Set your code’s scope to specific pages
To optimize performance, make sure your code executes only on the relevant pages. Care matters most with loop-based methods such asrunWhenConditionTrue and runWhenElementPresent, because running them across the entire site can lead to unnecessary executions and affect performance. For instance, if you are waiting for revenue information in the dataLayer, wrap your code in a condition that checks whether the URL corresponds to the confirmation page. This approach ensures that the code runs only on the confirmation page instead of running site-wide.
Example: Targeting a specific confirmation page before running a loop
3. Prefer CSS for visual changes instead of using JavaScript, especially when dealing with variations in scripts
Using CSS instead of JavaScript results in faster and smoother rendering. You can use CSS to hide or modify elements, swap blocks, and change styles or text.Handling single-page apps (SPAs)
If an experiment runs on a single-page app (SPA) or a dynamically updating page, you need specific implementation steps. Unlike traditional websites, SPAs don’t reload entire pages, so Kameleoon needs to identify when content updates happen. See the guide on setting up an experiment on a single-page app for best practices.Segments / Triggers
The first best practice is to base targeting on data that’s immediately available when the page loads. This includes information such as the page URL, browser type, device type, and any data stored in cookies or local storage. If your targeting requires data that isn’t instantly accessible, follow these guidelines to ensure efficient execution and avoid unnecessary delays.1. Custom Javascript condition
This condition allows you to use custom JavaScript to target visitors. There are two main options available: ####- a. Checking the condition immediately This script executes every 75 milliseconds before the DOM is fully loaded, and then every 250 milliseconds afterward. By default, it returnsundefined, which causes the script to continue running in a loop until it explicitly returns true (to include the visitor) or false (to exclude them). Always ensure that your condition eventually resolves to either true or false to prevent unnecessary looping. Here’s an optimized example:
- If you want to target a specific element, use the native condition “Element on the page” instead.
- If you want to wait for information on the page, use the “check condition immediately” option described earlier.
2. Custom event
Using custom events is an effective and passive way to target visitors. Rather than continuously monitoring conditions, this method listens for a specific custom event that you define and triggers targeting when that event occurs. This approach is particularly useful in two key scenarios:Scenario 1: When the targeting logic is complex
It’s better to avoid embedding all the logic directly in a JavaScript condition within the targeting. Instead, you can define a custom event in the Global Script. For example, if you want to target visitors on the cart page who have at least three products in their cart and a total amount of more than $30, you would trigger an event in the Global Script once a visitor meets these conditions, rather than writing all of the logic within the JavaScript segment condition.Scenario 2: When multiple segments share similar logic
If several segments require the same or slightly different variations of the same condition, using a custom event prevents redundant code and enhances maintainability. Example: You need to target visitors based on different cart amounts. Instead of duplicating the logic in multiple segments, you define a single script in the Global Script and trigger different custom events for each segment:Custom Data
Custom data, like custom events, helps to avoid duplicate code by allowing you to store and reuse values. You can define custom data in the Global Script or in other scripts, and then utilize it within segments. However, custom data differs from custom events in two key ways:a. By the scope options: “Page,” “Visit,” or “Visitor”
Unlike custom events, which only apply to the current page, custom data can persist across multiple pages and sessions:- Visit Scope: This keeps the value throughout a visitor’s session.
- Visitor Scope: This stores the value for up to 365 days, depending on the cookie policy.