Demand Forecasting Solutions using Data Analytics and AI

This is the second in the blog series, which we started with Data-Driven Decisions in retail commerce. Here we will dive into ‘Demand forecasting’ one of the business cases we listed earlier.

Inventory management will be the focus of key areas of improvement for any retailer. With the supply chain problem inhibiting the supply side and pandemic impacting the demand side, it feels like a perfect storm disrupting the retail operations.

Hence the importance of demand forecasting is increasing as it focuses on bringing a balance between supply and demand. As the technology improves, the forecast will be more precise, and the prediction rate will be accurate.

Google provides the necessary tools to help the retail ecosystem to improve the accuracy of demand forecasting. It continues to upgrade the ML/AI services that can be used to improvise predictions. Here is a reference architecture for using Google Analytics Data, BigQuery, and the Forecasting model in Vertex AI to derive the predictions for demand forecast problems:

Reference architecture for Forecasting model in Vertex AI

SpringML in partnership with Google Cloud can provide Retail solutions that can address any business problems or help in analyzing the current state and provide actionable insights.

Business case – Demand forecasting

Traditionally retailers did not have easy access to ML models to predict the user’s behavior. So the forecasts were based on simpler models. With the rise of data, there is an increase in available data points that brought in the required maturity in the Analytics and ML methodologies. These changes provide few options to rethink the way traditional forecasting was done. We are using one such option here that utilizes Advanced Analytics and AI/Ml Models to solve the Demand forecasting problem.
Google’s Vertex AI provides an AutoML forecasting model that uses relevant time series data and predicts the future data in the same series. There are a few advantages of using this method over using the ARIMA model on BQML:

  1. Easy to use Vertex AI interface provides few clicks to create and deploy the forecasting model
  2. Vertex AI approach can be used for near real-time responses
  3. It provides more optimization techniques during forecast model evaluation

Let us consider a retailer who has enabled analytics (GA 4) on the eCommerce website. The clickstream data includes eCommerce events that provide data points to understand the user behavior better during checkout. Here, we will consider the ‘begin_checkout’ event that shows the user’s intent to buy a product. These data points can be fed into the forecasting model to provide a more accurate prediction for checkout events on the site. Thus, improving the inventory management and, in turn, the user experience by adjusting the inventory before the user clicks on checkout.

Vertex AI Forecasting Model

As a data preparation step, use the “BigQuery Linking” option on the Google Analytics console to export the analytics data into the BQ. We require the web data stream to be enabled to source the necessary data. Also, this demo lets us only enable the batch/daily data export to keep it simple.

Please refer to the Google support documentation for the setup instructions.

Google provides sample data for public usage. We can use the GA events table data in the ga4_obfuscated_sample_ecommerce dataset.

This case study can be divided into 2 Parts:

  1. Prepare data source using the ‘begin_checkout’ event data from GA and transform it to the required format in BQ
  2. Create & test a Forecasting Model in Vertex AI and schedule daily batch predictions using this model

Here are the steps in detail:

Part I – Prepare Data Source

Prepare the source data required for the forecast model. For simplicity, we consider ‘public holidays’ as a covariate that drives website traffic to identify the impact on the sales.

Extract the required dataset from the ga4_obfuscated_sample_ecommerce public data as shown below. Let’s consider a popular product ‘​​Google Flat Front Bag Grey’ with id ‘9188227’ for this case study. The query below will pull the number of times this product was added to the cart per day using the GA4 ‘begin_checkout’ event.

Part I - Prepare Data Source

The above data would need some clean up to make sure we have data points with equal time intervals and including the holiday flag. So the massaged data would look like this:

Part I -Prepare Data Source-Massaged data

Part II – Forecasting Model

Using the Forecasting model in Vertex AI to predict the number of checkout events triggered can be done in the following steps:

1.Create Dataset

Part II - Forecasting Model - Creating Dataset

2.Upload Data Source

The CSV file created initially from BQ can be uploaded to the cloud storage bucket or directly uploaded to the dataset created in the above step


Upload Data Source-Option 2

3.Configure and train the model

Configure and train the model Figure 1
Configure and train the model Figure 2
Configure and train the model Figure 3
Configure and train the model Figure 4
Configure and train the model Figure 5

The training would take an hour, and the forecast predictions for the test data would be in a BQ dataset.

Configure and train the model - BQ dataset

4.Predict Data

Configure and train the model Figure 1

Batch predictions would generate 2 tables in the provided project. It includes:

1.Prediction data
Batch predictions - Prediction Data
2. Error data – Confirm this is empty and the ML model did not error out.

How Analytics and AI can improve forecasting

This approach brings the 2 most popular data concepts into forecasting.

Analytics can capture the required user events in near real-time. In this case, when a user makes the first move to buy (initiate checkout flow), ‘begin_checkout’ event will be triggered. This provides the user’s intent to buy a product, which is the main data point to build the forecasting on.

This model would predict future ‘begin_checkout’ events and make sure the necessary inventory is available. Unlike the other forecasting techniques that use the actual sales number, here we are using the intent to buy which can enhance the user experience.

AI has evolved into mainstream usage due to its easy accessibility provided by services like Vertex AI. Machine learning models for regression, classification, and forecasting problems can be built, tested & predicted with just a few clicks. So this takes the complexity of building and managing an ML model so that users can concentrate on the actual problem.

The Forecasting model in Vertex AI makes it very easy to build a solution for the demand forecasting problem. It also provides easy options for training a model with optimization techniques like Minimize mean-absolute error (MAE), Root mean square error (RMSE), etc.

The main advantage here is the use of forecasting models, as they evolve continuously and would improve the forecast predictions over time.


The demand forecasting problem remains the same but we have more sophisticated options to solve it. We have reviewed one of the options to stitch in best-of-breed tools like Google Analytics, BigQuery and also make use of the Machine Learning models provided by Vertex AI.

As we solve one piece of the problem, it is clear that the next piece of the puzzle is to visualize to make sense of the data. We will look deeper into this in our next segment.

Thought Leadership