Back to Blog

Monitoring with PostHog, Google Analytics, and Axiom

Analytics
Monitoring
Performance
Next.js
TypeScript
Error Tracking

Understanding user behavior, tracking errors, and monitoring performance are essential for any production SaaS application. From PostHog for product analytics to Google Analytics for traffic insights and Axiom for server-side logging, setting up a comprehensive monitoring system requires integrating multiple services and weeks of careful development. Fastack comes with a complete, multi-provider monitoring package that handles analytics, error tracking, and logging—saving you weeks of development time.

Why comprehensive monitoring matters

Production monitoring is essential for understanding your application's health and user behavior:

  • Track user behavior and product usage
  • Identify and debug errors quickly
  • Monitor application performance
  • Make data-driven product decisions
  • Detect issues before users report them

What you get with Fastack

Fastack includes a complete @saas/monitoring package that provides:

PostHog product analytics

Fastack includes complete PostHog integration for product analytics:

  • Event tracking - Track user actions and product events
  • User identification - Link events to specific users
  • Feature flags - A/B testing and gradual rollouts
  • Session recordings - Watch user sessions for UX insights
  • Automatic pageview tracking - Track navigation automatically
import { useTrackEvent } from '@saas/monitoring/hooks';

function CheckoutButton() {
  const { trackEvent } = useTrackEvent();
  
  const handleCheckout = () => {
    trackEvent({
      name: 'checkout_started',
      properties: {
        plan: 'pro',
        price: 29.99,
      },
    });
    // ... checkout logic
  };
  
  return <button onClick={handleCheckout}>Checkout</button>;
}

Google Analytics integration

Fastack includes Google Analytics 4 (GA4) integration for web analytics:

  • Pageview tracking - Automatic pageview tracking on route changes
  • Event tracking - Track custom events and conversions
  • User identification - Link analytics to authenticated users
  • Traffic analysis - Understand user acquisition and behavior
import { MonitoringProvider } from '@saas/monitoring/components';

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        <MonitoringProvider>
          {children}
        </MonitoringProvider>
      </body>
    </html>
  );
}

Google Analytics is automatically initialized and tracks pageviews on every route change, providing comprehensive traffic insights.

Axiom server-side logging

Fastack includes Axiom integration for structured server-side logging:

  • Structured logging - JSON-formatted logs with context
  • Log levels - DEBUG, INFO, WARN, ERROR with filtering
  • Request logging - Automatic request/response logging
  • Error tracking - Comprehensive error logging with stack traces
  • Context metadata - Include user ID, request path, and custom context
packages/monitoring/server/logger.ts
import { createLogger } from '@saas/monitoring/server';

export async function POST(request: Request) {
  const logger = createLogger({
    path: '/api/users',
    method: 'POST',
  });
  
  try {
    logger.request('Creating user', { email: '[email protected]' });
    // ... create user logic
    logger.success('User created', { userId: 'user_123' });
  } catch (error) {
    logger.error('Failed to create user', {}, error);
    throw error;
  }
}

All server-side logs are automatically sent to Axiom for centralized log management and analysis, making debugging production issues much easier.

Multi-provider event tracking

Fastack provides a unified event tracking system that sends events to all enabled providers simultaneously:

  • Single API - Track events once, sent to all enabled providers
  • Type-safe events - TypeScript ensures event properties are correct
  • Automatic provider detection - Only sends to enabled providers
  • Error handling - Graceful fallback if a provider fails
import { useTrackEvent } from '@saas/monitoring/hooks';

function MyComponent() {
  const { trackEvent } = useTrackEvent();
  
  // This event is automatically sent to:
  // - PostHog (if enabled)
  // - Google Analytics (if enabled)
  trackEvent({
    name: 'button_clicked',
    properties: {
      button_name: 'subscribe',
      page: '/pricing',
    },
    userId: 'user_123', // Optional: link to user
  });
}

Error tracking and debugging

Fastack provides comprehensive error tracking:

  • Structured error logging - Errors include stack traces and context
  • Request context - Errors include request path, method, and user ID
  • Log aggregation - All errors centralized in Axiom
  • Development vs production - Color-coded logs in dev, JSON in production
import { createLogger } from '@saas/monitoring/server';

export async function handler(request: Request) {
  const logger = createLogger({
    path: '/api/payment',
    method: 'POST',
    userId: 'user_123',
  });
  
  try {
    // ... payment logic
  } catch (error) {
    // Error is automatically logged with:
    // - Full stack trace
    // - Request context (path, method, userId)
    // - Custom metadata
    logger.error('Payment processing failed', {
      amount: 29.99,
      plan: 'pro',
    }, error);
    
    throw error;
  }
}

User behavior analysis

Fastack enables comprehensive user behavior tracking:

  • User identification - Link events to authenticated users
  • User properties - Track user attributes and traits
  • Session tracking - Understand user journeys
  • Conversion funnels - Track user conversion paths
import { useIdentifyUser } from '@saas/monitoring/hooks';

function UserProfile({ user }) {
  const identifyUser = useIdentifyUser();
  
  useEffect(() => {
    // Identify user in all analytics providers
    identifyUser(user.id, {
      email: user.email,
      plan: user.subscription?.plan,
      createdAt: user.createdAt,
    });
  }, [user]);
  
  return <div>...</div>;
}

Performance monitoring

Fastack includes performance monitoring capabilities:

  • Request logging - Track API response times and status codes
  • Error rates - Monitor error frequency and patterns
  • User activity tracking - Understand feature usage
  • Custom metrics - Track business-specific metrics

Cookie consent integration

Fastack respects user privacy with cookie consent integration:

  • GDPR compliance - Only tracks analytics if user consents
  • Automatic initialization - Analytics providers only initialize after consent
  • Server-side logging - Axiom logging works independently (no cookies)

Time saved: 25+ hours

Building monitoring and analytics from scratch typically requires:

  • Setting up PostHog integration (3-4 hours)
  • Implementing Google Analytics (2-3 hours)
  • Configuring Axiom logging (2-3 hours)
  • Building unified event tracking system (4-6 hours)
  • Creating structured logging utilities (3-4 hours)
  • Implementing error tracking (2-3 hours)
  • Building React hooks for tracking (2-3 hours)
  • Setting up cookie consent integration (2-3 hours)
  • Testing across all providers (2-3 hours)

Total: 22-32 hours of development time that you save with Fastack.

Best practices included

Fastack follows monitoring best practices:

  • Privacy-first - Respects cookie consent and user privacy
  • Structured logging - JSON logs in production for easy parsing
  • Error handling - Monitoring failures never break the app
  • Type safety - TypeScript ensures correct event properties
  • Performance - Non-blocking logging and async event tracking

Conclusion

Comprehensive monitoring and analytics are essential for understanding your application's health, user behavior, and performance. Fastack provides a complete, production-ready monitoring system that saves you weeks of development time while ensuring you have the insights needed to build and improve your SaaS product.

With Fastack, you get:

  • Complete PostHog integration for product analytics and feature flags
  • Google Analytics 4 integration for traffic and conversion tracking
  • Axiom integration for structured server-side logging
  • Unified event tracking system that sends to all enabled providers
  • Comprehensive error tracking with stack traces and context
  • Type-safe React hooks for client-side tracking
  • Cookie consent integration for GDPR compliance
  • Environment-based configuration for easy setup

Ready to build your SaaS faster?

Get our scalable, production-ready boilerplate to save endless hours of development and setup

Monitoring with PostHog, Google Analytics, and Axiom - Fastack