Push and Modify JavaScript Code into the Variant of the New Experiment
Requirements
access token
To use the Automation API, you first need an access token.
Follow the instructions in this section to retrieve it programmatically.
Goal
Once the experiment is created, update its variant (Variation 1
) with:
Kameleoon.API.Core.runWhenElementPresent("#bloc-567, .cta-button, #bloc-789", function(elements) {
document.querySelector(".cta-button, #bloc-789").innerText = "More new text";
});
Then change the text to "Updated Text". See this documentation for code details. We’ll use the Partial Update Variation endpoint.
Requirements
access token
To use the Automation API, you first need an access token.
Follow the instructions in this section to retrieve it programmatically.
variationId
andexperimentId
Find them by selecting Experiment_1
in the dashboard:
Steps
1. Inject the JavaScript Code
Endpoint:
PATCH https://api.kameleoon.com/variations/{variationId}
Name | Type | Description |
---|---|---|
jsCode | String | JavaScript code to update. |
name | String | Variation name. |
experimentId | String | The experiment ID associated with the code. |
Example:
curl -L -X PATCH 'https://api.kameleoon.com/variations/1053310' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <ACCESS_TOKEN>' \
--data-raw '{
"experimentId": 283505,
"jsCode": "Kameleoon.API.Core.runWhenElementPresent(\".cta-button, #bloc-789\", function(elements) { document.querySelector(\".cta-button, #bloc-789\").innerText = \"Text\";});"
}'
2. Verify the Code Injection
Check the Code Editor for Variation 1
:
3. Update the Code
Change the text to "Updated Text":
curl -L -X PATCH 'https://api.kameleoon.com/variations/1053310' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <ACCESS_TOKEN>' \
--data-raw '{
"experimentId": 283505,
"jsCode": "Kameleoon.API.Core.runWhenElementPresent(\"#bloc-567, .cta-button\", function(elements) { document.querySelector(\".cta-button, #bloc-789\").innerText = \"Updated Text\";});"
}'
4. Verify the Update
Refresh the editor to confirm the update: