Boosting Online Shopper Loyalty: Using ClickHouse, TensorFlow, and Google Cloud AI for Effective Discount Plans

Boosting Online Shopper Loyalty: Using ClickHouse, TensorFlow, and Google Cloud AI for Effective Discount Plans


3 min read

Integrating ClickHouse with TensorFlow and Google Cloud AI Platform enables an e-commerce platform to utilize a robust stack for machine learning tasks, such as dynamically offering discounts to users at risk of churn. This approach taps into ClickHouse's efficient data handling, TensorFlow's advanced machine learning capabilities, and AI Platform's scalable model deployment services. Below is an actionable strategy with examples for implementing such an integration:

1. Data Preparation in ClickHouse

  • Data Collection: Assume you collect user data in ClickHouse tables, tracking actions like "item added to cart," "item purchased," and "login event."

  • Feature Engineering Example: Create features such as days_since_last_purchase and average_purchase_value using ClickHouse SQL queries:

    now() - max(PurchaseDate) AS days_since_last_purchase,
    avg(PurchaseValue) AS average_purchase_value
FROM purchases

2. Exporting Data for ML

  • Data Export Example: Export the dataset from ClickHouse to CSV for TensorFlow processing. This might involve a command line utility or script to run the SQL query and save the output.
clickhouse-client --query="SELECT * FROM user_features" FORMAT CSV > user_features.csv

3. Model Development with TensorFlow

  • TensorFlow Model Training Example: Develop a neural network using TensorFlow to predict churn based on your features. Here's a simplified example:
import tensorflow as tf
from tensorflow.keras import layers, models

# Load your data
dataset =

# Define your model
model = models.Sequential([
  layers.Dense(64, activation='relu', input_shape=(num_features,)),
  layers.Dense(1, activation='sigmoid')


# Train the model, epochs=10)

4. Model Deployment and Inference on AI Platform

  • Deploy to AI Platform Example: After training, deploy your model to AI Platform for real-time predictions. First, export your TensorFlow model:'my_model')

Then, use gcloud CLI to deploy the model:

gcloud ai-platform models create my_churn_model
gcloud ai-platform versions create v1 --model my_churn_model --origin gs://my_bucket/my_model/
  • Automate Model Inference: Set up a process where current user data is periodically scored by the deployed model. This could involve exporting updated user features to Google Cloud Storage and invoking AI Platform Prediction:
gcloud ai-platform predict --model my_churn_model --version v1 --json-instances new_user_features.json

5. Dynamic Discount Offer Strategy

  • Identify At-Risk Users and Offer Discounts: Based on prediction results, categorize users and dynamically create discount offers. For example, users with a churn probability above 0.8 receive a 20% discount code via email, which can be automated through your platform's marketing tools.

6. Monitoring and Iteration

  • Performance Tracking Example: Use ClickHouse to monitor the redemption rate of discount offers and subsequent changes in user behavior:
    countIf(Action = 'purchase') / countIf(Action = 'offer_sent') AS redemption_rate
FROM user_actions
GROUP BY DiscountCode;
  • Iterate and Optimize: Continuously refine your model and strategies based on the insights gained from data analysis in ClickHouse.


By systematically integrating ClickHouse for data management, TensorFlow for model development, and AI Platform for model deployment, an e-commerce platform can effectively predict user churn and engage at-risk users with personalized discounts, driving retention and enhancing customer value. This example workflow illustrates the power of leveraging best-of-breed technologies in a complementary fashion for advanced, data-driven decision-making.