In the Kameleoon App, there are multiple entry points where you can add custom JavaScript that executes on your website. This article provides specific recommendations for each available entry point. Please note that all Kameleoon scripts run once per page load. Before implementing custom scripts, consider using Kameleoon Command Queue for optimized execution.Documentation Index
Fetch the complete documentation index at: https://docs.kameleoon.com/llms.txt
Use this file to discover all available pages before exploring further.
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 (e.g., Google Tag Manager) alongside the Kameleoon Command Queue. This 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 cannot be utilized, please refer to 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 can be used to enhance visitor experience:runWhenConditionTrue: Executes the code when a specific condition is met. 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. Returningfalseorundefinedwill not stop the loop or trigger the callback.
true on all pages other than the product page, causing the loop to run indefinitely:
It is 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 Mutation Observers are disabled on your site, you can specify a polling interval as the 3rd argument of this method.
- If the element is dynamically inserted into the DOM, set
isDynamicElementtotrueas the 4th 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. This is particularly important when using loop-based methods likerunWhenConditionTrue and runWhenElementPresent, as running these methods across the entire site can lead to unnecessary executions and impact 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 being triggered 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. CSS can be utilized for hiding or modifying elements, swapping blocks, and changing styles or text.Handling Single Page Applications (SPAs)
If an experiment is running on a single-page application (SPA) or a dynamically updating page, specific implementation steps are required. Unlike traditional websites, SPAs do not reload entire pages, so Kameleoon needs to identify when content updates happen. Please refer to the guide on setting up an experiment on a single-page application for best practices.Segments / Triggers
The first best practice is to base targeting on data that is 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 is not 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 above.
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 above $30, you would trigger an event in the Global Script once these conditions are met, 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.