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

astro-ym

v1.1.0

Published

Yandex Metrika integration for Astro with full ClientRouter support

Downloads

68

Readme

astro-ym

npm version License: MIT TypeScript

Lightweight, type-safe Yandex Metrika integration for Astro.

Designed for Astro 4+ and Astro 5+ (supports both View Transitions and the new Client Router). It handles SPA navigation correctly out of the box, eliminating duplicate hits and ensuring accurate tracking.

✨ Features

  • 🚀 Client Router Support: Automatically tracks page views on route changes (SPA navigation) using astro:page-load.
  • 💤 Lazy Loading: Optional lazy load of Yandex Metrika script (load on first user interaction or after timeout) for better Lighthouse scores.
  • Zero Config: Smart defaults (Clickmap, Link tracking enabled by default).
  • 🛡 TypeScript: Fully typed component and helper functions.
  • 🎯 Goals Helper: Exported reachGoal function for easy conversion tracking.
  • 🪶 Lightweight: Uses the modern Yandex tag.js structure and queues calls before the script loads.

📦 Installation

npm install astro-ym

🚀 Usage

Add the <YandexMetrika /> component to your main Layout file (e.g., src/layouts/Layout.astro). It is recommended to place it before the closing </body> tag.

---
import { YandexMetrika } from 'astro-ym';
---

<html lang="en">
  <head>
    <!-- Your meta tags -->
  </head>
  <body>
    <slot />

    <!-- Basic usage -->
    <YandexMetrika counterId={12345678} />
  </body>
</html>

Advanced Configuration

You can enable/disable specific features via props.

<YandexMetrika
  counterId={12345678}
  clickmap={true}
  trackLinks={true}
  accurateTrackBounce={true}
  webvisor={true}
  ecommerce="dataLayer"
  params={{ source: "astro" }}
/>

Lazy Loading (recommended for performance)

You can enable lazy loading so that the Yandex Metrika script loads only after the first user interaction (scroll, click, mousemove, touch, keydown) or after a fallback timeout.

<YandexMetrika
  counterId={12345678}
  webvisor={true}
  lazy={true}
  // optional: fallback timeout in ms (default: 3500)
  timeout={4000}
/>

This keeps ym available immediately (calls are queued), but delays loading tag.js to improve performance metrics like TBT and LCP.

🎯 Sending Goals (Conversions)

You can trigger goals from anywhere in your client-side code (UI components, scripts) using the exported helper. It automatically detects the counter ID.

Example in a React/Preact/Solid component:

import { reachGoal } from "astro-ym";

export const BuyButton = () => {
  const handleClick = () => {
    // Send goal
    reachGoal("purchase_click");

    // With params
    // reachGoal('purchase_click', { price: 100, currency: 'USD' });
  };

  return <button onClick={handleClick}>Buy Now</button>;
};

Example in Astro script tag:

<script>
  import { reachGoal } from "astro-ym";

  document.getElementById("my-btn")?.addEventListener("click", () => {
    reachGoal("my_target_id");
  });
</script>

📚 Props Reference

| Prop | Type | Default | Description | | :-------------------- | :------------------- | :----------- | :-------------------------------------------------------------------------------------------- | | counterId | number \| string | Required | Your Yandex Metrika Counter ID. | | ssr | boolean | true | Helps Yandex detect initial server-side load correctly. | | webvisor | boolean | false | Enables Webvisor (session recording). | | clickmap | boolean | true | Enables Click map. | | trackLinks | boolean | true | Tracks external link clicks. | | accurateTrackBounce | boolean \| number | true | Accurate bounce rate tracking (true = 15s, or pass custom timeout in ms). | | ecommerce | boolean \| string | false | Enable E-commerce data collection. Pass container name (e.g., "dataLayer") if needed. | | type | number | 0 | Counter type. 1 for Yandex Advertising Network (RSYA), 0 for standard. | | trackHash | boolean | false | Track changes in the URL hash (#anchor) as separate hits. | | sendTitle | boolean | true | Send the page <title> with each hit. | | childIframe | boolean | false | Record iframe content without a counter. | | trustedDomains | string[] | undefined | List of trusted domains for iframe recording. | | params | object \| any[] | undefined | Visit parameters (session params). | | userParams | object | undefined | User parameters. | | config | Record<string,any> | {} | Any additional raw Yandex config. defer and triggerEvent are controlled by the component. | | debug | boolean | false | Logs init and hits to console for debugging. | | lazy | boolean | false | Enable lazy loading of tag.js (loads on first interaction or after timeout). | | timeout | number | 3500 | Fallback timeout (ms) to auto-load script in lazy mode if there is no user interaction. |

🛠 Troubleshooting

"Yandex Metrika not initialized" warning

Ensure the <YandexMetrika /> component is mounted on the page. The helper functions (reachGoal) rely on the component being present to access the global counter ID.

AdBlockers

If you don't see events in the dashboard, check if you have an AdBlocker enabled. It often blocks mc.yandex.ru.

Lazy mode enabled but no hits

If you enabled lazy={true} and do not see hits on fast visits, remember that the script loads on first interaction or after the timeout. For critical pages you can disable lazy mode:

<YandexMetrika counterId={12345678} lazy={false} />

Author

Created by KochGO — AI Solutions Architect.

📄 License

MIT © KochGO