# fbt-walkthrough

## Frequently bought together

In this example, we’ll walk through building a ‘Frequently bought together’ feature to show in a mobile e-commerce app.

![Mockup](/files/-LDqY9WS_2p0OkwEymu2)

When a user views a ‘Simple Plant’ in our e-commerce app, we want to promote a set of products to encourage the user to spend more and hence increase the transaction value.

Therefore, the model that we’d like to build is:

Given a `product listing`, a set of `frequently bought together products` and a `specific user`, predict the `transaction value`.

Let’s say that we’re looking for the following features to train our initial model:

1. The product listing (`product_primary`)
2. Frequently bought together products (`fbt_1`, `fbt_2`, `fbt_3`)
3. Sessions in the past week (`session_index`)
4. Days since install (`days_since_install`)
5. User’s age (`age`)
6. User’s gender (`gender`)
7. User’s country (`country`)
8. User’s spend so far (`spend_to_date`)

and we will predict

1. User's spend in the current session (`transaction_value_this_session`)

Once we’ve trained and deployed the model, we query it with the model features and it returns a prediction for the transaction value in this session.

![Model query overview](/files/-LDqY9WhYo0XiAf7wUwc)

Doing it this way allows us to query the model multiple times with different `fbt_*` products, and use the ones that result in the highest `transaction_value_this_session`.

![Model query option1](/files/-LDqY9WkgX-vli3_GAOS)

![Model query option1](/files/-LDqY9WmOMeb_alQH4M5)

The model predicts a higher `transaction_value_this_session` for `simple_plant`, `complex_plant` and `plant_food`, so we’ll show that to the user instead of `simple_plant`, `pot` and `watering_can`.

### Raw data

We're assuming that we have the following raw data. These events are based on the Firebase SDK.

Session start event

```javascript
[
{
"event_name": "session_start",
"session_id": "72a039b9-6759-901a-cf3a-3354d21beed2",
"start_time": "2018-01-14T17:53:57",
"appInstanceId": "72a039b9-6759-901a-cf3a-3354d21beed2",
"age": "25-34",
"app_Store": "iTunes",
"app_Version": "3.4.2",
"country": "BR",
"device_brand": "Apple",
"device_category": "tablet",
"device_model": "iPad Pro",
"first_open_time": "2017-12-02T23:00:00",
"gender": "male",
"interests": "arts & entertainment, games",
"language": "pt-br",
"new/established": "established",
"os_version": "10.1"
}
]
```

Product page is viewed

```javascript
[
{
"event_name": "view_item",
"appInstanceId": "72a039b9-6759-901a-cf3a-3354d21beed2",
"session_id": "72a039b9-6759-901a-cf3a-3354d21beed2",
"item_id": "32",
"item_name": "simple_plant",
"item_category": "plants"
}
]
```

Purchase

```javascript
[
{
"event_name": "ecommerce_purchase",
"appInstanceId": "72a039b9-6759-901a-cf3a-3354d21beed2",
"session_id": "72a039b9-6759-901a-cf3a-3354d21beed2"’
"item_id": "32, 42, 94",
"currency": "usd",
"value": "12, 23, 5"
}
]
```

## Transforming data

To train the model, we are looking for data to be organized like this.

| Unique user\_id (appInstanceId) | product\_primary | fbt\_1 | fbt\_2 | fbt\_3 | session\_index | days\_since\_install | age | gender | country | spend\_to\_date | transaction\_value\_this\_session |
| ------------------------------- | ---------------- | ------ | ------ | ------ | -------------- | -------------------- | --- | ------ | ------- | --------------- | --------------------------------- |

Since user may not buy everything in one transaction, `fbt_1`, `fbt_2`, `fbt_3` are the top 3 products bought by the user in a 48 hour window (+- 24 hours) from the `product_primary`

![Anchor](/files/-LDqY9X2ymxgn22hGumz)

[Streaming BTS](https://github.com/productml/blurr/tree/1c9e96562bbb9fe68dfd195acd8c60d91a19fac5/docs/examples/frequently-bought-together/fbt-streaming-bts.yml)

[Window BTS](https://github.com/productml/blurr/tree/1c9e96562bbb9fe68dfd195acd8c60d91a19fac5/docs/examples/frequently-bought-together/fbt-window-bts.yml)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://productml.gitbook.io/blurr/docs/examples/frequently-bought-together/fbt-walkthrough.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
