Installation
This page takes you from a fresh clone to events arriving in your dashboard. You deploy two things to Cloudflare, the API Worker and the dashboard, and then add the Swift package to your app.
How It Fits Together
| Part | Folder | What it does |
|---|---|---|
| Swift package | repository root (Package.swift, Sources/Oddlytics) | Queues events in your app and sends them to the Worker in batches. |
| API Worker | packages/worker | A Cloudflare Worker (built with Hono) that accepts events at /track, stores them in a D1 database, and answers the dashboard's queries. |
| Dashboard | packages/dashboard | A Next.js app exported as static files. It runs in the browser and calls the Worker directly. |
What You Need
- A Cloudflare account. The free tier is enough for personal projects.
- Node.js 18 or later.
- Wrangler, Cloudflare's CLI. The Worker installs it as a dev dependency, so
npx wranglerworks insidepackages/worker. You can also install it globally withnpm install -g wrangler. - Xcode, for the Swift package (Swift tools 5.9).
1. Clone the Repository
git clone https://github.com/OddOmens/Oddlytics.git
cd Oddlytics2. Deploy the API Worker
Run these from packages/worker. The database schema lives in schema.sql at the repository root, two folders up.
Install dependencies and sign in to Cloudflare:
bashcd packages/worker npm install npx wrangler loginCreate the D1 database:
bashnpx wrangler d1 create oddlytics-dbCopy the
database_idit prints.Open
packages/worker/wrangler.tomland replace thedatabase_idwith yours. Keep the binding nameDB:toml[[d1_databases]] binding = "DB" database_name = "oddlytics-db" database_id = "YOUR_DATABASE_ID"Create the tables in the deployed database:
bashnpx wrangler d1 execute oddlytics-db --remote --file=../../schema.sqlSet the API key as a Worker secret. Use a long random string, for example from
openssl rand -hex 32:bashnpx wrangler secret put AUTH_KEYEnter the key when prompted. Your apps and the dashboard send this key in the
X-API-KEYheader.Deploy:
bashnpx wrangler deployNote the Worker URL it prints, for example
https://oddlytics-worker.<your-subdomain>.workers.dev.Check that it responds:
bashcurl https://oddlytics-worker.<your-subdomain>.workers.dev/You should get
{"status":"ok","service":"oddlytics-api","version":"1.0.0"}.
TIP
The --remote flag sends the SQL to the database on Cloudflare. Without it, recent versions of Wrangler apply it to a local copy used by wrangler dev.
3. Deploy the Dashboard
The dashboard is a static export (output: 'export' in next.config.js), so it builds to plain files in packages/dashboard/out. Its settings are compiled in at build time.
Create
packages/dashboard/.env.local:iniNEXT_PUBLIC_API_URL=https://oddlytics-worker.<your-subdomain>.workers.dev NEXT_PUBLIC_API_KEY=your-auth-keyNEXT_PUBLIC_API_KEYmust match the Worker'sAUTH_KEY. The dashboard needs it to rename events, change app settings and delete data.Build:
bashcd packages/dashboard npm install npm run buildDeploy the
outfolder. Pick one:Cloudflare Pages with Wrangler:
bashnpx wrangler pages deploy out --project-name=oddlytics-dashboardCloudflare Pages from GitHub: create a Pages project from your repository with the build command
cd packages/dashboard && npm install && npm run build, the output directorypackages/dashboard/out, and the twoNEXT_PUBLIC_variables set as environment variables.Vercel: import the repository, set the root directory to
packages/dashboard, and add the two environment variables.
WARNING
Anything that starts with NEXT_PUBLIC_ is built into the dashboard's JavaScript, so anyone who can open the dashboard can read the API key. Put the dashboard behind a sign-in, as described next.
4. Protect the Dashboard
Cloudflare Zero Trust can put an email sign-in in front of a Pages site.
The quickest way:
- In the Cloudflare dashboard, go to Workers & Pages and open your
oddlytics-dashboardproject. - Go to Settings > Access Policy and click Enable Access Policy.
- Allow only your own email address.
Or, in Cloudflare Zero Trust, go to Access > Applications, click Add an Application, choose Self-hosted, enter your dashboard's domain, and add a policy that allows your email. If you use the .pages.dev address, you may need to add a custom domain to the Pages project first.
After that, visiting the dashboard asks for your email and sends you a one-time code.
5. Add the Swift Package to Your App
In Xcode, choose File > Add Package Dependencies...
Enter
https://github.com/OddOmens/Oddlyticsand add the Oddlytics library to your app target.Configure it once at launch and track an event:
swiftimport SwiftUI import Oddlytics @main struct MyApp: App { init() { Analytics.configure( endpoint: "https://oddlytics-worker.<your-subdomain>.workers.dev", apiKey: "your-auth-key", appId: "MyApp" ) } var body: some Scene { WindowGroup { ContentView() .onAppear { Analytics.track("app_opened") } } } }Run the app. Events are sent within a few seconds (see Batching and Retries), and the app appears in the dashboard's sidebar and Your Apps list.
The Swift SDK page covers everything else the package does.
Run Everything Locally
For development you can run the Worker and dashboard on your computer with a local D1 database.
Give the local Worker an API key. Copy the example file and set a key:
bashcd packages/worker cp .dev.vars.example .dev.varsiniAUTH_KEY=oddlytics-local-keyCreate the tables in the local database, then start the Worker:
bashnpx wrangler d1 execute oddlytics-db --local --file=../../schema.sql npm run devWrangler serves the Worker at
http://localhost:8787.In another terminal, start the dashboard:
bashcd packages/dashboard npm install npm run devWithout a
.env.local, the dashboard useshttp://localhost:8787and the keyoddlytics-local-key. Next.js prints the address to open, normallyhttp://localhost:3000.
To put test events in the local database, send them with curl as shown in the API Reference.