# Award 1000 points for three purchases over $300 each

Let's create a gamification [promotion rule](/docs/product/rules/overview.md#promotion-rules) that gives 1000 loyalty points to customers who spend over $300 three times. Their three qualifying spends need not be consecutive.

:::important
This tutorial shows how we can use native Talon.One features as workaround to create complex rules.
In this case, we'll use a _fake_ loyalty program to count sessions.
:::

## Create the promotion

In the current scenario, let's use two rules:

1. Count Transactions over $300
1. 3rd transaction over $300

### Create the subledger accessor

Let's create a [subledger](/docs/product/loyalty-programs/use-subledgers.md) in our loyalty program to keep track of the number of sessions
of a given value each customer makes.

1. Click **Subledger accessor**.
1. Set **Loyalty program** to **Transaction counter** and in **Subledger** type `TransactionOver300`.
   This creates a new subledger inside the **Ledgers** loyalty program.
1. Save it as `TransactionOver300`.

### Rule 1: Count transactions over $300

Let's add a free item to sessions when they reach $300:

- Conditions:
  1. **Check for default event**: **Customer Closing session**.
  1. **Check attribute value**: <Attribute name="Session Total (Current Session)"/>`is greater than 300`.

- Effect: **Add loyalty points** and select:

  - **Loyalty program**: **Ledgers**
  - **Subledger name**: `TransactionOver300`
  - **Recipient**: **Current customer (friend)**
  - **Reason**: `1 transaction over $300`
  - **Amount of points**: `1`

This use of a subledger allows us to keep track of the number of transactions of 300.
It's not related to points in this case. It functions as a counter.

### Rule 2: 3rd transaction over $300

This rule detects the third transaction and grants 1000 points.

- Conditions:
  1. **Check for default event**: **Customer Closing session**.
  1. **Check attribute value**: <Attribute name="Session Total (Current Session)"/>`is greater than 300`.
  1. **Check attribute value**: <Attribute name="Current Balance (TransactionOver300)"/>`is equal to 3`.

- Effect: **Add loyalty points** and select:
  - **Loyalty program** to **Sample Wallet**. Note that we do not use the subledger for this.
  - **Recipient** to **Current Customer**.
  - **Reason** to `1000 bonus points`.
  - **Amount of point** to `1000`.

When a customer places an order for the third time, they will get 1000 points.

## Run the campaign

To run and test the campaign, we need three requests to trigger our two rules.
We send the requests to the Integration API's [Update customer session][cs] endpoint.

### A customer makes two purchases of $300 each

<Tabs
  defaultValue="req"
  values={[
    {label: '🚀 Request', value: 'req'},
    {label: '✅ Response', value: 'res'},
  ]}>

<TabItem value="req">

Let's send two requests for the _same_ customer profile but with different sessions.
That cart has a total value of $310 every time. The sessions must be closed.

```json title="Sending a session update" {4,5,11} showLineNumbers
CURL -X PUT https://mycompany.europe-west1.talon.one/v2/customer_sessions/my_session_id -d
'{
  "customerSession": {
    "profileId": "A_given_customer",
    "state": "closed",
    "cartItems": [
      {
        "name": "ProductL",
        "sku": "sku-00003",
        "quantity": 1,
        "price": 310,
        "Category": "",
        "weight": 0,
        "position": 0,
        "attributes": {}
      }
    ],
    "couponCodes": [""],
    "referralCode": "",
    "attributes": {

    },
    "additionalCosts": {}
  },
  "responseContent": []
}'
```

</TabItem>
<TabItem value="res">

We receive this response, which shows the
[`addLoyaltyPoints`](/docs/dev/integration-api/api-effects.md#addloyaltypoints) effect is triggered.

```json title="Response" {10} showLineNumbers
{
  "createdCoupons": [],
  "createdReferrals": [],
  "effects": [
    {
      "campaignId": 244,
      "rulesetId": 487,
      "ruleIndex": 0,
      "ruleName": "Count Transactions over $300",
      "effectType": "addLoyaltyPoints",
      "props": {
        "name": "1 transaction over $300",
        "programId": 56,
        "subLedgerId": "TransactionOver300",
        "value": 1,
        "recipientIntegrationId": "talonone-b46812yoxpb",
        "transactionUUID": "266611c9-22c2-4930-bfb6-26d5978c7880"
      }
    }
  ]
}
```

</TabItem>
</Tabs>

### Send the last of the three sessions

<Tabs
  defaultValue="req"
  values={[
    {label: '🚀 Request', value: 'req'},
    {label: '✅ Response', value: 'res'},
  ]}>

<TabItem value="req">

The last session we close for the same customers triggers the point addition.

```json title="Sending a session update" {4,11} showLineNumbers
CURL -X PUT https://mycompany.europe-west1.talon.one/v2/customer_sessions/my_session_id -d
'{
  "customerSession": {
    "profileId": "A_given_customer",
    "state": "closed",
    "cartItems": [
      {
        "name": "ProductL",
        "sku": "sku-00003",
        "quantity": 1,
        "price": 310,
        "Category": "",
        "weight": 0,
        "position": 0,
        "attributes": {}
      }
    ],
    "couponCodes": [""],
    "referralCode": "",
    "attributes": {

    },
    "additionalCosts": {}
  },
  "responseContent": []
}'
```

</TabItem>
<TabItem value="res">

We receive this response, which shows the
[`addLoyaltyPoints`](/docs/dev/integration-api/api-effects.md#addloyaltypoints) effect is triggered.

```json title="Response" {25,30} showLineNumbers
{
  "createdCoupons": [],
  "createdReferrals": [],
  "effects": [
    {
      "campaignId": 244,
      "rulesetId": 487,
      "ruleIndex": 0,
      "ruleName": "Count Transactions over $300",
      "effectType": "addLoyaltyPoints",
      "props": {
        "name": "1 transaction over $300",
        "programId": 56,
        "subLedgerId": "TransactionOver300",
        "value": 1,
        "recipientIntegrationId": "talonone-b46812yoxpb",
        "transactionUUID": "85ab20e3-aaf5-4e3c-a19f-2ba4f32b1936"
      }
    },
    {
      "campaignId": 244,
      "rulesetId": 487,
      "ruleIndex": 1,
      "ruleName": "3rd transaction over $300",
      "effectType": "addLoyaltyPoints",
      "props": {
        "name": "1000 bonus points",
        "programId": 1,
        "subLedgerId": "",
        "value": 1000,
        "recipientIntegrationId": "talonone-b46812yoxpb",
        "transactionUUID": "26d13ab7-01a4-49e4-b15e-8883fd2454c4"
      }
    }
  ]
}
```

</TabItem>
</Tabs>

:::tip Key Takeaways
- Talon.One's core features are flexible and can be used as workarounds
  to implement advanced logic.
:::

## Related pages

- [Use conditions](/docs/product/rules/conditions/overview.md) and [effects](/docs/product/rules/effects/overview.md)
- [Loyalty Programs][lp]

[cs]: /integration-api#tag/Customer-sessions/operation/updateCustomerSessionV2
[lp]: /docs/product/loyalty-programs/overview.md
[cs]: /integration-api#tag/Customer-sessions/operation/updateCustomerSessionV2
