
It is worth remembering how important it is to analyze what users view on the website, what subpages they visit and how much time they spend there. Behavioral data is the basis for creating a specific marketing strategy and appropriate personalization.  

Remember that even if the users do not add anything to the basket, they can be reminded of the products they most often visited by sending them a personalized email.

## Example of use - Electronic industry

A client from the electronics industry decided to make use of information about the products customers viewed. However, instead of sending them the products they viewed recently, they were sent most viewed products in general. 

<figure>
<img src="/api/docs/image/54176ad07f146575310749eba44b7c2f42c1b327/use-cases/all-cases/_gfx/email-products-visited.png" alt="Screenshot presenting personalized email with top products" class="full no-frame">
<figcaption> Email with top products </figcaption>
</figure>

If the customers were on the site but did not add anything to the cart, after the session ended, they would receive an email with the products they visited more than once - ordered from the most to the least frequently visited. 

## Prerequisites 
---

To implement this use case, you have to:
- Implement [tracking code](/docs/settings/tool/tracking_codes) on your website.
- Implement [OG Tags](/developers/web/og-tags).
- Import [product feed](/developers/product-feed).
- Create [product catalog](/docs/assets/catalogs).
- Integrate [transaction events](/developers/web/transactions-sdk).
- Have [add to cart event](/docs/assets/events).
- Create and set up your [email account](/docs/settings/configuration/email-account).
- Import your [subscriber's database](/docs/automation/actions/synerise-integrations/import-customers) to Synerise.

## Process
---

To create an email with products most frequently visited, perform the steps in the following order: 
1. [Prepare the aggregate](/use-cases/email-with-products-most-frequently-visited#prepare-the-aggregate).
2. [Prepare a product catalog](/use-cases/email-with-products-most-frequently-visited#prepare-a-product-catalog)
2. [Prepare the workflow](/use-cases/email-with-products-most-frequently-visited#prepare-the-workflow).

##  Prepare the aggregate
---
Build an aggregate that collects the SKUs of the most frequently visited products.

1. Go to <img src="/api/docs/image/54176ad07f146575310749eba44b7c2f42c1b327/icons/behavioral-data-hub-icon.svg" alt="Behavioral Data Hub icon" class="icon"> **Behavioral Data Hub > Live Aggregates > Create aggregate**.
2. As the aggregate type, select **TOP MULTI**.  
3. In the Size field, enter the number of recent events (for example, 10).
4. Select the event `page.visit` and the `retailer_part_no`.
5. Click button + where and add the condition that the `product:retailer_part_no` parameter is true. This will give us confidence that we only take into account the visited product pages.
5. Save your aggregate.

<figure>
   <img src="/api/docs/image/54176ad07f146575310749eba44b7c2f42c1b327/use-cases/all-cases/_gfx/emailfavoritesproducts1.png" alt="`Screenshot presenting aggregate with recently viewed product`" class="large">
   <figcaption> The aggregate with recently viewed product </figcaption>
   </figure>



## Prepare a product catalog 
---

The event page.visit contains the most important information about the product - its ID in the product:retailer_part_no parameter.
To add additional information about the product in the email template, such as a photo, price, link, you need a product catalog.
You can import your feed following these [instructions](/docs/settings/imports) or you can use Snrs-product-ogTag catalog which is automatically built from the product page og tags.



## Prepare the workflow
---

1. Go to <img src="/api/docs/image/54176ad07f146575310749eba44b7c2f42c1b327/icons/automation-hub-icon.svg" alt="Automation Hub icon" class="icon" > **Automation Hub > Workflows > New workflow**.
2. Add an **Profile Event** as a trigger node.
3. Select the **session.end** event.
   <figure>
    <img src="/api/docs/image/54176ad07f146575310749eba44b7c2f42c1b327/use-cases/all-cases/_gfx/emailfavoritesproducts3.png" alt="`Screenshot presenting using trigger`" class="large">
    <figcaption> Configuration of the Profile Event node </figcaption>
    </figure>

4. Click **Delay** to define the duration of delay - after the session ends, the system waits for the time defined in the Delay node before the email is sent.
   
   <figure>
    <img src="/api/docs/image/54176ad07f146575310749eba44b7c2f42c1b327/use-cases/all-cases/_gfx/emailfavoritesproducts4.png" alt="`Screenshot presenting delay`" class="large">
    <figcaption> Configuration of the Delay node </figcaption>
    </figure>

   
   <div class="admonition admonition-note"><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="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg></div><div class="admonition-body"><div class="admonition-content">

   Remember that event session.end appears 30 minutes after the last user activity on the website.

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


5. Add the **Generate Event** node. It generates an event on the customer's profile. In this case, the event will contain ID of the most frequently viewed products by a customer. 
    1. In the **Event name**, enter the name, for example `topvisited.products`.
    3. In the **Body**, enter the Jinjava code available below. Replace AGGREGATE_HASH with the hash of the aggregate you in the previous step. As a result you will analyze the products that the user has viewed, sort them in order of the most frequently viewed to the least viewed (products that have been viewed at least two times) and return them to the user’s card in this event.

   <figure>
    <img src="/api/docs/image/54176ad07f146575310749eba44b7c2f42c1b327/use-cases/all-cases/_gfx/emailfavoritesproducts5.png" alt="`Screenshot presenting sending Profile Event`" class="large">
    <figcaption> Generate Event </figcaption>
    </figure>

   
   <div class="admonition admonition-note"><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="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg></div><div class="admonition-body"><div class="admonition-content">

   An event created in this way will return all the products that the user saw at least twice within the time specified in your aggregate. The event with parameter will be returned in the form of the sku listed after the decimal point, e.g.: top: sku1, sku2, sku3.

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



   
   <details class="accordion"><summary>CHECK JINJA CODE</summary><div class="accordion-content"><pre><code class="language-jinja">{ "top": "{% set skus = [] %} {% set counter = [] %} {% set skusTmp = [] %} {% set skuDict = [] %} {% set sortDict = [] %} {% aggregate AGGREGATE_HASH %} {% for sku in aggregate_result|unique %}{% set k = [] %}{% for tmp in aggregate_result %}{% if sku == tmp %}{% do k.append('+') %}{% endif %}{% endfor %}{% do skuDict.append({'sku':sku,'counter':k|length }) %}{% endfor %}{% endaggregate %}{% for dictItem in skuDict %}{% do counter.append(dictItem.counter)%}{% endfor %}{% set sortCounter = counter|sort() %}{% for count in sortCounter %}{% for dictItem in skuDict %}{% if dictItem.counter == count &amp;&amp; dictItem.sku in skusTmp == false %}{% do skusTmp.append(dictItem.sku) %}{% do sortDict.append(dictItem) %}{% endif %}{% endfor %}{% endfor %}{% set top = [] %}{% set len = sortDict|length + 1 %} {% for i in range(len) %}{% set ind = sortDict|length - i %}{% set item = sortDict[ind] %}{% if sortDict[ind].counter &gt; 1 %}{% do top.append(sortDict[ind].sku) %}{% endif %}{% endfor %}{{ top|join(',')|trim }}" }</code></pre></div></details>


   <figure>
    <img src="/api/docs/image/54176ad07f146575310749eba44b7c2f42c1b327/use-cases/all-cases/_gfx/emailfavoritesproducts6.png" alt="`Screenshot presenting effect after implementing jinjava`" class="large">
    <figcaption> Effect after implementing jinjava </figcaption>
    </figure>
 
6. Add **Profile Event Filter**. This node allows you to wait for the event sent in the previous node. When the event appears on the customer's profile, it will be possible to refer to its parameters in the next node - here in **send.email**, you will refer to the "Top" parameter via `{{event.params.top}}`

    <figure>
   <img src="/api/docs/image/54176ad07f146575310749eba44b7c2f42c1b327/use-cases/all-cases/_gfx/emailfavoritesproducts7.png" alt="`Screenshot presenting Profile Event Filter`" class="large">
   <figcaption> Configuration of the Profile Event Filter node </figcaption>
   </figure>

7. Add the **Send Email** node. In the message template, we can use {{event.params.top}} which will return the "top" parameter of the last event we sent. For example, sku1, sku2, sku3 which can then be converted into an array using the | split (',') function and use in the template structure.

   
   <pre><code class="language-jinja">{% set topProducts = event.params.top %} 
      {% set skus = topProducts|split(',') %}</code></pre>


   Additionally - to add the most visited products , you will have to use Jinjava and add your own CSS. If you want to display all objects that are under the key products, follow the instruction:

   
   <pre><code class="language-jinja">&lt;!-- Opening the tag that retrieves the value from the aggregate prepared in point 1 --&gt;
   {% aggregate XXXXXXXXXXX %}
   &lt;!-- In the section {% for r in aggregate_result %} ... {% endfor %} there is access to product:retailer_part_no of the 10 most visited products --&gt;
   {% for r in aggregate_result %}
   next item:
   {{r}}
   &lt;!-- r is a single product:retailer_part_no and can be used as the key: {% catalog.Snrs-produktu-ogTag(r).og:XXX %} downloads additional data from a catalog built on the basis of og tags - which information about the product you want to add in the template depends on you. If you want to add the product name, photo, link, price in the e-mail - you can take it from the catalog --&gt;
   {% catalog.Snrs-produktu-ogTag(r).og:image %}
   {% catalog.Snrs-produktu-ogTag(r).og:title %}
   {% catalog.Snrs-produktu-ogTag(r).product:price:amount %}
   {% catalog.Snrs-produktu-ogTag(r).og:url %}
   {% endfor %}
   &lt;!-- Closing of the tag that gets the value from the aggregate prepared in point 1 --&gt;
   {% endaggregate %}</code></pre>


   
   <div class="admonition admonition-note"><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="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg></div><div class="admonition-body"><div class="admonition-content">

   To find the aggregate ID to replace XXX in the code, simply navigate to the aggregate in the Synerise application. The ID is the part of the URL that comes after /aggregates/, for example: **bfba46b4-e0d6-3ea9-8ae6-c7a2495c54c7** in the URL `https://app.synerise.com/analytics-v2/aggregates/bfba46b4-e0d6-3ea9-8ae6-c7a2495c54c7`. Copy this ID and use it in your code where needed.

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


8. Add final settings:
- Add the End nodes where the workflow should finish for users.
- Specify capping (here 1 for 1 day).
- Optionally add titles to each node so the workflow will be more understandable for your colleagues.
- Name the automation and Save it or Save & Run.
- Save your workflow.

  **Results:**
  <figure>
   <img src="/api/docs/image/54176ad07f146575310749eba44b7c2f42c1b327/use-cases/all-cases/_gfx/emailfavoritesproducts2.png" alt="`Screenshot presenting automation`" class="full">
   <figcaption> Workflow  </figcaption>
   </figure>


  <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">

  **It will result in an array:**

   skus = [sku1, sku2, sku3] which we can then use to build a template - we can, for example, download items from the product catalog and simply display them in our email or download similiar recommendations for the most viewed item (skus [0])

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



  <div class="admonition admonition-note"><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="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg></div><div class="admonition-body"><div class="admonition-content">

  Based on this workflow, you can also extract the user's favorite brand (by creating an aggregate that collects the brand parameter transferred in page visits, adding products to the cart or transactions).

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


  ## Check the use case set up on the Synerise Demo workspace
  ---
  You can check the configuration of each step directly in the Synerise Demo workspace:  

- [Aggregate](https://app.synerise.com/analytics/aggregates/bfba46b4-e0d6-3ea9-8ae6-c7a2495c54c7)
- [Workflow](https://app.synerise.com/automations/workflows/automation-diagram/e2685ff9-390e-47c7-805f-377f35f49bc4)

If you’re our partner or client, you already have automatic access to the **Synerise Demo workspace (1590)**, where you can explore all the configured elements of this use case and copy them to your workspace.  

If you’re not a partner or client yet, we encourage you to fill out the contact [form](https://demo.synerise.com/request) to schedule a meeting with our representatives. They’ll be happy to show you how our demo works and discuss how you can apply this use case in your business.


## Read more
---
- [Aggregates](/docs/analytics/aggregates/creating-aggregates)
- [Email template](/docs/campaign/e-mail/creating-email-templates)
- [Jinjava inserts](/developers/inserts/insert-usage)
- [Product feed to catalogs](/use-cases/import-product-feed-to-catalog)
- [Workflow](/docs/automation/creating-automation)
