@@ -5,6 +5,7 @@ This guide walks you through setting up Plausible Analytics with a Cloudflare Wo
55## Overview
66
77The setup consists of three parts:
8+
891 . Plausible dashboard configuration
9102 . Cloudflare Worker proxy deployment
10113 . SvelteKit app configuration
@@ -43,19 +44,22 @@ The setup consists of three parts:
43442. Delete the default code
44453. Copy the code from `cloudflare-worker-plausible-proxy.js`
45464. **Update the configuration** at the top of the file:
47+
4648 ```javascript
4749 // Replace with your Plausible script URL from Part 1
4850 const ProxyScript = 'https://plausible.io/js/pa-XXXXX.js';
4951
5052 // 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+ const ScriptName = '/metrics/script.js'; // Change to something unique
54+ const Endpoint = '/metrics/event'; // Should match folder above
5355 ```
56+
54575 . Click "Save and Deploy"
5558
5659### Step 3: Test the Worker
5760
58611 . Your worker will be available at:
62+
5963 ```
6064 https://your-worker-name.your-account.workers.dev
6165 ```
@@ -78,6 +82,7 @@ If your site is on Cloudflare CDN, you can run the proxy as a subdirectory:
78824 . Click "Save"
7983
8084Now your proxy will be available at:
85+
8186```
8287https://yourdomain.com/analytics/metrics/script.js
8388https://yourdomain.com/analytics/metrics/event
@@ -91,24 +96,25 @@ Open `apps/demo/src/routes/+layout.svelte` and update the TODO section:
9196
9297``` typescript
9398onMount (() => {
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+ initPlausible ({
100+ domain: ' demo.pgflow.dev' , // Your actual domain
101+ apiHost: ' https://your-worker.workers.dev/metrics' , // Your proxy URL
102+ trackLocalhost: false // Set to true for testing locally
103+ });
99104});
100105```
101106
102107** Configuration options:**
103108
104109- ** Without custom route** (worker URL):
110+
105111 ``` typescript
106- apiHost : ' https://your-worker-name.your-account.workers.dev/metrics'
112+ apiHost : ' https://your-worker-name.your-account.workers.dev/metrics' ;
107113 ```
108114
109115- ** With custom route** (subdirectory):
110116 ``` typescript
111- apiHost : ' /analytics/metrics' // Relative path works!
117+ apiHost : ' /analytics/metrics' ; // Relative path works!
112118 ```
113119
114120## Part 4: Track Custom Events
@@ -123,19 +129,23 @@ track('button_clicked');
123129
124130// Event with properties
125131track (' signup' , {
126- tier: ' pro' ,
127- plan: ' monthly' ,
128- source: ' landing_page'
132+ tier: ' pro' ,
133+ plan: ' monthly' ,
134+ source: ' landing_page'
129135});
130136
131137// Event with revenue tracking
132- track (' purchase' , {
133- product: ' pro-plan' ,
134- quantity: 1
135- }, {
136- amount: 29.99 ,
137- currency: ' USD'
138- });
138+ track (
139+ ' purchase' ,
140+ {
141+ product: ' pro-plan' ,
142+ quantity: 1
143+ },
144+ {
145+ amount: 29.99 ,
146+ currency: ' USD'
147+ }
148+ );
139149```
140150
141151### Common Event Examples
@@ -158,11 +168,13 @@ track('documentation_download', { doc_type: 'api_reference' });
158168## Part 5: Verify Installation
159169
1601701 . ** Start your dev server** :
171+
161172 ``` bash
162173 pnpm nx dev demo
163174 ```
164175
1651762 . ** Open your browser console** and look for:
177+
166178 ```
167179 [Plausible] Initialized successfully
168180 ```
@@ -173,10 +185,12 @@ track('documentation_download', { doc_type: 'api_reference' });
173185 - Note: It may take a few seconds for events to appear
174186
1751874 . ** Test custom events** :
188+
176189 ``` typescript
177190 // In browser console or your code
178191 track (' test_event' , { test: true });
179192 ```
193+
180194 - Check Plausible dashboard > Custom Events to see it
181195
182196## Troubleshooting
0 commit comments