
Campaign optimization is a very important feature that we develop to let you choose the best time and channel for your customers and send them messages with the highest chances for conversion.  


<div class="admonition admonition-important"><div class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg></div><div class="admonition-body"><div class="admonition-content">

Basic information about our campaign optimizer can be found [HERE](/use-cases/campaign-optimizer)

</div></div></div>


## Prerequisites 
---
**General**
- Implement [Synerise tracker](/developers/web/installation-and-configuration#creating-a-tracking-code) on your website.
- Channel optimization configuration.
- [Email account configuration](/docs/campaign/e-mail/configuring-email-account).
- [SMS account](/docs/settings/configuration/sms-account) configuration (integration e.g. with SMS API). 
- [Webpush integration](/docs/campaign/Webpush/configuring-web-push).
- [Mobile Push integration](/docs/campaign/Mobile/mobile_campaign). 
- [Transactional events](/developers/web/transactions-sdk) implemented (optional).

**AI time optimizer configuration**

- Page visits & other campaign visits.

## Create predefined analytics
---
At the very beginning, in order to achieve channel optimization, we have to use predefined analytics. Let’s say we want to use basic, predefined, CTR based channel optimization: 


<div class="admonition admonition-important"><div class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg></div><div class="admonition-body"><div class="admonition-content">

We are using this example to go through the process of configuring an optimized campaign. Please be aware that the base on which you calculate the optimization and metrics are fully customizable.

</div></div></div>


1. Create the predefined analytics. 

    - First of all **segments**

    Choose the predefined segment matching the channel in which you want to send a campaign. For example:

        - (Default) Optimal Channel by CTR - mobile push
        - (Default) Optimal Channel by CTR - web push
        - (Default) Optimal Channel by CTR - email
        - (Default) Optimal Channel by CTR - sms

    Segment is based on scoring calculated in “optimal.channel” event. In its configuration we used the “Last” aggregate for ”optimal.channel” event with parameter “type:CTR”.

    - **Expressions** for all channels you want to use (e.g. email, sms, newsletter) 

    - **Metrics** for all channels you want to use (e.g. email, sms, newsletter) 

2. Create an example expression for a newsletter ratio CTR 

    In these analytics we have to calculate the ratio between clicked and sent messages, but in order to achieve a proper reference we require that those customers be sent at least 5 campaigns. It should look like below.

   ![Screenshot presenting campaign optimizer](/api/docs/image/54176ad07f146575310749eba44b7c2f42c1b327/use-cases/all-cases/_gfx/newsletterratio.png) 


For the calculation of the value of each channel, we can add the expression which meet the conditions described below: 

- Expressions for optimal individual channels are being compared if they are higher than the global metric​ 
- If the Expression is less than 5, then its value equals 0​ 
- If the Expression is lower than the metrics, then is not taken into consideration in comparison ​ 
- If all expressions are lower than metrics, then we randomly select a channel​ 
- If all expressions return 0, then we randomly select a channel 

It can look like this:


<pre><code class="language-jinja">&lt;!-- candidates --&gt;
{% set NEWSLETTER = {"name": "NEWSLETTER", "expression":0, "metric":0} %}
{% set MOBILE_PUSH = {"name": "MOBILE_PUSH", "expression":0, "metric":0}%}

{% set CANDIDATES = [NEWSLETTER, MOBILE_PUSH]%}

&lt;!-- value assignment --&gt;
{% expressionvar 37368423-6210-4ee4-bd67-5e2143544576 %}{% do
NEWSLETTER.update({"expression": expression_result})%}{%endexpressionvar%}
{metricsvar metric_id:550060f2-4feb-a6a5-46e8ad01b8ac%}{%do
NEWSLETTER.update({"metric_result})%}{% endmetricsvar %}
{% expressionvar 6e3206d9-708a-4fc5-bfe4-88fea18da90f %}{% do
MOBILE_PUSH.update({"expression_result})%}{% endexpressionvar %}
{% metricsvar metric_id:85651a3a-5163-4b1f-971a-4109e83bf753 %}{% do
MOBILE_PUSH.update ({"metric":metric_result})%}{% endmetricsvar %}

&lt;!-- candidates by expression--&gt;
{set expression_winners = [] %}
{% for candidat in CANDIDATES %}
    {% if candidat["expression"] &gt; candidat ["metric"] %}
        {% do expression_winners.append(candidat)%}
    {% endif %}
{% endfor %}

&lt;!-- print winner--&gt;
{% if expression_winners|count !=0 %}
    {% set winner = expression_winners|sort(true, true, 'expression')|first %}
{% else %}
    {% set winner = CANDIDATES|random %}
{% endif %}

{{winner}}</code></pre>


Everything here is based on jinjava, which produced the event at the very end of this calculation. Based on that we can actually select the segment in which we want to send the message automatically. 

## Read more
---
- [Aggregates](/docs/analytics/aggregates/introduction-to-aggregates)
- [Automation Hub](/docs/automation) 
- [Basic optimizer configuration](/use-cases/campaign-optimizer) 
- [Build segmentation](/docs/analytics/segmentations/creating-segmentations)
- [Expressions](/docs/analytics/expressions)
- [Metrics](/docs/analytics/metrics)








 