Skip to content

Commit 719bf82

Browse files
committed
feat: add lightweight privacy focused analytics
1 parent 9865567 commit 719bf82

File tree

20 files changed

+19629
-14
lines changed

20 files changed

+19629
-14
lines changed

.github/workflows/cloudflare-hosting-merge.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,26 @@ jobs:
3838
- name: Build project NOW
3939
run: npm run build
4040

41-
- name: Deploy to Cloudflare Pages
41+
- name: Build analytics dashboard
42+
run: |
43+
cd analytics-dashboard
44+
npm ci
45+
npm run build
46+
47+
- name: Deploy main site to Cloudflare Pages
4248
uses: cloudflare/pages-action@v1
4349
with:
4450
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
4551
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
4652
projectName: portfolio
4753
directory: build
54+
gitHubToken: ${{ secrets.GITHUB_TOKEN }}
55+
56+
- name: Deploy analytics dashboard to Cloudflare Pages
57+
uses: cloudflare/pages-action@v1
58+
with:
59+
apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }}
60+
accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
61+
projectName: analytics-dashboard
62+
directory: analytics-dashboard/build
4863
gitHubToken: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ yarn-error.log*
3737
.env.development.local
3838
.env.test.local
3939
.env.production.local
40+
.env.example
4041

4142
npm-debug.log*
4243
yarn-debug.log*

analytics-dashboard/SETUP.md

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
# Privacy-First Analytics System
2+
3+
A lightweight, privacy-focused analytics solution built for your portfolio website. No cookies, no personal data collection, no tracking - just essential insights to improve your content.
4+
5+
## 🏗️ Architecture
6+
7+
```
8+
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
9+
│ Your Website │───▶│ Analytics API │───▶│ Cloudflare R2 │
10+
│ (React Client) │ │ (Cloudflare CF) │ │ (Data Store) │
11+
└─────────────────┘ └──────────────────┘ └─────────────────┘
12+
13+
14+
┌──────────────────┐
15+
│ Analytics Dashboard│
16+
│ analytics.domain.com│
17+
└──────────────────┘
18+
```
19+
20+
## 🔒 Privacy Features
21+
22+
- **No Personal Data**: No cookies, no localStorage, no user identification
23+
- **IP Hashing**: One-way hash of IP addresses for rate limiting only
24+
- **Data Minimization**: Only collect essential metrics
25+
- **Automatic Expiry**: Data auto-expires after 90 days
26+
- **GDPR Compliant**: No consent banners needed
27+
- **Local First**: Works offline, fails gracefully
28+
29+
## 📊 What We Track
30+
31+
### Page Analytics
32+
- Page views and unique sessions
33+
- Time spent on pages
34+
- Navigation patterns
35+
- Error rates
36+
37+
### Blog Analytics
38+
- Reading progress (25%, 50%, 75%, 90%)
39+
- Time to read completion
40+
- Anonymous likes/reactions
41+
- Search queries (for content improvement)
42+
43+
### Technical Metrics
44+
- Load times and performance
45+
- Device type (mobile/tablet/desktop) - generalized
46+
- Browser compatibility issues
47+
48+
## 🚀 Setup Instructions
49+
50+
### 1. Cloudflare Worker Setup
51+
52+
1. Install Wrangler CLI:
53+
```bash
54+
npm install -g wrangler
55+
```
56+
57+
2. Authenticate with Cloudflare:
58+
```bash
59+
wrangler login
60+
```
61+
62+
3. Create R2 bucket:
63+
```bash
64+
wrangler r2 bucket create analytics-data
65+
```
66+
67+
4. Create KV namespace:
68+
```bash
69+
wrangler kv:namespace create "ANALYTICS_KV"
70+
wrangler kv:namespace create "ANALYTICS_KV" --preview
71+
```
72+
73+
5. Update `wrangler.toml` with your KV namespace IDs
74+
75+
6. Deploy the worker:
76+
```bash
77+
cd analytics-worker
78+
wrangler deploy
79+
```
80+
81+
### 2. Main Website Integration
82+
83+
1. Add environment variables to `.env`:
84+
```env
85+
REACT_APP_ANALYTICS_API=https://analytics-api.yourdomain.com/collect
86+
REACT_APP_ANALYTICS_ENABLED=true
87+
```
88+
89+
2. The analytics are already integrated into your App.js component
90+
91+
3. For blog posts, add to your blog post component:
92+
```javascript
93+
import { useBlogAnalytics } from '../../hooks/useAnalytics';
94+
95+
function BlogPost({ postId, title }) {
96+
const { trackLike, trackShare } = useBlogAnalytics(postId, title);
97+
98+
// Use trackLike() and trackShare() in your UI
99+
}
100+
```
101+
102+
### 3. Analytics Dashboard Setup
103+
104+
1. Deploy dashboard to a subdomain:
105+
```bash
106+
cd analytics-dashboard
107+
npm install
108+
npm run build
109+
```
110+
111+
2. Deploy build folder to `analytics.yourdomain.com`
112+
113+
3. Create API key for dashboard access (store securely)
114+
115+
## 🔧 Configuration
116+
117+
### Environment Variables
118+
119+
**Main Website (.env)**
120+
```env
121+
REACT_APP_ANALYTICS_API=https://analytics-api.yourdomain.com/collect
122+
REACT_APP_ANALYTICS_ENABLED=true
123+
```
124+
125+
**Cloudflare Worker**
126+
- Configure domain routing in `wrangler.toml`
127+
- Set up R2 bucket and KV namespace bindings
128+
- Add rate limiting and security settings
129+
130+
### Privacy Settings
131+
132+
The system is configured with privacy-first defaults:
133+
- 30 requests per minute rate limit per IP
134+
- No cross-site tracking
135+
- Minimal data collection
136+
- Automatic data expiration
137+
138+
## 📈 Dashboard Features
139+
140+
### Real-time Metrics
141+
- Live visitor count
142+
- Page views and unique visitors
143+
- Session duration
144+
- Top performing content
145+
146+
### Blog Analytics
147+
- Reading completion rates
148+
- Time spent reading
149+
- Popular posts
150+
- Search queries
151+
152+
### Privacy-Safe Insights
153+
- Geographic data (country level only)
154+
- Device types (generalized)
155+
- Traffic sources (cleaned referrers)
156+
- Performance metrics
157+
158+
## 🛡️ Security Considerations
159+
160+
1. **API Key Management**: Store dashboard API keys securely
161+
2. **Rate Limiting**: Prevents abuse and spam
162+
3. **Data Validation**: All inputs are sanitized
163+
4. **CORS Protection**: Only your domain can send data
164+
5. **No PII Storage**: Zero personally identifiable information
165+
166+
## 📊 Data Retention
167+
168+
- **Hot Storage**: 30 days in R2 for dashboard queries
169+
- **Cold Storage**: Archive older data or delete automatically
170+
- **Rate Limit Data**: 1 minute TTL in KV store
171+
- **Session Data**: Ephemeral, resets on page reload
172+
173+
## 🔄 Data Processing Pipeline
174+
175+
```
176+
User Action → Analytics Client → Cloudflare Worker → Data Validation → R2 Storage
177+
178+
Rate Limiting Check
179+
180+
IP Hash Generation
181+
182+
Event Processing
183+
```
184+
185+
## 🚦 Monitoring & Alerts
186+
187+
Set up Cloudflare Worker alerts for:
188+
- High error rates
189+
- Unusual traffic spikes
190+
- Storage quota warnings
191+
- API performance issues
192+
193+
## 🎨 Customization
194+
195+
### Adding New Events
196+
1. Extend the analytics client with new event types
197+
2. Update worker validation for new events
198+
3. Add dashboard visualization
199+
200+
### Custom Metrics
201+
Define custom events for your specific needs:
202+
```javascript
203+
analytics.trackCustomEvent('project_demo_view', {
204+
projectId: 'rust-game-engine',
205+
section: 'video_demo',
206+
timestamp: Date.now()
207+
});
208+
```
209+
210+
## 📄 Legal Compliance
211+
212+
This system is designed to be:
213+
- **GDPR Compliant**: No personal data collection
214+
- **CCPA Compliant**: No sale of personal information
215+
- **Cookie Law Compliant**: No cookies used
216+
- **Privacy-First**: Transparent data practices
217+
218+
## 🛠️ Development
219+
220+
### Local Testing
221+
```bash
222+
# Test analytics client
223+
npm start
224+
225+
# Test worker locally
226+
cd analytics-worker
227+
wrangler dev
228+
229+
# Test dashboard
230+
cd analytics-dashboard
231+
npm start
232+
```
233+
234+
### Debugging
235+
- Check browser network tab for analytics requests
236+
- Monitor Cloudflare Worker logs
237+
- Verify R2 bucket data structure
238+
239+
## 📞 Support
240+
241+
For issues or questions:
242+
1. Check the troubleshooting section
243+
2. Review Cloudflare Worker logs
244+
3. Verify environment configuration
245+
4. Test with browser developer tools
246+
247+
## 🎯 Roadmap
248+
249+
### Phase 1 (Current)
250+
- [x] Basic page view tracking
251+
- [x] Blog engagement metrics
252+
- [x] Privacy-safe data collection
253+
- [x] Real-time dashboard
254+
255+
### Phase 2 (Future)
256+
- [ ] A/B testing framework
257+
- [ ] Heatmap visualization
258+
- [ ] Advanced search analytics
259+
- [ ] Performance monitoring
260+
261+
### Phase 3 (Advanced)
262+
- [ ] ML-powered insights
263+
- [ ] Predictive analytics
264+
- [ ] Content recommendations
265+
- [ ] Advanced privacy features

0 commit comments

Comments
 (0)