@vnejs/plugins.platform.metric 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ import "@vnejs/plugins.platform.metric.contract";
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ import "@vnejs/plugins.platform.metric.contract";
2
+ import { regPlugin } from "@vnejs/shared";
3
+ import { PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/plugins.platform.metric.contract";
4
+ import { YaMetrika } from "./modules/yandex.js";
5
+ regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, params: PARAMS }, [YaMetrika]);
@@ -0,0 +1,13 @@
1
+ import { Module } from "@vnejs/module";
2
+ import type { MetricPluginConstants, MetricPluginEvents, MetricPluginParams, MetricPluginSettings } from "../types.js";
3
+ import type { MetricSavePayload, MetricStackPayload } from "../utils/metric.js";
4
+ export declare class YaMetrika extends Module<MetricPluginEvents, MetricPluginConstants, MetricPluginSettings, MetricPluginParams> {
5
+ name: string;
6
+ subscribe: () => void;
7
+ init: () => void;
8
+ onStack: ({ action, label }?: MetricStackPayload) => void | undefined;
9
+ onSave: ({ key }?: MetricSavePayload) => void | undefined;
10
+ onLoad: ({ key }?: MetricSavePayload) => void | undefined;
11
+ onParams: (params: Record<string, unknown>) => void | undefined;
12
+ onReachGoal: (target: string) => void | undefined;
13
+ }
@@ -0,0 +1,20 @@
1
+ import { Module } from "@vnejs/module";
2
+ import { initYandexMetrika } from "../utils/metric.js";
3
+ export class YaMetrika extends Module {
4
+ name = "ya-metrika";
5
+ subscribe = () => {
6
+ if (!this.PARAMS.METRIC.YA_COUNTER_ID)
7
+ return;
8
+ this.on(this.EVENTS.STACK.EMIT, this.onStack);
9
+ this.on(this.EVENTS.SAVE.CREATE, this.onSave);
10
+ this.on(this.EVENTS.SAVE.LOAD, this.onLoad);
11
+ this.on(this.EVENTS.METRIC.METRIC_GOAL, this.onReachGoal);
12
+ this.on(this.EVENTS.METRIC.METRIC_PARAMS, this.onParams);
13
+ };
14
+ init = () => initYandexMetrika(this.PARAMS.METRIC.YA_COUNTER_ID);
15
+ onStack = ({ action, label } = {}) => this.onParams({ VNE_STACK: { action, label } });
16
+ onSave = ({ key } = {}) => this.onParams({ VNE_SAVE: { key } });
17
+ onLoad = ({ key } = {}) => this.onParams({ VNE_LOAD: { key } });
18
+ onParams = (params) => window.ym?.(this.PARAMS.METRIC.YA_COUNTER_ID, "params", params);
19
+ onReachGoal = (target) => window.ym?.(this.PARAMS.METRIC.YA_COUNTER_ID, "reachGoal", target);
20
+ }
@@ -0,0 +1,8 @@
1
+ import type { VnePluginConstantsMapRegistry, VnePluginEventsMapRegistry, VnePluginParamsMapRegistry, VnePluginSettingsMapRegistry } from "@vnejs/shared";
2
+ import type { PluginName as StackPluginName, SubscribeEvents as StackSubscribeEvents } from "@vnejs/plugins.core.stack.contract";
3
+ import type { PluginName, Params, SubscribeEvents } from "@vnejs/plugins.platform.metric.contract";
4
+ import type { PluginName as SavePluginName, SubscribeEvents as SaveSubscribeEvents } from "@vnejs/plugins.save.contract";
5
+ export type MetricPluginEvents = VnePluginEventsMapRegistry & Record<PluginName, SubscribeEvents> & Record<StackPluginName, StackSubscribeEvents> & Record<SavePluginName, SaveSubscribeEvents>;
6
+ export type MetricPluginConstants = VnePluginConstantsMapRegistry;
7
+ export type MetricPluginSettings = VnePluginSettingsMapRegistry;
8
+ export type MetricPluginParams = VnePluginParamsMapRegistry & Record<PluginName, Params>;
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,18 @@
1
+ export type MetricStackPayload = {
2
+ action?: string;
3
+ label?: string;
4
+ };
5
+ export type MetricSavePayload = {
6
+ key?: string;
7
+ };
8
+ export type YmFunction = ((counterId: number, method: string, ...args: unknown[]) => void) & {
9
+ a?: unknown[][];
10
+ l?: number;
11
+ };
12
+ export declare const YANDEX_METRIKA_INIT_OPTIONS: {
13
+ readonly defer: true;
14
+ readonly clickmap: true;
15
+ readonly trackLinks: true;
16
+ readonly accurateTrackBounce: true;
17
+ };
18
+ export declare const initYandexMetrika: (counterId: number) => void;
@@ -0,0 +1,31 @@
1
+ const TAG_SRC = "https://mc.yandex.ru/metrika/tag.js";
2
+ export const YANDEX_METRIKA_INIT_OPTIONS = {
3
+ defer: true,
4
+ clickmap: true,
5
+ trackLinks: true,
6
+ accurateTrackBounce: true,
7
+ };
8
+ const ensureYmLoader = () => {
9
+ window.ym =
10
+ window.ym ||
11
+ function (...args) {
12
+ const ym = window.ym;
13
+ ym.a = ym.a || [];
14
+ ym.a.push(args);
15
+ };
16
+ if (Array.from(document.scripts).some((script) => script.src === TAG_SRC))
17
+ return;
18
+ window.ym.l = Date.now();
19
+ const script = document.createElement("script");
20
+ const firstScript = document.getElementsByTagName("script")[0];
21
+ script.async = true;
22
+ script.src = TAG_SRC;
23
+ firstScript?.parentNode?.insertBefore(script, firstScript);
24
+ };
25
+ export const initYandexMetrika = (counterId) => {
26
+ if (!counterId)
27
+ return;
28
+ ensureYmLoader();
29
+ window.ym?.(counterId, "init", YANDEX_METRIKA_INIT_OPTIONS);
30
+ window.ym?.(counterId, "hit", window.location.href);
31
+ };
package/package.json CHANGED
@@ -1,17 +1,44 @@
1
1
  {
2
2
  "name": "@vnejs/plugins.platform.metric",
3
- "version": "0.0.3",
4
- "main": "index.js",
3
+ "version": "0.0.5",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js",
11
+ "require": "./dist/index.js",
12
+ "default": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist",
17
+ "src",
18
+ "tsconfig.json"
19
+ ],
5
20
  "scripts": {
6
21
  "test": "echo \"Error: no test specified\" && exit 1",
22
+ "build": "npx @vnejs/monorepo package",
7
23
  "publish:major:plugin": "npm run publish:major",
8
24
  "publish:minor:plugin": "npm run publish:minor",
9
25
  "publish:patch:plugin": "npm run publish:patch",
10
- "publish:major": "npm version major && npm publish --access public",
11
- "publish:minor": "npm version minor && npm publish --access public",
12
- "publish:patch": "npm version patch && npm publish --access public"
26
+ "publish:major": "npx @vnejs/monorepo publish major --access public",
27
+ "publish:minor": "npx @vnejs/monorepo publish minor --access public",
28
+ "publish:patch": "npx @vnejs/monorepo publish patch --access public"
13
29
  },
14
30
  "author": "",
15
31
  "license": "ISC",
16
- "description": ""
32
+ "dependencies": {
33
+ "@vnejs/plugins.platform.metric.contract": "~0.0.1"
34
+ },
35
+ "peerDependencies": {
36
+ "@vnejs/module": "~0.0.1",
37
+ "@vnejs/plugins.core.stack.contract": "~0.0.1",
38
+ "@vnejs/plugins.save.contract": "~0.0.1",
39
+ "@vnejs/shared": "~0.0.9"
40
+ },
41
+ "devDependencies": {
42
+ "@vnejs/configs.ts-common": "~0.0.1"
43
+ }
17
44
  }
@@ -0,0 +1,6 @@
1
+ interface Window {
2
+ ym?: ((counterId: number, method: string, ...args: unknown[]) => void) & {
3
+ a?: unknown[][];
4
+ l?: number;
5
+ };
6
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ import "@vnejs/plugins.platform.metric.contract";
2
+
3
+ import { regPlugin } from "@vnejs/shared";
4
+ import { PARAMS, PLUGIN_NAME, SUBSCRIBE_EVENTS } from "@vnejs/plugins.platform.metric.contract";
5
+
6
+ import { YaMetrika } from "./modules/yandex.js";
7
+
8
+ regPlugin(PLUGIN_NAME, { events: SUBSCRIBE_EVENTS, params: PARAMS }, [YaMetrika]);
@@ -0,0 +1,30 @@
1
+ import { Module } from "@vnejs/module";
2
+
3
+ import type { MetricPluginConstants, MetricPluginEvents, MetricPluginParams, MetricPluginSettings } from "../types.js";
4
+ import type { MetricSavePayload, MetricStackPayload } from "../utils/metric.js";
5
+ import { initYandexMetrika } from "../utils/metric.js";
6
+
7
+ export class YaMetrika extends Module<MetricPluginEvents, MetricPluginConstants, MetricPluginSettings, MetricPluginParams> {
8
+ name = "ya-metrika";
9
+
10
+ subscribe = () => {
11
+ if (!this.PARAMS.METRIC.YA_COUNTER_ID) return;
12
+
13
+ this.on(this.EVENTS.STACK.EMIT, this.onStack);
14
+
15
+ this.on(this.EVENTS.SAVE.CREATE, this.onSave);
16
+ this.on(this.EVENTS.SAVE.LOAD, this.onLoad);
17
+
18
+ this.on(this.EVENTS.METRIC.METRIC_GOAL, this.onReachGoal);
19
+ this.on(this.EVENTS.METRIC.METRIC_PARAMS, this.onParams);
20
+ };
21
+
22
+ init = () => initYandexMetrika(this.PARAMS.METRIC.YA_COUNTER_ID);
23
+
24
+ onStack = ({ action, label }: MetricStackPayload = {}) => this.onParams({ VNE_STACK: { action, label } });
25
+ onSave = ({ key }: MetricSavePayload = {}) => this.onParams({ VNE_SAVE: { key } });
26
+ onLoad = ({ key }: MetricSavePayload = {}) => this.onParams({ VNE_LOAD: { key } });
27
+
28
+ onParams = (params: Record<string, unknown>) => window.ym?.(this.PARAMS.METRIC.YA_COUNTER_ID, "params", params);
29
+ onReachGoal = (target: string) => window.ym?.(this.PARAMS.METRIC.YA_COUNTER_ID, "reachGoal", target);
30
+ }
package/src/types.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type { VnePluginConstantsMapRegistry, VnePluginEventsMapRegistry, VnePluginParamsMapRegistry, VnePluginSettingsMapRegistry } from "@vnejs/shared";
2
+ import type { PluginName as StackPluginName, SubscribeEvents as StackSubscribeEvents } from "@vnejs/plugins.core.stack.contract";
3
+ import type { PluginName, Params, SubscribeEvents } from "@vnejs/plugins.platform.metric.contract";
4
+ import type { PluginName as SavePluginName, SubscribeEvents as SaveSubscribeEvents } from "@vnejs/plugins.save.contract";
5
+
6
+ export type MetricPluginEvents = VnePluginEventsMapRegistry &
7
+ Record<PluginName, SubscribeEvents> &
8
+ Record<StackPluginName, StackSubscribeEvents> &
9
+ Record<SavePluginName, SaveSubscribeEvents>;
10
+
11
+ export type MetricPluginConstants = VnePluginConstantsMapRegistry;
12
+
13
+ export type MetricPluginSettings = VnePluginSettingsMapRegistry;
14
+
15
+ export type MetricPluginParams = VnePluginParamsMapRegistry & Record<PluginName, Params>;
@@ -0,0 +1,51 @@
1
+ export type MetricStackPayload = {
2
+ action?: string;
3
+ label?: string;
4
+ };
5
+
6
+ export type MetricSavePayload = {
7
+ key?: string;
8
+ };
9
+
10
+ export type YmFunction = ((counterId: number, method: string, ...args: unknown[]) => void) & {
11
+ a?: unknown[][];
12
+ l?: number;
13
+ };
14
+
15
+ const TAG_SRC = "https://mc.yandex.ru/metrika/tag.js";
16
+
17
+ export const YANDEX_METRIKA_INIT_OPTIONS = {
18
+ defer: true,
19
+ clickmap: true,
20
+ trackLinks: true,
21
+ accurateTrackBounce: true,
22
+ } as const;
23
+
24
+ const ensureYmLoader = () => {
25
+ window.ym =
26
+ window.ym ||
27
+ function (...args: unknown[]) {
28
+ const ym = window.ym as YmFunction;
29
+ ym.a = ym.a || [];
30
+ ym.a.push(args);
31
+ };
32
+
33
+ if (Array.from(document.scripts).some((script) => script.src === TAG_SRC)) return;
34
+
35
+ (window.ym as YmFunction).l = Date.now();
36
+
37
+ const script = document.createElement("script");
38
+ const firstScript = document.getElementsByTagName("script")[0];
39
+ script.async = true;
40
+ script.src = TAG_SRC;
41
+ firstScript?.parentNode?.insertBefore(script, firstScript);
42
+ };
43
+
44
+ export const initYandexMetrika = (counterId: number) => {
45
+ if (!counterId) return;
46
+
47
+ ensureYmLoader();
48
+
49
+ window.ym?.(counterId, "init", YANDEX_METRIKA_INIT_OPTIONS);
50
+ window.ym?.(counterId, "hit", window.location.href);
51
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": "@vnejs/configs.ts-common/tsconfig.json",
3
+ "compilerOptions": {
4
+ "rootDir": "src",
5
+ "outDir": "dist"
6
+ },
7
+ "include": ["src/**/*.ts", "src/**/*.d.ts"],
8
+ "exclude": ["dist", "node_modules"]
9
+ }
package/const/const.js DELETED
File without changes
package/const/events.js DELETED
@@ -1,4 +0,0 @@
1
- export const SUBSCRIBE_EVENTS = {
2
- METRIC_GOAL: "vne.metric.goal",
3
- METRIC_PARAMS: "vne.metric.params",
4
- };
package/const/params.js DELETED
@@ -1 +0,0 @@
1
- export const YA_COUNTER_ID = 0;
package/const/settings.js DELETED
File without changes
package/index.js DELETED
@@ -1,9 +0,0 @@
1
- import { regPlugin } from "@vnejs/shared";
2
-
3
- import * as constants from "./const/const";
4
- import { SUBSCRIBE_EVENTS } from "./const/events";
5
- import * as params from "./const/params";
6
-
7
- import { YaMetrika } from "./modules/yandex";
8
-
9
- regPlugin("METRIC", { constants, events: SUBSCRIBE_EVENTS, params }, [YaMetrika]);
package/modules/yandex.js DELETED
@@ -1,46 +0,0 @@
1
- import { Module } from "@vnejs/module";
2
-
3
- export class YaMetrika extends Module {
4
- name = "ya-metrika";
5
-
6
- subscribe = () => {
7
- if (!this.PARAMS.METRIC.YA_COUNTER_ID) return;
8
-
9
- this.on(this.EVENTS.STACK.EMIT, this.onStack);
10
-
11
- this.on(this.EVENTS.SAVE.CREATE, this.onSave);
12
- this.on(this.EVENTS.SAVE.LOAD, this.onLoad);
13
-
14
- this.on(this.EVENTS.METRIC.METRIC_GOAL, this.onReachGoal);
15
- this.on(this.EVENTS.METRIC.METRIC_PARAMS, this.onParams);
16
- };
17
- init = () =>
18
- new Promise((resolve) => {
19
- if (!this.PARAMS.METRIC.YA_COUNTER_ID) return resolve();
20
-
21
- (function (m, e, t, r, i, k, a) {
22
- m[i] =
23
- m[i] ||
24
- function () {
25
- (m[i].a = m[i].a || []).push(arguments);
26
- };
27
- m[i].l = 1 * new Date();
28
- for (var j = 0; j < document.scripts.length; j++) {
29
- if (document.scripts[j].src === r) {
30
- return;
31
- }
32
- }
33
- (k = e.createElement(t)), (a = e.getElementsByTagName(t)[0]), (k.async = 1), (k.src = r), a.parentNode.insertBefore(k, a);
34
- })(window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym");
35
-
36
- window.ym(this.PARAMS.METRIC.YA_COUNTER_ID, "init", { clickmap: true, trackLinks: true, accurateTrackBounce: true });
37
- resolve();
38
- });
39
-
40
- onStack = ({ action, label }) => this.onParams({ VNE_STACK: { action, label } });
41
- onSave = ({ key }) => this.onParams({ VNE_SAVE: { key } });
42
- onLoad = ({ key }) => this.onParams({ VNE_LOAD: { key } });
43
-
44
- onParams = (params) => window.ym(this.PARAMS.METRIC.YA_COUNTER_ID, "params", params);
45
- onReachGoal = (target) => window.ym(this.PARAMS.METRIC.YA_COUNTER_ID, "reachGoal", target);
46
- }