-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(yandex-metrica): add Yandex Metrica utils
- Loading branch information
1 parent
bd1f24c
commit 6948d6d
Showing
5 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |