Get sales metrics (example)

Script example for blogpost

Script stripe

by tristan795 ยท 3/30/2026

  • Submitted by tristan795 Bun
    Created 40 days ago
    1
    // Give sales metrics, and allow you to manipulate them on specific date ranges.
    2
    // We use mock data to keep things simple for this example
    3
    // But you can can easily create a script that gets this data from your own database
    4
    
    
    5
    export async function main() {
    6
      const today = new Date();
    7
      const results = [];
    8
    
    
    9
      // Sales values that create realistic patterns (weekends lower, some spikes for promotions)
    10
      const baseSales = [1200, 850, 720, 1150, 1180, 1220, 1190, 3850, 3620, 3290,
    11
                         1450, 1280, 890, 1100, 6420, 5890, 5240, 1520, 1380, 1050,
    12
                         920, 1180, 1240, 1210, 1290, 1350, 1180, 1220, 1340, 1410];
    13
    
    
    14
      for (let i = 29; i >= 0; i--) {
    15
        const date = new Date(today);
    16
        date.setDate(today.getDate() - i);
    17
        const dateStr = date.toISOString().split('T')[0];
    18
        results.push({
    19
          date: dateStr,
    20
          sales: baseSales[29 - i]
    21
        });
    22
      }
    23
    
    
    24
      return results;
    25
    }
    26