> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Add strategy

> Set a personalization strategy.

export const Legacy = ({title, href}) => {
  return <Note>

    This page documents an earlier version of the API client.
    For the latest version, see <a href={href}>{title}</a>.

    </Note>;
};

<Legacy title="Define the personalization strategy" href="/doc/libraries/sdk/methods/personalization/set-personalization-strategy" />

**Required ACL:** `editSettings`

## Examples

<CodeGroup>
  ```cs C# theme={"system"}
  var strategy = new SetStrategyRequest
  {
      EventsScoring =
          new List<EventsScoring>
          {
              new EventsScoring("Add to cart", "conversion", 50),
              new EventsScoring("Purchase", "conversion", 100)
          },
      FacetsScoring = new List<FacetsScoring>
      {
          new FacetsScoring("brand", 100),
          new FacetsScoring("categories", 10)
      },
      PersonalizationImpact = 50
  };

  client.SetPersonalizationStrategy(strategy);
  ```

  ```go Go theme={"system"}
  strategy := recommendation.Strategy{
  	EventsScoring: []recommendation.EventsScoring{
  		{"Add to cart", "conversion", 50},
  		{"Purchase", "conversion", 100},
  	},
  	FacetsScoring: []recommendation.FacetsScoring{
  		{"brand", 100},
  		{"categories", 10},
  	},
  	PersonalizationImpact: opt.PersonalizationImpact(50),
  }

  res, err := client.SetPersonalizationStrategy(strategy)
  ```

  ```java Java theme={"system"}
  SetStrategyRequest strategy = new SetStrategyRequest()
          .setEventsScoring(Arrays.asList(
                  new EventsScoring("Add to cart", "conversion", 50),
                  new EventsScoring("Purchase", "conversion", 100)
          ))
          .setFacetsScoring(Arrays.asList(
                  new FacetsScoring("brand", 100),
                  new FacetsScoring("categories", 10)
          ))
          .setPersonalizationImpact(50);


  client.setPersonalizationStrategy(strategy);
  ```

  ```js JavaScript theme={"system"}
  recommendation
    .setPersonalizationStrategy({
      eventsScoring: [
        { eventName: "Add to cart", eventType: "conversion", score: 50 },
        { eventName: "Purchase", eventType: "conversion", score: 100 },
      ],
      facetsScoring: [
        { facetName: "brand", score: 100 },
        { facetName: "categories", score: 10 },
      ],
      personalizationImpact: 50,
    })
    .then(() => {
      // done
    });
  ```

  ```kotlin Kotlin theme={"system"}
  val strategy = PersonalizationStrategy(
      eventsScoring = listOf(
          EventScoring("Add to cart", "conversion", 50),
          EventScoring("Purchase", "conversion", 100)
      ),
      facetsScoring = listOf(
          FacetScoring("brand", 100),
          FacetScoring("categories", 10)
      ),
      personalizationImpact = 50
  )

  clientRecommendation.setPersonalizationStrategy(strategy)
  ```

  ```php PHP theme={"system"}
  $recommendation->setPersonalizationStrategy([
      'eventsScoring' => [
          [
              'eventName' => 'Add to cart',
              'eventType' => 'conversion',
              'score' => 50,
          ],
          [
              'eventName' => 'Purchase',
              'eventType' => 'conversion',
              'score' => 100,
          ],
      ],
      'facetsScoring' => [
          ['facetName' => 'brand', 'score' => 100],
          ['facetName' => 'categories', 'score' => 10],
      ],
      'personalizationImpact' => 50,
  ]);
  ```

  ```python Python theme={"system"}
  recommendation.set_personalization_strategy(
      {
          "eventsScoring": [
              {"eventName": "Add to cart", "eventType": "conversion", "score": 50},
              {"eventName": "Purchase", "eventType": "conversion", "score": 100},
          ],
          "facetsScoring": [
              {"facetName": "brand", "score": 100},
              {"facetName": "categories", "score": 10},
          ],
          "personalizationImpact": 50,
      }
  )
  ```

  ```ruby Ruby theme={"system"}
  client = Algolia::Recommendation::Client.create("YourApplicationID", "YourWriteAPIKey")

  client.set_personalization_strategy(
    {
      eventsScoring: [
        {
          eventName: "Add to cart",
          eventType: "conversion",
          score: 50
        },
        {
          eventName: "Purchase",
          eventType: "conversion",
          score: 100
        }
      ],
      facetsScoring: [
        {facetName: "brand", score: 100},
        {facetName: "categories", score: 10}
      ],
      personalizationImpact: 50
    }
  )
  ```

  ```scala Scala theme={"system"}
  val strategy = SetStrategyRequest(
    eventsScoring = Seq(
      EventsScoring("Add to cart", "conversion", 50),
      EventsScoring("Purchase", "conversion", 100)
    ),
    facetsScoring = Seq(
      FacetsScoring("brand", 100),
      FacetsScoring("categories", 10)
    ),
    personalizationImpact = 50
  )

  client.execute {
    set personalizationRecommendationStrategy strategy
  }
  ```

  ```swift Swift theme={"system"}
  let personalizationClient = PersonalizationClient(appID: "YourApplicationID", apiKey: "YourAPIKey")

  let eventsScoring: [EventScoring] = [
    .init(eventName: "Add to cart", eventType: .conversion, score: 50),
    .init(eventName: "Purchase", eventType: .conversion, score: 100)
  ]

  let facetsScoring: [FacetScoring] = [
    .init(facetName: "brand", score: 100),
    .init(facetName: "categories", score: 10)
  ]

  let strategy = PersonalizationStrategy(eventsScoring: eventsScoring,
                                         facetsScoring: facetsScoring,
                                         personalizationImpact: 0)

  personalizationClient.setPersonalizationStrategy(strategy) { result in
    if case .success(let response) = result {
      print("Response: \(response)")
    }
  }
  ```
</CodeGroup>

## Parameters

<ParamField body="strategy" type="object" required>
  An object for configuring the Personalization strategy.

  <Expandable>
    <ParamField body="eventsScoring" type="object[]" required>
      Associate a score to some events.

      **Example:**

      ```json JSON icon=braces theme={"system"}
      {
        "eventsScoring": [
          {
            "eventName": "purchase"
            "eventType": "conversion"
            "score": 100
          }
        ],
      }
      ```

      <Expandable>
        <ParamField body="eventName" type="string">
          Event name.
        </ParamField>

        <ParamField body="eventType" type="string">
          Event type: `conversion`, `click`, or `view`.
        </ParamField>

        <ParamField body="score" type="number">
          Score of the event.
        </ParamField>
      </Expandable>
    </ParamField>

    <ParamField body="facetsScoring" type="object[]" required>
      Associate a score to some facets.

      **Example:**

      ```json JSON icon=braces theme={"system"}
      {
        "facetsScoring": [
          {
            "facetName": "brand",
            "score": 100
          }
        ]
      }
      ```

      <Expandable>
        <ParamField body="facetName" type="string">
          Facet name
        </ParamField>

        <ParamField body="score" type="number">
          Score for this facet.
        </ParamField>
      </Expandable>
    </ParamField>

    <ParamField body="personalizationImpact" type="integer" required>
      The impact personalization has on the search results:
      a number between 0 (personalization disabled) and 100 (personalization fully enabled)
    </ParamField>
  </Expandable>
</ParamField>

<ParamField body="requestOptions" type="object">
  A mapping of request options to send along with the request.
</ParamField>

## Response

<ResponseField name="message" type="string">
  Response message.
</ResponseField>

<ResponseField name="status" type="integer">
  Response status code.
</ResponseField>

### Response as JSON

This section shows the JSON response returned by the API.
Each API client wraps this response in language-specific objects, so the structure may vary.
To view the response, use the `getLogs` method.
Don't rely on the order of properties—JSON objects don't preserve key order.

```jsonc JSON icon=braces theme={"system"}
{
  "status": 200,
  "message": "Strategy was successfully updated"
}
```
