|
| 1 | +# Plausible Analytics Setup Guide |
| 2 | + |
| 3 | +This guide walks you through setting up Plausible Analytics with a Cloudflare Worker proxy for the pgflow demo app. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The setup consists of three parts: |
| 8 | +1. Plausible dashboard configuration |
| 9 | +2. Cloudflare Worker proxy deployment |
| 10 | +3. SvelteKit app configuration |
| 11 | + |
| 12 | +## Part 1: Plausible Dashboard Setup |
| 13 | + |
| 14 | +1. **Sign up or log in** to [Plausible](https://plausible.io) |
| 15 | + |
| 16 | +2. **Add your website** |
| 17 | + - Click "Add a website" |
| 18 | + - Enter your domain (e.g., `demo.pgflow.dev`) |
| 19 | + - Click "Add site" |
| 20 | + |
| 21 | +3. **Get your script URL** |
| 22 | + - Go to Site Settings > General > Site Installation |
| 23 | + - Find your unique script URL - it will look like: |
| 24 | + ``` |
| 25 | + https://plausible.io/js/pa-XXXXX.js |
| 26 | + ``` |
| 27 | + - **Save this URL** - you'll need it for the Cloudflare Worker |
| 28 | +
|
| 29 | +## Part 2: Cloudflare Worker Setup |
| 30 | +
|
| 31 | +### Step 1: Create the Worker |
| 32 | +
|
| 33 | +1. Go to your [Cloudflare Dashboard](https://dash.cloudflare.com) |
| 34 | +2. Navigate to "Workers & Pages" in the sidebar |
| 35 | +3. Click "Create" > "Create Worker" |
| 36 | +4. Give it a name (avoid words like 'analytics', 'tracking', 'stats') |
| 37 | + - Good examples: `proxy-service`, `data-relay`, `metrics-hub` |
| 38 | +5. Click "Deploy" |
| 39 | +
|
| 40 | +### Step 2: Configure the Worker |
| 41 | +
|
| 42 | +1. Click "Edit Code" |
| 43 | +2. Delete the default code |
| 44 | +3. Copy the code from `cloudflare-worker-plausible-proxy.js` |
| 45 | +4. **Update the configuration** at the top of the file: |
| 46 | + ```javascript |
| 47 | + // Replace with your Plausible script URL from Part 1 |
| 48 | + const ProxyScript = 'https://plausible.io/js/pa-XXXXX.js'; |
| 49 | +
|
| 50 | + // Customize these paths (avoid obvious names) |
| 51 | + const ScriptName = '/metrics/script.js'; // Change to something unique |
| 52 | + const Endpoint = '/metrics/event'; // Should match folder above |
| 53 | + ``` |
| 54 | +5. Click "Save and Deploy" |
| 55 | + |
| 56 | +### Step 3: Test the Worker |
| 57 | + |
| 58 | +1. Your worker will be available at: |
| 59 | + ``` |
| 60 | + https://your-worker-name.your-account.workers.dev |
| 61 | + ``` |
| 62 | + |
| 63 | +2. Test if the script is accessible: |
| 64 | + ``` |
| 65 | + https://your-worker-name.your-account.workers.dev/metrics/script.js |
| 66 | + ``` |
| 67 | + You should see JavaScript code (the Plausible script) |
| 68 | + |
| 69 | +### Step 4 (Optional): Add a Custom Route |
| 70 | + |
| 71 | +If your site is on Cloudflare CDN, you can run the proxy as a subdirectory: |
| 72 | + |
| 73 | +1. In the Worker settings, go to "Triggers" > "Routes" |
| 74 | +2. Click "Add route" |
| 75 | +3. Configure: |
| 76 | + - Route: `*yourdomain.com/analytics/*` (change "analytics" to something else) |
| 77 | + - Zone: Select your domain |
| 78 | +4. Click "Save" |
| 79 | + |
| 80 | +Now your proxy will be available at: |
| 81 | +``` |
| 82 | +https://yourdomain.com/analytics/metrics/script.js |
| 83 | +https://yourdomain.com/analytics/metrics/event |
| 84 | +``` |
| 85 | + |
| 86 | +## Part 3: SvelteKit App Configuration |
| 87 | + |
| 88 | +### Update the Layout File |
| 89 | + |
| 90 | +Open `apps/demo/src/routes/+layout.svelte` and update the TODO section: |
| 91 | + |
| 92 | +```typescript |
| 93 | +onMount(() => { |
| 94 | + initPlausible({ |
| 95 | + domain: 'demo.pgflow.dev', // Your actual domain |
| 96 | + apiHost: 'https://your-worker.workers.dev/metrics', // Your proxy URL |
| 97 | + trackLocalhost: false // Set to true for testing locally |
| 98 | + }); |
| 99 | +}); |
| 100 | +``` |
| 101 | + |
| 102 | +**Configuration options:** |
| 103 | + |
| 104 | +- **Without custom route** (worker URL): |
| 105 | + ```typescript |
| 106 | + apiHost: 'https://your-worker-name.your-account.workers.dev/metrics' |
| 107 | + ``` |
| 108 | + |
| 109 | +- **With custom route** (subdirectory): |
| 110 | + ```typescript |
| 111 | + apiHost: '/analytics/metrics' // Relative path works! |
| 112 | + ``` |
| 113 | + |
| 114 | +## Part 4: Track Custom Events |
| 115 | + |
| 116 | +Use the `track()` function anywhere in your SvelteKit app: |
| 117 | + |
| 118 | +```typescript |
| 119 | +import { track } from '$lib/analytics'; |
| 120 | + |
| 121 | +// Simple event |
| 122 | +track('button_clicked'); |
| 123 | + |
| 124 | +// Event with properties |
| 125 | +track('signup', { |
| 126 | + tier: 'pro', |
| 127 | + plan: 'monthly', |
| 128 | + source: 'landing_page' |
| 129 | +}); |
| 130 | + |
| 131 | +// Event with revenue tracking |
| 132 | +track('purchase', { |
| 133 | + product: 'pro-plan', |
| 134 | + quantity: 1 |
| 135 | +}, { |
| 136 | + amount: 29.99, |
| 137 | + currency: 'USD' |
| 138 | +}); |
| 139 | +``` |
| 140 | + |
| 141 | +### Common Event Examples |
| 142 | + |
| 143 | +```typescript |
| 144 | +// User signup |
| 145 | +track('signup', { method: 'email' }); |
| 146 | + |
| 147 | +// Feature usage |
| 148 | +track('flow_created', { flow_type: 'data_pipeline' }); |
| 149 | + |
| 150 | +// Form submission |
| 151 | +track('contact_form', { page: 'pricing' }); |
| 152 | + |
| 153 | +// Download tracking (already automatic with fileDownloads: true) |
| 154 | +// But you can track custom downloads: |
| 155 | +track('documentation_download', { doc_type: 'api_reference' }); |
| 156 | +``` |
| 157 | + |
| 158 | +## Part 5: Verify Installation |
| 159 | + |
| 160 | +1. **Start your dev server**: |
| 161 | + ```bash |
| 162 | + pnpm nx dev demo |
| 163 | + ``` |
| 164 | + |
| 165 | +2. **Open your browser console** and look for: |
| 166 | + ``` |
| 167 | + [Plausible] Initialized successfully |
| 168 | + ``` |
| 169 | + |
| 170 | +3. **Check Plausible dashboard**: |
| 171 | + - Go to your Plausible dashboard |
| 172 | + - You should see pageviews appearing in real-time |
| 173 | + - Note: It may take a few seconds for events to appear |
| 174 | + |
| 175 | +4. **Test custom events**: |
| 176 | + ```typescript |
| 177 | + // In browser console or your code |
| 178 | + track('test_event', { test: true }); |
| 179 | + ``` |
| 180 | + - Check Plausible dashboard > Custom Events to see it |
| 181 | + |
| 182 | +## Troubleshooting |
| 183 | + |
| 184 | +### Events not showing up |
| 185 | + |
| 186 | +1. Check browser console for errors |
| 187 | +2. Verify the proxy worker is accessible |
| 188 | +3. Check that `domain` in config matches your Plausible site exactly |
| 189 | +4. Make sure you're not on localhost (unless `trackLocalhost: true`) |
| 190 | + |
| 191 | +### Worker not accessible |
| 192 | + |
| 193 | +1. Verify the worker is deployed (check Cloudflare dashboard) |
| 194 | +2. Check the worker logs for errors |
| 195 | +3. Test the script URL directly in your browser |
| 196 | + |
| 197 | +### Ad blocker blocking requests |
| 198 | + |
| 199 | +1. Make sure you're using the proxy (not direct Plausible URLs) |
| 200 | +2. Avoid obvious path names in your worker configuration |
| 201 | +3. Use a custom route on your own domain |
| 202 | + |
| 203 | +## Production Checklist |
| 204 | + |
| 205 | +- [ ] Updated `ProxyScript` in worker with your Plausible script URL |
| 206 | +- [ ] Customized `ScriptName` and `Endpoint` to avoid detection |
| 207 | +- [ ] Deployed Cloudflare Worker successfully |
| 208 | +- [ ] Tested worker script is accessible |
| 209 | +- [ ] Updated `domain` in `+layout.svelte` with production domain |
| 210 | +- [ ] Updated `apiHost` in `+layout.svelte` with worker URL |
| 211 | +- [ ] Set `trackLocalhost: false` for production |
| 212 | +- [ ] Verified events appear in Plausible dashboard |
| 213 | +- [ ] Tested custom event tracking |
| 214 | + |
| 215 | +## Additional Resources |
| 216 | + |
| 217 | +- [Plausible Documentation](https://plausible.io/docs) |
| 218 | +- [Plausible NPM Package](https://www.npmjs.com/package/@plausible-analytics/tracker) |
| 219 | +- [Cloudflare Workers Documentation](https://developers.cloudflare.com/workers/) |
| 220 | +- [Custom Events Guide](https://plausible.io/docs/custom-event-goals) |
| 221 | +- [Revenue Tracking Guide](https://plausible.io/docs/ecommerce-revenue-tracking) |
0 commit comments