Visualizing Distributions

Let's explore a different use case: visualizing the distribution of customer purchase amounts across percentiles. This can help you understand the spending habits of your customers, identify the most valuable segments, and tailor your marketing or product offerings accordingly.

Visualizing Purchase Amount Distribution with Telemetry SDK (Using Percentiles)

In this guide, we'll walk you through the process of using the Telemetry SDK to log customer purchase amounts and visualize their distribution across percentiles. This approach allows you to understand the distribution of spending among your customers, highlighting which percentiles contribute the most to your revenue.

Prerequisites

  • A valid API key for Telemetry

  • Basic understanding of JavaScript and Node.js

  • An e-commerce platform or any system where customer purchases are tracked

Step 1: Install Telemetry SDK

First, you need to install the Telemetry SDK in your project. If you haven’t done so already, run the following command:

npm install telemetry-sh

Step 2: Initialize Telemetry

After installing the SDK, import and initialize Telemetry in your project. Replace YOUR_API_KEY with your actual Telemetry API key.

import telemetry from "telemetry-sh";

telemetry.init("YOUR_API_KEY");

Step 3: Log Purchase Amount Data

To visualize the distribution of customer purchase amounts, you need to log the amount each time a purchase is made. Here’s an example function to log this data:

const logPurchaseAmount = (customerId, purchaseAmount) => {
  telemetry.log("customer_purchases", {
    customer_id: customerId,        // The ID of the customer
    purchase_amount: purchaseAmount, // The purchase amount in dollars
    timestamp: new Date().toISOString()
  });
};

// Example usage
logPurchaseAmount("customer_123", 49.99);  // Log a purchase amount for a customer
logPurchaseAmount("customer_456", 120.50); // Log another purchase amount for a different customer

Step 4: Automate Purchase Amount Logging

You should set up your system to automatically log purchase amounts whenever a transaction is completed. This can be done by integrating the logging function into your checkout or payment processing system. Here’s a simplified example:

const processPurchase = (customerId, amount) => {
  // Additional logic for processing the purchase

  // Log the purchase amount
  logPurchaseAmount(customerId, amount);
};

// Example transactions
processPurchase("customer_123", 75.00);    // Log a purchase of $75
processPurchase("customer_789", 300.00);   // Log a purchase of $300

Step 5: Visualize Purchase Amount Distribution in Telemetry UI

Once you’ve logged sufficient data, you can use Telemetry’s UI to visualize the distribution of purchase amounts across percentiles.

  1. Create a Distribution Query:

    • In the Telemetry UI, navigate to the query section.

    • Use the following SQL-like query to generate percentile data:

      SELECT
        PERCENTILE_CONT(0.01 * series.i) WITHIN GROUP (ORDER BY purchase_amount) AS purchase_amount,
        series.i AS percentile
      FROM
        generate_series(0, 100) AS series(i),
        customer_purchases
      GROUP BY
        series.i
      ORDER BY
        series.i
    • This query generates a result set with one row for each percentile from 0 to 100, with the corresponding purchase amount.

  2. Visualize the Data:

    • In the UI, create a line chart with percentile on the X-axis and purchase_amount on the Y-axis.

    • This will give you a distribution curve showing how purchase amounts vary across different customer percentiles, helping you identify the spending patterns within your customer base.

Conclusion

By following these steps, you can effectively monitor and visualize the distribution of customer purchase amounts using the Telemetry SDK. This setup allows you to gain insights into customer spending behavior, which can inform your marketing strategies, product development, and customer segmentation.

Last updated