DEV Community

Alex Spinov
Alex Spinov

Posted on

PostHog Has a Free API — Here's How to Build Product Analytics Without Google Analytics

A product manager I know was flying blind. Google Analytics showed page views but not why users churned. She switched to PostHog — within a week she found that 73% of users dropped off at step 3 of onboarding. One UI change later, retention jumped 40%.

What PostHog Offers for Free

PostHog Cloud free tier:

  • 1 million events/month — enough for serious products
  • Product analytics — funnels, retention, paths, trends
  • Session replays — watch real users interact with your app (5K/month free)
  • Feature flags — A/B testing and gradual rollouts
  • Surveys — in-app feedback collection
  • Self-hosted option — unlimited events, forever free

Quick Start

<!-- Add to your HTML -->
<script>
  !function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src=s.api_host+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],u.toString=function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e},u.people.toString=function(){return u.toString(1)+".people (stub)"},o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures getActiveMatchingSurveys getSurveys".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
  posthog.init('YOUR_PROJECT_KEY', { api_host: 'https://app.posthog.com' });
</script>
Enter fullscreen mode Exit fullscreen mode

Track Events

// Track custom events
posthog.capture('purchase_completed', {
  product: 'Pro Plan',
  amount: 49.99,
  currency: 'USD',
  payment_method: 'stripe'
});

// Identify users
posthog.identify('user_123', {
  email: 'alice@example.com',
  plan: 'pro',
  company: 'Acme Inc'
});

// Track page views (automatic with SPA support)
posthog.capture('$pageview');
Enter fullscreen mode Exit fullscreen mode

PostHog API

# Query events
curl 'https://app.posthog.com/api/projects/YOUR_PROJECT_ID/events/?event=purchase_completed' \
  -H 'Authorization: Bearer YOUR_PERSONAL_API_KEY'

# Create a cohort
curl -X POST 'https://app.posthog.com/api/projects/YOUR_PROJECT_ID/cohorts/' \
  -H 'Authorization: Bearer YOUR_PERSONAL_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "name": "Power Users",
    "groups": [{
      "properties": [{
        "key": "plan",
        "value": "pro",
        "type": "person"
      }]
    }]
  }'

# Run a funnel query
curl -X POST 'https://app.posthog.com/api/projects/YOUR_PROJECT_ID/insights/funnel/' \
  -H 'Authorization: Bearer YOUR_PERSONAL_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{
    "events": [
      {"id": "$pageview", "name": "Page View"},
      {"id": "signup_started", "name": "Signup Started"},
      {"id": "signup_completed", "name": "Signup Completed"}
    ],
    "date_from": "-30d"
  }'
Enter fullscreen mode Exit fullscreen mode

Feature Flags

// Check feature flag
if (posthog.isFeatureEnabled('new-checkout')) {
  showNewCheckout();
} else {
  showOldCheckout();
}

// Get flag payload (for configuration)
const config = posthog.getFeatureFlagPayload('pricing-experiment');
// Returns: { price: 29.99, cta: "Start Free Trial" }

// Server-side (Node.js)
const PostHog = require('posthog-node');
const client = new PostHog('YOUR_PROJECT_KEY');

const isEnabled = await client.isFeatureEnabled('new-checkout', 'user_123');
Enter fullscreen mode Exit fullscreen mode

Why PostHog Over Google Analytics

PostHog Google Analytics
Event-based from day 1 Retrofitting events
Session replays included Need separate tool
Feature flags built in Need LaunchDarkly/etc
Self-host option (privacy) Google owns your data
SQL access to raw data Limited export

Need to collect competitor analytics data? Check out my web scraping actors on Apify — automated data collection from any website.

Need a custom analytics pipeline? Email me at spinov001@gmail.com.

Top comments (0)