> For the complete documentation index, see [llms.txt](https://docs.umbraco.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.umbraco.com/umbraco-engage/developers/settings/custom-goals-scoring.md).

# Custom goals scoring

Discover how to set up and trigger custom goals in Umbraco Engage using C# code.

## Setting up custom goals

To set custom goals:

1. Navigate to **Settings** > **Goals** in the Umbraco Engage section and select **Create goal**.
2. Set **Trigger** to **Custom code**.
3. Execute C# code to trigger the goal.

Creating the goal is similar to creating a page view or page event goal. After you save the goal, the code snippet shows its **goal ID**. Use this ID to trigger the goal from code.

![Goal id](https://2164986937-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fds5IoWmsX6a6ooVdYthx%2Fuploads%2Fgit-blob-626a7e8fd6f4923472d52c9686d712d84fe3c14d%2FSettings-Goals-customcode-v16.png?alt=media)

## Trigger goal in C\#

To trigger the goal, execute C# code during the visitor's pageview. Inject `Umbraco.Engage.Infrastructure.Analytics.Goals.IGoalService`, which provides multiple overloads to trigger goals:

* `TriggerGoal(long goalId, int value)` - Using numeric ID
* `TriggerGoal(Guid goalKey, int value)` - Using GUID key (preferred)

An implementation looks like:

```cs
using Umbraco.Engage.Infrastructure.Analytics.Goals;

public class YourService
{
    private IGoalService _goalService;

    public YourService(IGoalService goalService) => _goalService = goalService;

    public void TriggerGoalById()
    {
        // Using numeric ID (legacy approach)
        _goalService.TriggerGoal(goalId: 37, value: 42);
    }

    public void TriggerGoalByKey()
    {
        // Using GUID key (preferred approach)
        _goalService.TriggerGoal(goalKey: new Guid("a1b2c3d4-e5f6-7890-abcd-ef1234567890"), value: 42);
    }
}
```

### Triggering Outside of HttpContext

The method automatically determines the current page view, linking the goal to a session and visitor. This means the `HttpContext` should be available and `triggerGoal` should be called from within a valid pageview. See [Request tracking](/umbraco-engage/developers/analytics/what-is-measured-by-default.md) for what is considered a valid pageview.

To trigger a goal outside of an HTTP request or a valid pageview, use the overload of `TriggerGoal` that takes the GUID of the pageview.

Retrieve the pageview GUID in the original request using `Umbraco.Engage.Infrastructure.Analytics.Common.IAnalyticsPageviewGuidManager.GetPageviewGuid()`. You will need to store this pageview GUID for later use when invoking:

```cs
// Using numeric goal ID
_goalService.TriggerGoal(pageviewGuid, goalId, value);

// Using GUID goal key (preferred)
_goalService.TriggerGoal(pageviewGuid, goalKey, value);
```

This custom goal can now be used like other goals and will show up in any statistics related to goals.

## Available Method Overloads

| Method        | Parameters                                         | Use Case                                                |
| ------------- | -------------------------------------------------- | ------------------------------------------------------- |
| `TriggerGoal` | `(long goalId, int value = 0)`                     | Within HttpContext/pageview, using numeric ID           |
| `TriggerGoal` | `(Guid goalKey, int value = 0)`                    | Within HttpContext/pageview, using GUID key (preferred) |
| `TriggerGoal` | `(Guid pageviewGuid, long goalId, int value = 0)`  | Outside HttpContext, using numeric ID                   |
| `TriggerGoal` | `(Guid pageviewGuid, Guid goalKey, int value = 0)` | Outside HttpContext, using GUID key (preferred)         |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.umbraco.com/umbraco-engage/developers/settings/custom-goals-scoring.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
