Skip to content

Commit

Permalink
feat(yandex-metrica): add Yandex Metrica utils
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepolischuk committed Aug 14, 2024
1 parent bd1f24c commit 6948d6d
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/yandex-metrica/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Yandex Metrica

Yandex Metrica utils

## Install

```
npm install -D @rambler-tech/yandex-metrica
```

or

```
yarn add -D @rambler-tech/yandex-metrica
```
88 changes: 88 additions & 0 deletions packages/yandex-metrica/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* eslint-disable import/no-unused-modules */
import waitForTarget from 'wait-for-target'
import {createDebug} from '@rambler-tech/debug'

const debug = createDebug('common:yandex-metrica')

const DEFAULT_YANDEX_METRICA_PARAMS = {
clickmap: true,
trackLinks: true,
triggerEvent: true,
accurateTrackBounce: true,
trackHash: true,
webvisor: true
}

/** Yandex Metrica options */
export interface YandexMetricaOptions {
id: number
params?: Record<string, any>
}

/** Yandex Metrica */
export class YandexMetrica {
options: YandexMetricaOptions
ready: Promise<void>

constructor(options: YandexMetricaOptions) {
this.options = options

this.ready = new Promise((resolve) => {
if (process.env.NODE_ENV === 'development') {
resolve()

return
}
/* eslint-disable */
// prettier-ignore
;(function (m, e, t, r, i, k, a) {
// @ts-ignore
m[i] = m[i] || function () { (m[i].a = m[i].a || []).push(arguments) };
// @ts-ignore
m[i].l = 1 * new Date(); k = e.createElement(t), a = e.getElementsByTagName(t)[0], k.async = 1, k.src = r, k.onload = () => resolve(), k.onerror = () => resolve(), a.parentNode.insertBefore(k, a)
})
(window, document, 'script', 'https://mc.yandex.ru/metrika/tag.js', 'ym')
try {
// @ts-ignore
ym(options.id, 'init', {
...DEFAULT_YANDEX_METRICA_PARAMS,
...options.params
})
} catch (e) {}
/* eslint-enable */
})
}

async run(method: string, ...args: any[]) {
const {id} = this.options

if (process.env.NODE_ENV === 'development') {
debug(`${method} ${id} %o`, args)

return
}

try {
const counter = await waitForTarget(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
() => window.ym,
1000
)

counter(id, method, ...args)
} catch {}
}

async hit(params: Record<string, any>) {
await this.run('hit', params)
}

async setParams(params: Record<string, any>) {
await this.run('params', params)
}

async reachGoal(...args: any[]) {
await this.run('reachGoal', ...args)
}
}
16 changes: 16 additions & 0 deletions packages/yandex-metrica/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@rambler-tech/yandex-metrica",
"version": "0.0.0",
"main": "dist",
"module": "dist",
"types": "dist/index.d.ts",
"license": "MIT",
"sideEffects": false,
"publishConfig": {
"access": "public"
},
"dependencies": {
"@rambler-tech/debug": "^0.1.0",
"wait-for-target": "^0.3.0"
}
}
9 changes: 9 additions & 0 deletions packages/yandex-metrica/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"baseUrl": ".",
"outDir": "dist"
},
"include": [".", "../../types"]
}
5 changes: 5 additions & 0 deletions packages/yandex-metrica/typedoc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "@rambler-tech/typedoc-config",
"readme": "../../README.md",
"entryPoints": ["./index.ts"]
}

0 comments on commit 6948d6d

Please sign in to comment.