Templates/SaaS & accounts

Product analytics database schema

Users, sessions and tracked events with properties, plus saved funnels.

5 tables, 4 relationships

Opens as a new diagram you can change. Free.

Tables

tracked_users

  • iduuid not null
  • distinct_idtext not null
  • first_seen_attimestamptz not null
  • propertiesjsonb not null
  • created_attimestamptz not null

sessions

  • iduuid not null
  • user_id> tracked_usersuuid not null
  • started_attimestamptz not null
  • ended_attimestamptz
  • devicetext
  • created_attimestamptz not null

events

  • iduuid not null
  • user_id> tracked_usersuuid not null
  • session_id> sessionsuuid
  • nametext not null
  • occurred_attimestamptz not null
  • created_attimestamptz not null

event_properties

  • iduuid not null
  • event_id> eventsuuid not null
  • keytext not null
  • valuetext not null
  • created_attimestamptz not null

funnels

  • iduuid not null
  • nametext not null
  • stepstext[] not null
  • created_attimestamptz not null

As PostgreSQL

The same diagram exports to MySQL, MariaDB, SQLite, SQL Server and Oracle, and to Prisma, Drizzle, SQLAlchemy and Convex models.

CREATE TABLE "event_properties" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "event_id" UUID NOT NULL,
  "key" TEXT NOT NULL,
  "value" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "events" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "user_id" UUID NOT NULL,
  "session_id" UUID,
  "name" TEXT NOT NULL,
  "occurred_at" TIMESTAMPTZ NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "funnels" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "name" TEXT NOT NULL,
  "steps" TEXT[] NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "sessions" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "user_id" UUID NOT NULL,
  "started_at" TIMESTAMPTZ NOT NULL,
  "ended_at" TIMESTAMPTZ,
  "device" TEXT,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "tracked_users" (
  "id" UUID DEFAULT gen_random_uuid() NOT NULL,
  "distinct_id" TEXT NOT NULL UNIQUE,
  "first_seen_at" TIMESTAMPTZ NOT NULL,
  "properties" JSONB DEFAULT '{}' NOT NULL,
  "created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
  PRIMARY KEY ("id")
);

ALTER TABLE "sessions" ADD CONSTRAINT "fk_sessions_user_id" FOREIGN KEY ("user_id") REFERENCES "tracked_users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "events" ADD CONSTRAINT "fk_events_user_id" FOREIGN KEY ("user_id") REFERENCES "tracked_users" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

ALTER TABLE "events" ADD CONSTRAINT "fk_events_session_id" FOREIGN KEY ("session_id") REFERENCES "sessions" ("id") ON DELETE SET NULL ON UPDATE NO ACTION;

ALTER TABLE "event_properties" ADD CONSTRAINT "fk_event_properties_event_id" FOREIGN KEY ("event_id") REFERENCES "events" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;

More SaaS & accounts templates

Start with one diagram, free

One diagram with two branches, no card needed. Upgrade when your team needs more.