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

@jwiedeman/gtm-kit-astro

v1.1.6

Published

Astro components and helpers for GTM Kit - Google Tag Manager integration with View Transitions support.

Readme

@jwiedeman/gtm-kit-astro

CI npm version Bundle Size TypeScript License: MIT Astro

Astro components and helpers for Google Tag Manager. Supports View Transitions API.

The Astro integration for GTM Kit - includes ready-to-use components and client-side helpers for page tracking.


Installation

npm install @jwiedeman/gtm-kit @jwiedeman/gtm-kit-astro
yarn add @jwiedeman/gtm-kit @jwiedeman/gtm-kit-astro
pnpm add @jwiedeman/gtm-kit @jwiedeman/gtm-kit-astro

Quick Start

Option 1: Use the GtmHead Component (Recommended)

The easiest way to add GTM to your Astro site:

---
// src/layouts/Layout.astro
import { GtmHead } from '@jwiedeman/gtm-kit-astro/components';
---
<html>
  <head>
    <GtmHead
      containers="GTM-XXXXXX"
      enablePageTracking
    />
  </head>
  <body>
    <slot />
  </body>
</html>

Option 2: Use Individual Components

For more control, use the script and noscript components separately:

---
// src/layouts/Layout.astro
import { GtmScript, GtmNoScript } from '@jwiedeman/gtm-kit-astro/components';
---
<html>
  <head>
    <GtmScript containers="GTM-XXXXXX" />
  </head>
  <body>
    <GtmNoScript containers="GTM-XXXXXX" />
    <slot />
  </body>
</html>

Option 3: Client-Side API

For programmatic control:

---
// src/layouts/Layout.astro
---
<html>
  <head>
    <!-- Your head content -->
  </head>
  <body>
    <slot />
    <script>
      import { initGtm, push, setupPageTracking } from '@jwiedeman/gtm-kit-astro';

      initGtm({ containers: 'GTM-XXXXXX' });
      setupPageTracking();

      // Push custom events
      push({ event: 'page_loaded', page_type: 'home' });
    </script>
  </body>
</html>

Features

| Feature | Description | | -------------------- | --------------------------------------------------------- | | View Transitions | Automatic page tracking with Astro's View Transitions API | | Zero Config | Works out of the box with sensible defaults | | Astro 3, 4, 5 | Compatible with all modern Astro versions | | TypeScript | Full type definitions included | | Consent Mode v2 | Built-in GDPR compliance support | | SSR Safe | Works with static and SSR modes |


Components

<GtmHead />

All-in-one component for the <head> section. Includes script loading, consent setup, and optional page tracking.

<GtmHead
  containers="GTM-XXXXXX"
  enablePageTracking
  defaultConsent={{
    ad_storage: 'denied',
    analytics_storage: 'denied'
  }}
  pageViewEventName="page_view"
/>

Props

| Prop | Type | Default | Description | | -------------------- | -------------------- | ---------------------------------- | ------------------------------- | | containers | string \| string[] | Required | GTM container ID(s) | | host | string | https://www.googletagmanager.com | Custom GTM host | | dataLayerName | string | dataLayer | Custom dataLayer name | | defaultConsent | ConsentState | - | Default consent state | | enablePageTracking | boolean | false | Enable automatic page tracking | | pageViewEventName | string | page_view | Custom page view event name | | scriptAttributes | object | - | Script attributes (nonce, etc.) |

<GtmScript />

Generates GTM script tags for the <head>.

<GtmScript
  containers="GTM-XXXXXX"
  scriptAttributes={{ nonce: 'abc123' }}
/>

<GtmNoScript />

Generates noscript iframe fallback for the <body>.

<GtmNoScript containers="GTM-XXXXXX" />

Client-Side API

initGtm(options)

Initialize GTM on the client side.

import { initGtm } from '@jwiedeman/gtm-kit-astro';

initGtm({
  containers: 'GTM-XXXXXX',
  dataLayerName: 'dataLayer'
});

push(data)

Push data to the dataLayer.

import { push } from '@jwiedeman/gtm-kit-astro';

push({ event: 'button_click', button_id: 'cta-main' });

setupPageTracking(options)

Set up automatic page view tracking. Works with Astro's View Transitions.

import { setupPageTracking } from '@jwiedeman/gtm-kit-astro';

setupPageTracking({
  eventName: 'page_view' // Optional, defaults to 'page_view'
});

setupViewTransitions(options)

Alias for setupPageTracking. Sets up tracking for View Transitions API.

import { setupViewTransitions } from '@jwiedeman/gtm-kit-astro';

setupViewTransitions({ eventName: 'virtual_page_view' });

trackPageView(options)

Manually track a page view.

import { trackPageView } from '@jwiedeman/gtm-kit-astro';

trackPageView({
  eventName: 'page_view',
  pagePath: '/custom-path',
  pageTitle: 'Custom Title'
});

Consent Mode v2 (GDPR)

Setting Default Consent

<GtmHead
  containers="GTM-XXXXXX"
  defaultConsent={{
    ad_storage: 'denied',
    ad_user_data: 'denied',
    ad_personalization: 'denied',
    analytics_storage: 'denied'
  }}
/>

Updating Consent After User Choice

import { updateConsent } from '@jwiedeman/gtm-kit-astro';

// User accepts analytics
updateConsent({
  analytics_storage: 'granted'
});

// User accepts all
updateConsent({
  ad_storage: 'granted',
  ad_user_data: 'granted',
  ad_personalization: 'granted',
  analytics_storage: 'granted'
});

View Transitions Integration

GTM Kit automatically tracks page views when using Astro's View Transitions. Just enable page tracking:

---
import { GtmHead } from '@jwiedeman/gtm-kit-astro/components';
import { ViewTransitions } from 'astro:transitions';
---
<html>
  <head>
    <ViewTransitions />
    <GtmHead
      containers="GTM-XXXXXX"
      enablePageTracking
    />
  </head>
  <body>
    <slot />
  </body>
</html>

Page views are automatically tracked on:

  • Initial page load
  • Client-side navigation via View Transitions
  • Browser back/forward navigation

Multiple Containers

<GtmHead
  containers={['GTM-XXXXXX', 'GTM-YYYYYY']}
  enablePageTracking
/>

CSP (Content Security Policy)

<GtmHead
  containers="GTM-XXXXXX"
  scriptAttributes={{ nonce: 'your-nonce-value' }}
/>

Requirements

  • Astro 3.0+, 4.0+, or 5.0+
  • @jwiedeman/gtm-kit (peer dependency)

Related Packages


Support

Have a question, found a bug, or need help?

Open an issue on GitHub — we're actively maintaining this project and respond quickly.


License

MIT