npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

lab-analytics

v1.0.0

Published

Backend server to collect analytics data and store it in BigQuery

Downloads

7

Readme

Lab Analytics Backend

A TypeScript backend server that collects analytics data and stores it in Google BigQuery.

Schema

The BigQuery table has the following schema:

| Column | Type | Nullable | | --------------- | --------- | -------- | | event_timestamp | TIMESTAMP | NULLABLE | | event_name | STRING | NULLABLE | | session_id | STRING | NULLABLE | | user_id | STRING | NULLABLE | | client_name | STRING | NULLABLE | | page_url | STRING | NULLABLE | | referer_url | STRING | NULLABLE | | event_data | JSON | NULLABLE | | event_item | STRING | NULLABLE | | user_agent | STRING | NULLABLE |

Setup

  1. Clone this repository
  2. Install dependencies:
    npm install
  3. Create a .env file based on .env.example:
    cp .env.example .env
  4. Update the environment variables in .env with your Google Cloud project details:
    • GCP_PROJECT_ID: Your Google Cloud project ID
    • GCP_KEY_FILE: Path to your service account key file (JSON) with BigQuery permissions
    • BQ_DATASET_ID: BigQuery dataset ID where your analytics table is located
    • BQ_TABLE_ID: BigQuery table ID for your analytics data

Google Cloud Setup

  1. Create a service account in the Google Cloud Console
  2. Grant it BigQuery Data Editor permissions
  3. Download the service account key (JSON file)
  4. Place the key file in a secure location and update the GCP_KEY_FILE path in your .env

Firebase Authentication Setup

This server uses Firebase Authentication to verify user tokens for secure API access.

  1. Create a Firebase project in the Firebase Console
  2. Enable Authentication in your Firebase project and set up your preferred authentication methods
  3. Generate a Firebase Admin SDK service account key:
    • Go to Project Settings > Service accounts
    • Click "Generate new private key" for Firebase Admin SDK
    • Save the JSON file securely
  4. Update your .env file with Firebase configuration:
    • Set FIREBASE_PROJECT_ID to your Firebase project ID
    • Set GOOGLE_APPLICATION_CREDENTIALS to the path of your Firebase service account key

Note: You can use the same service account key for both GCP and Firebase if they're in the same Google Cloud project, but make sure it has the required permissions.

Running the Server

Build the TypeScript code:

npm run build

Development mode with auto-reload:

npm run dev

Production mode:

npm start

API Endpoints

POST /api/analytics

Accepts analytics data and inserts it into BigQuery.

Request Body Example:

{
  "event_name": "page_view",
  "session_id": "sess_123456",
  "user_id": "user_789",
  "client_name": "web",
  "page_url": "https://example.com/products",
  "referer_url": "https://example.com",
  "event_data": {
    "productId": "prod_123",
    "category": "electronics"
  },
  "event_item": "product_page"
}

Note: If event_timestamp is not provided, the current server timestamp will be used. If user_agent is not provided, it will be extracted from the request headers.

GET /health

Health check endpoint that returns a 200 status with a JSON response of {"status": "OK"}.

Client Integration

To send analytics data from a client (e.g., browser), you can use a simple fetch request with Firebase authentication:

// First authenticate with Firebase and get the ID token
firebase
  .auth()
  .currentUser.getIdToken(/* forceRefresh */ true)
  .then((idToken) => {
    // Send the token with your analytics request
    fetch("https://your-analytics-server.com/api/analytics", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${idToken}`,
      },
      body: JSON.stringify({
        event_name: "button_click",
        session_id: "user_session_id",
        // ... other analytics data
      }),
    })
      .then((response) => response.json())
      .catch((error) => console.error("Analytics error:", error));
  })
  .catch((error) => {
    console.error("Error getting auth token:", error);
  });