A/B split your users with Supabase

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • esm.sh

    A fast, smart, & global CDN for modern(es2015+) web development.

  • // Follow this setup guide to integrate the Deno language server with your editor: // https://deno.land/manual/getting_started/setup_your_environment // This enables autocomplete, go to definition, etc. import { serve } from "https://deno.land/[email protected]/http/server.ts" import { createClient, SupabaseClient } from "https://esm.sh/@supabase/supabase-js@2" serve(async (req) => { const supabaseClient = createClient( // Supabase API URL - env var exported by default. Deno.env.get('SUPABASE_URL') ?? '', // Supabase API ANON KEY - env var exported by default. Deno.env.get('SUPABASE_ANON_KEY') ?? '', ); // If you want to use Authentication // const { // data: { user }, // } = await supabaseClient.auth.getUser() const body = await req.json() const experiment = await getExpId(supabaseClient, body.exp_number); const variant = await getUserVariant(supabaseClient, experiment.id, body.user_id); return new Response( JSON.stringify({ variant }), { headers: { "Content-Type": "application/json" } }, ) }) async function storeUserVariant(supabaseClient: SupabaseClient, expId: string, userId: string, variant: string) { await supabaseClient .from('user_experiments') .insert({ experiment_id: expId, user_id: userId, variant }) } async function getExpId(supabaseClient: SupabaseClient, expNumber: number) { const { data, error } = await supabaseClient .from('experiments') .select('id') .eq('exp_number', expNumber) .limit(1) .maybeSingle(); if (error) throw error return data } async function getUserVariant(supabaseClient: SupabaseClient, expId: string, userId: string) { // OPTIONAL - you can use an extra call to see if the user has already been given a variant. But you could save the variant locally so you don't need to fetch it everytime let variant = await getUserStoredVariant(supabaseClient, expId, userId); if (variant) return variant; let expVariantCounts = await getCountsForExperimentNumber(supabaseClient, expId); variant = expVariantCounts.A >= expVariantCounts.B ? 'B' : 'A' await storeUserVariant(supabaseClient, expId, userId, variant); return variant; } async function getUserStoredVariant(supabaseClient: SupabaseClient, expId: string, userId: string) { const { data, error } = await supabaseClient .from('user_experiments') .select('variant') .eq('experiment_id', expId) .eq('user_id', userId) .limit(1) .maybeSingle(); if (error) throw error return data?.variant } async function getCountsForExperimentNumber(supabaseClient: SupabaseClient, expId: string) { const variantA = await supabaseClient .from('user_experiments') .select('experiment_id,variant', { count: 'exact', head: true}) .eq('experiment_id', expId) .eq('variant', 'A') const variantB = await supabaseClient .from('user_experiments') .select('experiment_id,variant', { count: 'exact', head: true}) .eq('experiment_id', expId) .eq('variant', 'B') if (variantA.error || variantB.error) throw variantA.error || variantB.error; return { A: variantA.count || 0, B: variantB.count || 0, }; }

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts