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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@koiztech/next-yandex-metrika

v0.0.7

Published

Yandex Metrika for Next.js >= 14

Downloads

29

Readme

@koiztech/next-yandex-metrika

Other languages: Russian

IMPORTANT: This package is a Work in Progress.

A Next.js component and hook for integrating Yandex Metrika analytics into your Next.js application (version 14 and above).

Installation

Install the package:

npm install @koiztech/next-yandex-metrika
# or
pnpm add @koiztech/next-yandex-metrika

Add the Yandex Metrika ID to your environment variables:

NEXT_PUBLIC_YANDEX_ID=your_counter_id

Usage

1. Add the Yandex Metrika component

First, add the YandexMetrika component to your app layout or page:

// app/layout.tsx
import { YandexMetrika } from '@koiztech/next-yandex-metrika'

export default function RootLayout({ children }) {
  return (
    <html>
      <body>
        {children}
        <YandexMetrika />
      </body>
    </html>
  )
}

The YandexMetrika component accepts the following props:

  • clickmap (optional): Enable click map collection (default: true)
  • trackLinks (optional): Enable external link tracking (default: true)
  • accurateTrackBounce (optional): Enable accurate bounce rate tracking (default: true)
  • webvisor (optional): Enable session recording (default: false)
  • strategy (optional): Script loading strategy ('lazyOnload' | 'afterInteractive' | 'beforeInteractive', default: 'afterInteractive'). Reference.

e.g.:

<YandexMetrika clickmap={true} trackLinks={true} accurateTrackBounce={true} webvisor={false} strategy="afterInteractive" />

2. (optional) Track Page Views

The Yandex Metrika Tag by itself fails to track page views accurately in Next.js.

For this reason, there's an additional hook that you can use to track page views: usePageViews.

You could add it to your app/layout.tsx directly or – if your app is SSR – to a client layout, e.g.:

// e.g. src/components/LayoutClient.tsx
'use client'
import { usePageViews } from '@koiztech/next-yandex-metrika'

export default function LayoutClient({ children }: { children: React.ReactNode }) {
  usePageViews()
  return <>{children}</>
}
// app/layout.tsx
import LayoutClient from '@/components/LayoutClient'

const yid = process.env.NEXT_PUBLIC_YANDEX_ID

export default function RootLayout({
  children,
}: Readonly<{
  children: React.ReactNode
}>) {
  return (
    <html>
      <body>  
        <LayoutClient>
          {children}
        </LayoutClient>
        <YandexMetrika clickmap={true} trackLinks={true} accurateTrackBounce={true} webvisor={false} />
      </body>
    </html>
  )
}

...or you can use the hook on specific pages:

// e.g. app/page.tsx
'use client'
import { usePageViews } from '@koiztech/next-yandex-metrika'

export default function App() {
  usePageViews()
  return <div>Your app content</div>
}

Note: Use only on client components.

The usePageViews hook accepts an options object with the following properties:

  • ignoreHashChange (optional): Disable tracking of hash changes (default: false)
  • disabled (optional): Disable page view tracking (default: false)

TypeScript Support

This package includes TypeScript definitions out of the box.