# Generating promo codes

This use case demonstrates how to generate random promo codes, send them to users, and store them for future verification.

<figure><img src="https://77049817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfS5hl0PiysHtKAKMsQTe%2Fuploads%2Fgit-blob-ab1a971b2baf6ecc9c3ae314534dacdc3fd9f6e7%2Funknown.png?alt=media" alt=""><figcaption></figcaption></figure>

## Creating a label tag

Create a new label tag in the **Tools \ Label tags**, select the tag value type as "J**avaScript Function**", call it "**promo\_code**" (or any other name), and place the following code that generates a random code every time the label tag is used.

```javascript
(function() {
    // Define the characters that can be used in the promo code
    var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    // Initialize an empty string to store the result
    var promoCode = '';
    // Set the desired length of the promo code
    var length = 6;

    // Loop 'length' times to generate each character of the code
    for (var i = 0; i < length; i++) {
        // Generate a random index to pick a character from the 'chars' string
        var randomIndex = Math.floor(Math.random() * chars.length);
        // Append the randomly selected character to the promo code
        promoCode += chars.charAt(randomIndex);
    }

    // Return the generated 6-symbol promo code
    return promoCode.toUpperCase();
})();
```

You can adjust it to limit the set of characters used in the promo code and its length.

<figure><img src="https://77049817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfS5hl0PiysHtKAKMsQTe%2Fuploads%2Fgit-blob-62815ac1bbf83ceae01114c5faa7ad811e506468%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

## Creating a popup with the code

Create a popup resource based on the template "Promo Code with Label Tag"

<figure><img src="https://77049817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfS5hl0PiysHtKAKMsQTe%2Fuploads%2Fgit-blob-db18a6c275992470dffdc5e3ecd594da585d7e30%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

You will need to pay attention to two main parameters:

* The name of the label tag that you have created in the previous step
* A special code in the section "When shown", you can leave it as "CODE\_IS\_SHOWN", this code will be used in the next step.

<figure><img src="https://77049817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfS5hl0PiysHtKAKMsQTe%2Fuploads%2Fgit-blob-99e6b606fc572dd9a1aaadfb3e034c1e596a7d39%2FPop-up%20-%20smartico.ai%20%40%20EU2%202025-12-11%2000-25-32.png?alt=media" alt=""><figcaption></figcaption></figure>

## Build a campaign

Create a Realtime campaign (Journey) that will show the popup.

<figure><img src="https://77049817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfS5hl0PiysHtKAKMsQTe%2Fuploads%2Fgit-blob-d4715ae2d0c3060623cc1fac6a0b4681151c7d5b%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Test it; you will see that the pop-up sent to the client contains randomly generated code.

<figure><img src="https://77049817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfS5hl0PiysHtKAKMsQTe%2Fuploads%2Fgit-blob-ab1a971b2baf6ecc9c3ae314534dacdc3fd9f6e7%2Funknown.png?alt=media" alt=""><figcaption></figcaption></figure>

## Call your API to save the promo code

Extend the campaign with "Wait for event" activity, select "Client action" as the event, and "CODE\_IS\_SHOWN" as the action name.

<figure><img src="https://77049817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfS5hl0PiysHtKAKMsQTe%2Fuploads%2Fgit-blob-d55c4b0e911e2a623830ae5010c787cdf41574ed%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Note: If you don't see the client action "CODE\_IS\_SHOWN", make sure you have tested the previous step, and the popup was shown to you. You can also go to the Tools \ Properties, find the property "Core: client action" and use the "+" sign to register this action manually.
{% endhint %}

Connect "Wait for event" activity to the Popup on "Impression".

<figure><img src="https://77049817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfS5hl0PiysHtKAKMsQTe%2Fuploads%2Fgit-blob-68bde40486c858d04c9107983d81e4d7f250ffec%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Create a "WebHook" activity that makes a POST request to your API and passes the generated promo code. For example, you can use a simple JSON structure like the following:

```json
{ 
    "code" : "{{event.code}}"
}
```

<figure><img src="https://77049817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfS5hl0PiysHtKAKMsQTe%2Fuploads%2Fgit-blob-1fe615c8d5aa147be7dfb6119689eaea3ed6ceda%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

Connect the "WebHook" activity to the "Wait for event" using the "When happened" connector.

<figure><img src="https://77049817-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfS5hl0PiysHtKAKMsQTe%2Fuploads%2Fgit-blob-355e5485bc054a2171f45efee38fc0cc802bc712%2Fimage.png?alt=media" alt=""><figcaption></figcaption></figure>

## The final flow

How the flow is working:

1. When the campaign is triggered, Smartico will start preparing the popup to send to the user
2. As part of the preparation, it will generate a promo code and place it into the popup.
3. When the popup is delivered, Smartico will progress the flow by "Impression" connection to the "Wait for event"
4. Right at this moment, the Popup will trigger "Core: Client action" event with action code "CODE\_IS\_SHOWN" and the actual promo code that will be passed in "event.code".
5. The flow will proceed to the WebHook and call your API with the code value populated from the {{event.code}}.

## Next steps and ideas

* Save the submitted promo code via the API and verify it on the next deposit.
* Play with the popup template to adjust UI, text, and images
* Review the JavaScript code that generates promo code and adapt it to your needs (length & character set)
* Use different communication channels to deliver promo code - emails, pushes, telegram, SMS etc.
* Make a copy of "Promo Code with Label Tag" popup template and adjust it to match your style and layout needs.
