Skip to content

Feat/posthog analytics#2

Open
aman-wednesdaysol wants to merge 3 commits into
itunesfrom
feat/posthog-analytics
Open

Feat/posthog analytics#2
aman-wednesdaysol wants to merge 3 commits into
itunesfrom
feat/posthog-analytics

Conversation

@aman-wednesdaysol

Copy link
Copy Markdown
Owner

PostHog Analytics Integration

Description

This PR introduces backend analytics tracking using PostHog across authentication and music library flows.

A centralized analytics layer was added to provide consistent, non-blocking event tracking while keeping business logic isolated from analytics concerns. Tracking is implemented using fire-and-forget helpers to avoid impacting request latency.

Key Features

  • Singleton PostHog client with graceful shutdown support
  • Centralized event definitions
  • Lightweight tracking helpers
  • Analytics automatically disabled/mocked during tests
  • Coverage added across auth and music interactions

Implementation Details

New Files

lib/analytics/client.js

  • Initializes PostHog singleton client
  • Exposes shutdownAnalytics() for graceful process termination

lib/analytics/events.js

  • Centralized analytics event name constants

lib/analytics/tracker.js

  • Fire-and-forget tracking helpers
  • Prevents analytics failures from affecting API responses

Modified Files

Infrastructure

  • webpack/server.config.js — Added @analytics alias

  • jest.config.json — Added analytics module mapping

  • jest.setup.js — Mocked analytics during tests

  • .env.local — Added:

    • POSTHOG_API_KEY
    • POSTHOG_HOST

Server Lifecycle

  • server.js — Added SIGINT handler invoking shutdownAnalytics() to flush queued events before shutdown

Tracked Routes

  • lib/routes/login/routes.jstrackLogin

  • lib/routes/signup/routes.jstrackSignup

  • lib/routes/logout/routes.jstrackLogout

  • lib/routes/music/resources/routes.jstrackSongSearched

  • lib/routes/music/library/routes.js

    • trackLibraryViewed
    • trackSongLiked
    • trackSongUnliked

Tests

  • lib/routes/signup/tests/routes.test.js

    • Updated mock data shape to include required user property

Results

  • ✅ Application builds successfully
  • ✅ Analytics runs asynchronously without affecting request flow
  • ✅ Graceful shutdown ensures event delivery
  • ✅ All existing tests remain stable
  • 115/115 tests passing

Steps to Reproduce / Test

  1. Add analytics environment variables:
POSTHOG_API_KEY=<your_key>
POSTHOG_HOST=<host_url>
  1. Start the server:
yarn start
  1. Perform the following actions:

    • Sign up
    • Log in / log out
    • Search for songs
    • View library
    • Like / unlike a song
  2. Verify events appear in PostHog dashboard.

  3. Run tests:

yarn test

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

@aman-wednesdaysol aman-wednesdaysol marked this pull request as draft February 24, 2026 13:28
@aman-wednesdaysol aman-wednesdaysol changed the base branch from main to itunes February 25, 2026 09:31
@vercel

vercel Bot commented Feb 25, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
nodejs-hapi-template Error Error Feb 25, 2026 9:35am

@aman-wednesdaysol aman-wednesdaysol marked this pull request as ready for review February 25, 2026 09:35
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.

1 participant