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

yandex-metrika-wrapper

v1.0.1

Published

A TypeScript wrapper for Yandex Metrika

Downloads

16

Readme

yandex-metrika-wrapper

yandex-metrika-wrapper is a TypeScript wrapper for the Yandex Metrika API, providing a strongly-typed and easy-to-use interface for tracking user interactions and sending data to Yandex Metrika.

Features

  • TypeScript support with generics for goal names and parameters.
  • Easy integration with Yandex Metrika for tracking events, page views, and user actions.
  • Automatically injects the Yandex Metrika script into your webpage.
  • Supports initializing multiple Metrika counters.

Installation

Install the package via npm:

npm install yandex-metrika-wrapper

Getting Started

1. Inserting the Yandex Metrika Script

Before using the Metrika service, you need to insert the Yandex Metrika script into your webpage:

import { YandexMetrikaService } from 'yandex-metrika-wrapper';

// Insert the Yandex Metrika script into the page
YandexMetrikaService.insertMetrika();

2. Initializing Metrika Counters

Initialize one or more Metrika counters with their configurations:

YandexMetrikaService.initMetrika([
    { id: 123456, accurateTrackBounce: true },
    { id: 654321, webvisor: true },
]);

3. Using the Metrika Service

Define your goals and their associated parameter types:

type MyGoals = 'purchase' | 'signup';

interface MyGoalParams {
    purchase: { orderId: number };
    signup: { userId: string };
}

// Instantiate MetrikaService with specific goals and their parameters
const metrikaService = new YandexMetrikaService<MyGoals, MyGoalParams>(123456);

// Track a 'purchase' goal
metrikaService.reachGoal('purchase', { orderId: 123 });

// Track a 'signup' goal
metrikaService.reachGoal('signup', { userId: 'abc123' });

Full Example

Here's a complete example showing how to set up and use yandex-metrika-wrapper:

import { YandexMetrikaService } from 'yandex-metrika-wrapper';

// Insert the Yandex Metrika script into the page
YandexMetrikaService.insertMetrika();

// Initialize Metrika counters
YandexMetrikaService.initMetrika([
    { id: 123456, accurateTrackBounce: true },
]);

// Define your goals and their parameter types
type MyGoals = 'purchase' | 'signup';
interface MyGoalParams {
    purchase: { orderId: number };
    signup: { userId: string };
}

// Create an instance of MetrikaService
const metrikaService = new YandexMetrikaService<MyGoals, MyGoalParams>(123456);

// Track a purchase event
metrikaService.reachGoal('purchase', { orderId: 123 });

// Track a signup event
metrikaService.reachGoal('signup', { userId: 'abc123' });

API Reference

YandexMetrikaService

insertMetrika(alternativeUrl?: string)

Inserts the Yandex Metrika script into the page. Call this method before tracking any events.

  • alternativeUrl (optional): A custom URL for the Yandex Metrika script.

initMetrika(counterConfigs: CounterConfig[])

Initializes one or more Yandex Metrika counters.

  • counterConfigs: An array of counter configurations, each including an id and other initialization parameters.

reachGoal<T extends G>(goal: T, params: P[T], counterId?: number): Promise<void>

Tracks a goal with specific parameters.

  • goal: The name of the goal.
  • params: The parameters for the goal, inferred based on the goal name.
  • counterId (optional): A specific counter ID to use. Defaults to the defaultCounterId passed in the constructor.

Types

GoalParams

Defines the structure for goal parameters. Extend this interface to define your own goal parameter types.

Example

interface MyGoalParams {
    purchase: { orderId: number };
    signup: { userId: string };
}

License

MIT