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

@dmwd/ga4-to-sqlite

v1.0.8

Published

Export data from Google Analytics 4 (GA4) to a local SQLite database using a configurable JSON query structure.

Readme

GA4 SQLite Exporter

A Node.js tool to fetch Google Analytics 4 (GA4) data and store it in a local SQLite database. Supports:

  • Custom GA4 queries
  • Lowercased and filtered dimensions
  • Auto-generated tables
  • Large datasets across multiple GA4 properties

📦 Features

  • Pulls GA4 data using a service account
  • Stores results in SQLite with auto-created schemas
  • Supports batching, offsets, filtering, and aggregations
  • Works with advanced dimensionExpression (like lowercase)
  • Easy integration with analytics dashboards or audits

🚀 Quick Start

1. Install dependencies

npm install

2. Create your GA4 service account

Follow Google’s instructions to create a service account and download the JSON key file: 📖 Set up service account

Ensure your GA4 property has granted Viewer or Analyst access to that service account email.

Place the credentials JSON in your project root (default path: analytics-service-account.json).


3. Configure your query

Create a config file like ./config/total-pageviews.json:

{
  "credentialsPath": "analytics-service-account.json",
  "query": {
    "dateRanges": { "startDate": "365daysAgo", "endDate": "yesterday" },
    "dimensions": [
      {
        "name": "pagePath_lower",
        "dimensionExpression": {
          "lowerCase": { "dimensionName": "pagePath" }
        }
      },
      {
        "name": "hostname_lower",
        "dimensionExpression": {
          "lowerCase": { "dimensionName": "hostname" }
        }
      },
      { "name": "yearWeek" }
    ],
    "metrics": [
      { "name": "screenPageViews" },
      { "name": "totalUsers" },
      { "name": "userEngagementDuration" },
      { "name": "sessions" }
    ],
    "metricAggregations": ["TOTAL"],
    "orderBys": [
      { "dimension": { "dimensionName": "yearWeek" }, "desc": false },
      { "dimension": { "dimensionName": "pagePath_lower" }, "desc": false },
      { "dimension": { "dimensionName": "hostname_lower" }, "desc": false }
    ],
    "dimensionFilter": {
      "orGroup": {
        "expressions": [
          {
            "filter": {
              "fieldName": "hostname_lower",
              "stringFilter": {
                "matchType": "CONTAINS",
                "value": ".gov",
                "caseSensitive": false
              }
            }
          },
          {
            "filter": {
              "fieldName": "hostname_lower",
              "stringFilter": {
                "matchType": "CONTAINS",
                "value": ".state.md.us",
                "caseSensitive": false
              }
            }
          }
        ]
      }
    },
    "metricFilter": {
      "andGroup": {
        "expressions": [
          {
            "filter": {
              "fieldName": "sessions",
              "numericFilter": {
                "operation": "GREATER_THAN",
                "value": { "int64Value": 1 }
              }
            }
          },
          {
            "filter": {
              "fieldName": "screenPageViews",
              "numericFilter": {
                "operation": "GREATER_THAN",
                "value": { "int64Value": 5 }
              }
            }
          }
        ]
      }
    }
  }
}

4. Run the report

node index.js

Or inside a script:

import { runGA4Report } from "@dmwd/ga4-to-sqlite";
const configPath = "./config/pageviews.json";
const dbPath = "./pageviews.db";

try {
  console.time("⏱️ Total time");
  await runGA4Report(configPath, dbPath);
  console.timeEnd("⏱️ Total time");
} catch (err) {
  console.error("❌ Failed to run GA4 report:", err);
}

📁 Output

  • SQLite DB file (e.g., ./total-pageviews.db)
  • Auto-created table name (inferred from config file name)
  • Includes:
    • id, created_date, updated_date, property_id
    • all dimension values
    • all metric values
  • fullPageUrl and fullPageUrl_lower (auto-joined from hostname + pagePath)

📚 Helpful Docs

🛠 Customize

You can modify:

  • credentialsPath: to change your key location
  • query: any supported GA4 requestBody
  • dbPath: for output location or naming

🤝 Contributing

Pull requests welcome! Please:

  • Use clear, concise commits
  • Include examples or reproducible steps if submitting an issue

📄 License

MIT