PostHog Analytics
By Amr
Implement privacy-first web analytics in Jekyll using PostHog with GDPR/CCPA compliance, custom event tracking, and Do Not Track support.
Estimated reading time: 5 minutes
Table of Contents
PostHog Analytics
Implement privacy-first, GDPR-compliant analytics in your Jekyll site using PostHog with custom event tracking and Do Not Track support.
Overview
PostHog is an open-source product analytics platform that respects user privacy. Unlike traditional analytics (Google Analytics), PostHog offers:
- Self-hostable — full data ownership option
- Privacy-first — GDPR/CCPA compliant by design
- Do Not Track support — respects browser DNT settings
- Custom events — track any user interaction
- Session recordings — optional, with input masking
- Feature flags — A/B testing built-in
- Free tier — 1 million events/month free
Prerequisites
- PostHog account — Sign up at posthog.com
- Project API key — Found in Project Settings
- Jekyll site in production environment
Configuration
Step 1: Configure _config.yml
Add the PostHog configuration block:
# PostHog Analytics Configuration
posthog:
enabled: true
api_key: 'phc_YOUR_API_KEY_HERE'
api_host: 'https://us.i.posthog.com' # or eu.i.posthog.com for EU
# Automatic tracking
autocapture: true
capture_pageview: true
capture_pageleave: true
# Privacy settings
session_recording: false
respect_dnt: true
# Custom event tracking
custom_events:
track_downloads: true
track_external_links: true
track_search: true
track_scroll_depth: true
Step 2: Disable in Development
In _config_dev.yml, disable analytics for local development:
posthog:
enabled: false
Custom Event Tracking
File Downloads
Track when users download PDFs, ZIPs, and other files:
document.addEventListener('click', function(e) {
var target = e.target.closest('a');
if (target && target.href) {
var href = target.href.toLowerCase();
var downloadExts = ['.pdf', '.zip', '.doc', '.xlsx'];
var isDownload = downloadExts.some(ext => href.includes(ext));
if (isDownload) {
posthog.capture('file_download', {
'file_url': target.href,
'file_name': target.href.split('/').pop()
});
}
}
});
External Links
Track clicks to external websites:
document.addEventListener('click', function(e) {
var target = e.target.closest('a');
if (target && target.href && target.hostname !== window.location.hostname) {
posthog.capture('external_link_click', {
'link_url': target.href,
'link_text': target.innerText
});
}
});
Scroll Depth
Track how far users scroll:
var scrollDepths = [25, 50, 75, 90];
var triggeredDepths = [];
window.addEventListener('scroll', function() {
var scrollPercent = Math.round(
(window.scrollY / (document.documentElement.scrollHeight - window.innerHeight)) * 100
);
scrollDepths.forEach(function(depth) {
if (scrollPercent >= depth && !triggeredDepths.includes(depth)) {
triggeredDepths.push(depth);
posthog.capture('scroll_depth', { 'depth_percentage': depth });
}
});
});
Privacy Compliance
GDPR/CCPA Compliance
- Cookie consent integration — Only load PostHog after user consent
- Disable cookies — Set
disable_cookie: truefor cookieless tracking - IP anonymization — Enable
ip_anonymization: true - Session recordings — Keep
session_recording: falseunless needed - Data retention — Configure in PostHog dashboard
Do Not Track Support
The implementation respects browser DNT settings:
if (navigator.doNotTrack === '1') {
console.log('PostHog: Respecting Do Not Track setting');
// PostHog not loaded
}
Troubleshooting
Analytics Not Loading
- Check environment — Must be
production, notdevelopment - Verify API key — Ensure key is correct in
_config.yml - Check browser console — Look for PostHog errors
- Test DNT setting — Try with DNT disabled
Events Not Appearing
- Wait a few minutes — Events can be delayed
- Check PostHog dashboard — Events → Live Events
- Verify autocapture — Ensure
autocapture: true - Check custom event code — Console log to debug
High Event Volume
If exceeding free tier limits:
- Disable
autocapture(captures many events) - Reduce
track_scroll_depthgranularity - Limit
session_recordingto specific pages - Use sampling in PostHog dashboard
Comparison with Google Analytics
| Feature | PostHog | Google Analytics |
|---|---|---|
| Privacy-first | Yes | Limited |
| Self-hostable | Yes | No |
| DNT support | Yes | No |
| Session recordings | Built-in | No |
| Free tier | 1M events/mo | 10M hits/mo |
| Data ownership | Full | Google-owned |
Further Reading
This guide is part of the Zer0-Mistakes Jekyll Theme documentation.