> ## 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.API.Goals

> Reference for the Kameleoon.API.Goals methods that trigger goals, process conversions, and cancel conversions.

This module manages goal triggering and conversion data, which covers purchase confirmations and revenue registration.

## cancelConversion

The `cancelConversion()` method cancels a conversion that you triggered during the current visit. This method can't cancel conversions from previous visits.

```javascript theme={null}
var buttons = Kameleoon.API.Utils.querySelectorAll(".btnRemoveFromCart");

buttons.forEach(function (button) {
  Kameleoon.API.Utils.addEventListener(button, "mousedown", function (event) {
    Kameleoon.API.Goals.cancelConversion(event.target.id);
  });
});
```

**Arguments**

| Name | Type | Description |
| - | - | - |
| goalNameOrID | String or Number | Name or ID of the goal as defined in the Kameleoon App. This field is mandatory. |

## processConversion

> [!WARNING]
>
> Before implementing the processConversion, review the [Custom Code for Custom Goal](#triggergoal-custom-code-for-custom-goal) feature, which simplifies customization by removing the requirement for a goal ID.

The `processConversion()` method triggers a conversion. Metadata must be [configured in the Kameleoon App](https://docs.kameleoon.com/user-manual/assets/goals/create-a-goal.md#metadata) before setting them with `processConversion()`.

```javascript theme={null}
//Conversion for "Add to cart" goal (goal id 123456)
Kameleoon.API.Core.runWhenElementPresent("#buyButton", ([buyButton]) => {
  Kameleoon.API.Utils.addEventListener(buyButton, "click", () => {
    Kameleoon.API.Goals.processConversion(123456); //Add to cart
  });
});

//Conversion for "Add to cart" goal (goal id 123456) with optional revenue argument
Kameleoon.API.Core.runWhenElementPresent("#buyButton", ([buyButton]) => {
  Kameleoon.API.Utils.addEventListener(buyButton, "click", () => {
    Kameleoon.API.Goals.processConversion(123456, 19.99); //Add to cart
  });
});
```

> [!NOTE]
>
> If you initiate conversions from a Tag Management System, such as Google Tag Manager, use the [Kameleoon Command Queue](https://docs.kameleoon.com/developer-docs/apis/activation-api-js/api-reference/command-queue.md) to delay execution until the engine loads. The engine processes queued commands in order after initialization. Example:
>
> ```javascript theme={null}
> window.kameleoonQueue = window.kameleoonQueue || [];
> kameleoonQueue.push(['Kameleoon.API.Goals.processConversion', 'GOAL_ID']);
> ```

**Arguments**

| Name | Type | Description |
| - | - | - |
| goalNameOrID | String or Number | Name or ID of the goal as defined in the Kameleoon App. Goal names must be unique. This field is mandatory. |
| revenue | Number | Transaction amount. Providing this value enables Kameleoon to track purchase metrics, such as revenue and average cart amount. This field is optional. |
| metadata | Object | Sets specific values for custom data defined as goal metadata in the Kameleoon App. [Define the metadata beforehand in the Kameleoon App](https://docs.kameleoon.com/user-manual/assets/goals/create-a-goal.md#metadata). The object keys are the **custom data indexes** (numeric values found in the `INDEX` column of the [Custom data dashboard](https://docs.kameleoon.com/user-manual/assets/custom-data/manage-custom-data.md#find-a-custom-data’s-index)). This field is optional. |

> [!NOTE]
>
> Metadata values are accessible through [raw data exports](https://docs.kameleoon.com/user-manual/experiment-analytics/analyze-results/results-page/results-page-actions.md#export) and [the results page](https://docs.kameleoon.com/user-manual/experiment-analytics/analyze-results/data-and-metrics/goal-metadata.md).
>
> If you provide the `metadata` parameter, Kameleoon uses those values for the current conversion instead of the values you previously collected through `setCustomData()`. If you omit the parameter, Kameleoon uses the last tracked `customData` values before the conversion in the same visit.
>
> Kameleoon only considers the metadata values you pass directly to the `processConversion()` method. It ignores previously set custom data. In the following example, the conversion associates only with the provided metadata (for example, index 5 with 'Amex Credit Card').
>
> ```javascript theme={null}
> Kameleoon.API.Data.setCustomData(5, 'Credit Card');
> Kameleoon.API.Data.setCustomData(9, 'Express Delivery');
>
> Kameleoon.API.Goals.processConversion("GoalIDorName", revenue, {
>     5: ["Amex Credit Card", "PayPal"], // PaymentMode Custom Data
>     4: 1234567, // OrderID CustomData
>     3: "1q2w3e4r" // PayPalID CustomData 
> })
> ```

## triggerGoal (Custom Code for Custom Goal)

To trigger **custom goals**, inject custom code by creating a new goal and adding the code to the `Trigger my goal` panel.

![Adding custom code to the Trigger my goal panel of a custom goal](https://storage.googleapis.com/kameleoon-storage-documentation/developers/images/api-tutorial/custom-code-for-custom-goals.gif)

The `triggerGoal()` function triggers the goal from the code panel without requiring a goal ID.

```javascript theme={null}
//No params
triggerGoal();
//With revenue
triggerGoal(49.99);
//With revenue and metadata
triggerGoal(49.99, {5: "Gold"});
```

**Arguments**

| Name | Type | Description |
| - | - | - |
| revenue | Number | Transaction amount. Providing this value enables Kameleoon to track purchase metrics, such as revenue and average cart amount. This field is optional. |
| metadata | Object | Sets specific values for custom data defined as goal metadata in the Kameleoon App. The object keys are the **custom data indexes** (numeric values found in the `INDEX` column of the [Custom data dashboard](https://docs.kameleoon.com/user-manual/assets/custom-data/manage-custom-data.md#find-a-custom-data’s-index)). This field is optional. |

> [!NOTE]
>
> Metadata values are accessible through [raw data exports](https://docs.kameleoon.com/user-manual/experiment-analytics/analyze-results/results-page/results-page-actions.md#export) and [the results page](https://docs.kameleoon.com/user-manual/experiment-analytics/analyze-results/data-and-metrics/goal-metadata.md).
>
> If you provide the `metadata` parameter, Kameleoon uses those values for the current conversion instead of the values you previously collected through `setCustomData()`. If you omit the parameter, Kameleoon uses the last tracked `customData` values before the conversion in the same visit.
>
> Kameleoon only considers the metadata values you pass directly to the `triggerGoal()` method. It ignores previously set custom data. In the following example, the conversion associates only with the provided metadata (for example, index 5 with 'Amex Credit Card').
>
> ```javascript theme={null}
> Kameleoon.API.Data.setCustomData(5, 'Credit Card');
> Kameleoon.API.Data.setCustomData(9, 'Express Delivery');
>
> triggerGoal( revenue, {
>     5: ["Amex Credit Card", "PayPal"], // PaymentMode Custom Data
>     4: 1234567, // OrderID CustomData
>     3: "1q2w3e4r" // PayPalID CustomData 
> })
>
> ```
