Skip to main content
Signals implementation
Real-time personalization

Train and serve a purchase propensity model with Signals

Build a point-in-time correct training dataset from Signals attributes, train a model on it, and score live sessions with the same attributes.

Progress0%

Introduction

Open in Colab

In this tutorial you'll build a model that predicts whether an ecommerce session will end in a purchase, and score live sessions with it while the user is still browsing.

The hard part of a real-time model is usually not the model. It's making sure that the features it trains on are computed the same way as the features it sees in production. If you write a SQL pipeline for training and separate code for serving, the two drift apart, and the model quietly gets worse. Signals avoids this by using one definition for both:

  • The dataset builder turns an attribute group into a training table in your warehouse, computing each attribute as it was at each moment in the past
  • The same attribute group, once published, is computed in real time from your event stream and served through the Signals Profiles API

By the end you'll have:

  • A published attribute group with in-session shopping behavior, such as product views, cart additions, and checkout steps
  • A labeled training dataset built by the Signals dataset builder, from your historical events or from the sample
  • A gradient-boosted model trained and evaluated on that dataset
  • A /predict endpoint that fetches a session's live attributes from Signals and returns a purchase score

This tutorial is for data scientists and engineers who want to train models on Snowplow behavioral data. You'll write and run the code in your own Python environment. To run it all in Google Colab instead, use the Open in Colab badge, or download the notebook from GitHub. The notebook has the same steps, and uses the sample data unless you switch it to your own. To create the attribute group, you can use either the Python SDK or an AI assistant: the Snowplow Assistant in Console, or your own assistant connected to the Snowplow MCP server. It should take around 30 minutes.

How the pieces fit together​

The purchase_propensity attribute group you'll define is used by two Signals components. The dataset builder computes it from the history in your warehouse, for training. The streaming engine computes it from live events, and the Profiles API serves the result, for scoring. The model itself runs in your own code:

flowchart LR site["Your website"] -- "events" --> pipeline["Snowplow pipeline"] subgraph warehouse["Your Snowflake"] events[("atomic.events")] table[("Training table")] end subgraph signals["Signals"] builder["Dataset builder"] engine["Streaming engine"] profiles["Profiles API"] end subgraph yours["Your code"] notebook["Training notebook"] predict["/predict endpoint"] end pipeline --> events pipeline --> engine events --> builder builder --> table table --> notebook notebook -- "trained model" --> predict engine --> profiles profiles -- "live attributes" --> predict

Each row in the training table is a moment in a past session, called an anchor. The row has the attribute values as they were at that moment, and a label that says whether the session went on to buy. At serving time, the live attribute values for a session describe the same moment: "now". The model sees the same columns in both cases.

Prerequisites​

You can follow the tutorial with your own ecommerce data, or with a sample dataset if you don't have any. The sample is a training table built from the public Google Analytics 4 Merchandise Store sample, converted to Snowplow events, and a script that sends shopper events in place of a website.

This route assumes that you have:

  • Signals enabled with a Snowflake warehouse connection, because the dataset builder runs its queries in your Snowflake account
  • Snowplow ecommerce events from a website in your Snowflake atomic.events table, including product_view, add_to_cart, and transaction actions, so the model has purchases to learn from: you'll check whether you have enough in Build the dataset
  • A website that sends those events to your pipeline now, so you can watch scores for a live session
  • Snowflake credentials that can read the schema Signals writes to, to load the training table
  • Python 3.10 or later, and basic familiarity with pandas and scikit-learn

Install the Python packages you'll use:

bash
pip install "snowplow-signals>=0.4.9" "snowflake-connector-python[pandas]" scikit-learn joblib fastapi uvicorn

With the sample data, you can leave out snowflake-connector-python[pandas] and install snowplow-tracker instead.

On this page

Want to see a custom demo?

Our technical experts are here to help.