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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@vp-tw/nanostores-data-layer

v1.0.3

Published

A nanostores adapter for Google Tag Manager dataLayer

Readme

@vp-tw/nanostores-data-layer

A nanostores adapter for Google Tag Manager dataLayer. Reactively sync GTM dataLayer changes to a nanostores store.

Installation

npm install @vp-tw/nanostores-data-layer nanostores
# or
pnpm add @vp-tw/nanostores-data-layer nanostores
# or
yarn add @vp-tw/nanostores-data-layer nanostores

Usage

Processing All Events (Including Past)

Use subscribe() when you need to process events that were pushed before your code runs (e.g., GTM tags that fire on page load). The callback is executed immediately upon subscription:

import { $dataLayer } from "@vp-tw/nanostores-data-layer";

// Events already in dataLayer (e.g., pushed by GTM before your code runs)
window.dataLayer.push({ event: "gtm.js" });
window.dataLayer.push({ event: "page_view", page_path: "/home" });

// Callback runs IMMEDIATELY (even before any new push)
const unsubscribe = $dataLayer.subscribe((events, oldEvents) => {
  const newEvents = events.slice(oldEvents?.length ?? 0);
  newEvents.forEach((event) => {
    console.log("Event:", event);
  });
});
// Console: Event: { event: 'gtm.js' }           ← immediately!
// Console: Event: { event: 'page_view', ... }  ← immediately!

// Future pushes also trigger the callback
window.dataLayer.push({ event: "click" });
// Console: Event: { event: 'click' }

unsubscribe();

Processing Only New Events

Use listen() when you only care about future events. The callback is not executed immediately—it only fires when new events are pushed:

import { $dataLayer } from "@vp-tw/nanostores-data-layer";

// Events already in dataLayer
window.dataLayer.push({ event: "gtm.js" });
window.dataLayer.push({ event: "page_view", page_path: "/home" });

// Callback does NOT run immediately
const unlisten = $dataLayer.listen((events, oldEvents) => {
  const newEvents = events.slice(oldEvents?.length ?? 0);
  newEvents.forEach((event) => {
    console.log("Event:", event);
  });
});
// (no output)

// Only future pushes trigger the callback
window.dataLayer.push({ event: "click" });
// Console: Event: { event: 'click' }

unlisten();

Getting Current Value

import { $dataLayer } from "@vp-tw/nanostores-data-layer";

console.log($dataLayer.get()); // Current dataLayer contents

With React

import { useStore } from "@nanostores/react";
import { $dataLayer } from "@vp-tw/nanostores-data-layer";

function Analytics() {
  const dataLayer = useStore($dataLayer);

  return <pre>{JSON.stringify(dataLayer, null, 2)}</pre>;
}

With Vue

<script setup>
import { useStore } from "@nanostores/vue";
import { $dataLayer } from "@vp-tw/nanostores-data-layer";

const dataLayer = useStore($dataLayer);
</script>

<template>
  <pre>{{ dataLayer }}</pre>
</template>

How It Works

The library:

  1. Initializes window.dataLayer if not present
  2. Wraps the native push method to intercept all additions
  3. Syncs changes to a readonly nanostores atom

The store is readonly (ReadableAtom) - you should push to window.dataLayer directly, and the store will automatically update.

SSR Support

The library safely handles server-side rendering. When window is undefined, the store returns an empty array and no errors are thrown.

TypeScript

Full TypeScript support is included. The DataLayer type is Array<unknown> by design, since GTM dataLayer can contain arbitrary data from various sources (GTM tags, third-party scripts, etc.).

import type { DataLayer } from "@vp-tw/nanostores-data-layer";

Type Safety with Runtime Validation

Since dataLayer contents are inherently dynamic, we recommend using runtime validation libraries like Zod or ArkType for type-safe event handling:

import type { DataLayer } from "@vp-tw/nanostores-data-layer";
import { $dataLayer } from "@vp-tw/nanostores-data-layer";
import { z } from "zod";

// Define your event schemas
const PageViewEvent = z.object({
  event: z.literal("page_view"),
  page_path: z.string(),
});

const PurchaseEvent = z.object({
  event: z.literal("purchase"),
  transaction_id: z.string(),
  value: z.number(),
});

const MyEvent = z.discriminatedUnion("event", [PageViewEvent, PurchaseEvent]);
type MyEvent = z.infer<typeof MyEvent>;

// Type-safe event processing
$dataLayer.subscribe((events, oldEvents) => {
  const newEvents = events.slice(oldEvents?.length ?? 0);

  for (const item of newEvents) {
    const result = MyEvent.safeParse(item);
    if (result.success) {
      // result.data is fully typed as MyEvent
      switch (result.data.event) {
        case "page_view":
          console.log("Page view:", result.data.page_path);
          break;
        case "purchase":
          console.log("Purchase:", result.data.transaction_id, result.data.value);
          break;
      }
    }
  }
});

Generic Type Parameter

For simpler cases where you control all dataLayer inputs, you can use the generic type parameter for type assertions:

import type { DataLayer } from "@vp-tw/nanostores-data-layer";

interface MyEvent {
  event: string;
  [key: string]: unknown;
}

// Type assertion (use with caution - no runtime validation)
const events = $dataLayer.get() as DataLayer<MyEvent>;

License

MIT

Copyright (c) 2026 ViPro [email protected] (https://vdustr.dev)