A complete analytics solution with a TypeScript SDK for frontend tracking and a Go backend for data processing and storage.
- TypeScript analytics SDK for tracking user events
- Supports pageviews, clicks, and conversions
- Easy integration with any web application
- Built with TypeScript and bundled with tsdown
- Go-based analytics API server
- Redis for event queuing and processing
- PostgreSQL for data storage
- Consumer groups for reliable event processing
- CORS-enabled for cross-origin requests
- Next.js demo application showcasing the SDK
- Product catalog with shopping cart functionality
- Real-time analytics tracking
- Complete e-commerce analytics example
- Go 1.21+ (for the API server)
- Node.js 18+ (for the SDK and demo)
- Docker & Docker Compose (for databases)
- Redis (for event queuing)
- PostgreSQL (for data storage)
git clone <your-repo-url>
cd go-analytics-sdk# Start Redis and PostgreSQL using Docker Compose
docker-compose up -dcd api
# Install Go dependencies
go mod tidy
# Run database migrations (if needed)
# The API will create tables automatically on first run
# Start the API server
go run main.goThe API will be available at http://localhost:8080
cd web/next-analytics-demo
# Install dependencies
npm install
# Start the development server
npm run devThe demo will be available at http://localhost:3000
cd sdk
# Install dependencies
npm install
# Build the SDK
npm run buildnpm install <your-sdk-package>import { AnalyticsSDK } from 'go-analytics-sdk';
const analytics = new AnalyticsSDK({
clientId: 'your-client-id',
baseUrl: 'http://localhost:8080', // Your API endpoint
debug: true, // Enable for development
});// Track pageviews
analytics.sendEvent({
user_id: 'user123',
event_type: 'pageview',
event_url: window.location.href,
eventData: {
page_title: document.title,
referrer: document.referrer,
},
});
// Track clicks
analytics.sendEvent({
user_id: 'user123',
event_type: 'click',
event_url: window.location.href,
eventData: {
element: 'button',
product_id: 'prod_123',
},
});analytics.sendEvent({
user_id: 'user123',
event_type: 'conversion',
event_url: window.location.href,
eventData: {
order_id: 'order_456',
order_total: 99.99,
products: [
{
product_id: 'prod_123',
product_name: 'Widget',
price: 99.99,
quantity: 1,
},
],
},
});βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β Frontend β β Go API β β Databases β
β (Next.js) βββββΆβ Server βββββΆβ Redis + PG β
β β β β β β
β - SDK β β - Event Handlersβ β - Event Queue β
β - Analytics β β - Redis Streams β β - Data Storage β
β - Demo App β β - CORS Support β β - Consumer Groupsβ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
- Track page visits and navigation
- Automatic URL and referrer capture
- Custom page metadata support
- Track user interactions
- Element identification
- Product and action tracking
- E-commerce transactions
- Order tracking
- Revenue analytics
interface AnalyticsConfig {
clientId: string; // Required: Your client identifier
baseUrl?: string; // Optional: API endpoint (default: localhost:8080)
debug?: boolean; // Optional: Enable debug logging
}- Port: Set via
PORTenvironment variable (default: 8080) - Database: PostgreSQL connection via environment variables
- Redis: Redis connection for event queuing
# Build the Go binary
cd api
go build -o analytics-api main.go
# Run with environment variables
PORT=8080 ./analytics-api# Build and publish to npm
cd sdk
npm run build
npm publish- Real-time event processing via Redis streams
- Consumer groups for reliable event handling
- PostgreSQL storage for analytics queries
- CORS support for cross-origin tracking
- Start the infrastructure:
docker-compose up -d - Start the API:
cd api && go run main.go - Start the demo:
cd web/next-analytics-demo && npm run dev - Visit
http://localhost:3000and interact with the products - Check the API logs to see events being processed
POST /event/pageview- Track pageview eventsPOST /event/click- Track click eventsPOST /event/conversion- Track conversion eventsGET /health- Health check endpoint
MIT License - see LICENSE file for details
Built with β€οΈ using Go, TypeScript, Next.js, Redis, and PostgreSQL