diff --git a/packages/yandex-metrica/README.md b/packages/yandex-metrica/README.md new file mode 100644 index 0000000..752fd3f --- /dev/null +++ b/packages/yandex-metrica/README.md @@ -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 +``` diff --git a/packages/yandex-metrica/index.ts b/packages/yandex-metrica/index.ts new file mode 100644 index 0000000..b63e69c --- /dev/null +++ b/packages/yandex-metrica/index.ts @@ -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 +} + +/** Yandex Metrica */ +export class YandexMetrica { + options: YandexMetricaOptions + ready: Promise + + 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) { + await this.run('hit', params) + } + + async setParams(params: Record) { + await this.run('params', params) + } + + async reachGoal(...args: any[]) { + await this.run('reachGoal', ...args) + } +} diff --git a/packages/yandex-metrica/package.json b/packages/yandex-metrica/package.json new file mode 100644 index 0000000..9864ceb --- /dev/null +++ b/packages/yandex-metrica/package.json @@ -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" + } +} diff --git a/packages/yandex-metrica/tsconfig.json b/packages/yandex-metrica/tsconfig.json new file mode 100644 index 0000000..b81d8e5 --- /dev/null +++ b/packages/yandex-metrica/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "module": "commonjs", + "baseUrl": ".", + "outDir": "dist" + }, + "include": [".", "../../types"] +} diff --git a/packages/yandex-metrica/typedoc.json b/packages/yandex-metrica/typedoc.json new file mode 100644 index 0000000..fd9d16b --- /dev/null +++ b/packages/yandex-metrica/typedoc.json @@ -0,0 +1,5 @@ +{ + "extends": "@rambler-tech/typedoc-config", + "readme": "../../README.md", + "entryPoints": ["./index.ts"] +}