Skip to content

Conversation

Copy link

Copilot AI commented Jul 25, 2025

This PR implements a complete Payload CMS plugin that integrates with PostHog analytics, providing automatic tracking of collection changes and admin login events.

Features Implemented

🚀 Plugin Foundation

  • Complete TypeScript plugin scaffolding following Payload conventions
  • NPM package structure with proper build and type generation
  • Environment-based configuration with sensible defaults

📊 Collection Event Tracking

Automatically tracks all collection operations across your Payload CMS:

  • Create events: {collection_slug}_created
  • Update events: {collection_slug}_updated
  • Delete events: {collection_slug}_deleted

Each event includes:

{
  id: string,           // Document ID
  userId: string | null, // User performing the action
  data: object,         // Full document data
  timestamp: string     // ISO timestamp
}

🔐 Admin Login Tracking

Monitors admin user authentication with automatic user identification:

  • Event: admin_login
  • Properties: { userId, email, loginTime, timestamp }
  • Automatically identifies users in PostHog for enhanced analytics

⚙️ Configuration

Simple setup via environment variables:

# Required
POSTHOG_PROJECT_API_KEY=your_project_api_key

# Optional (defaults to EU instance)
POSTHOG_HOST=https://app.posthog.com

🛡️ Error Handling

  • Graceful degradation when PostHog is not configured
  • Comprehensive error logging without breaking your application
  • Safe try-catch blocks around all PostHog operations

Usage Example

import { buildConfig } from 'payload/config'
import { posthogPlugin } from 'payload-plugin-posthog'

export default buildConfig({
  // ... your existing config
  plugins: [
    posthogPlugin({
      // Optional: override environment variables
      // apiKey: 'your_api_key',
      // host: 'https://app.posthog.com'
    })
  ]
})

Development Setup

  • Includes /dev folder with example Payload configuration
  • Complete TypeScript setup with type definitions
  • Ready for npm publication

The plugin automatically hooks into all collections and auth-enabled collections without requiring any additional configuration, making it a true "plug and play" solution for PostHog analytics in Payload CMS.

Fixes #1.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Co-authored-by: SebastianSchuetze <7256168+SebastianSchuetze@users.noreply.github.com>
Copilot AI changed the title [WIP] Plugin foundation Implement complete PostHog plugin foundation with collection tracking and admin login events Jul 25, 2025
Copilot AI requested a review from SebastianSchuetze July 25, 2025 17:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Plugin foundation

2 participants