@gravity-ui/page-constructor 1.15.0-alpha.6 → 1.15.0-alpha.8

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.
package/README.md CHANGED
@@ -43,7 +43,8 @@ interface PageConstructorProviderProps {
43
43
  isMobile?: boolean; //A flag indicating that the code is executed in mobile mode.
44
44
  locale?: LocaleContextProps; //Info about the language and domain (used when generating and formatting links).
45
45
  location?: Location; //API of the browser or router history, the page URL.
46
- metrika?: Metrika; //Functions for sending analytics
46
+ analytics?: AnalyticsContextProps; // function to handle analytics event
47
+
47
48
  ssrConfig?: SSR; //A flag indicating that the code is run on the server size.
48
49
  theme?: 'light' | 'dark'; //Theme to render the page with.
49
50
  }
@@ -81,10 +82,17 @@ interface SSR {
81
82
  isServer?: boolean;
82
83
  }
83
84
 
84
- interface Metrika {
85
- metrika?: any;
86
- pixel?: any;
87
- }
85
+ type AnalyticsCounters = {
86
+ include?: string[];
87
+ exclude?: string[];
88
+ };
89
+
90
+ type AnalyticsEvent<T = {}> = T & {
91
+ name: string;
92
+ type?: string;
93
+ counters?: AnalyticsCounters;
94
+ blockName?: string;
95
+ };
88
96
 
89
97
  interface NavigationData {
90
98
  logo: NavigationLogo;
@@ -211,6 +219,68 @@ import {configure, Lang} from '@gravity-ui/page-constructor';
211
219
  configure({lang: Lang.En});
212
220
  ```
213
221
 
222
+ ### Analytics
223
+
224
+ #### Init
225
+
226
+ To start use any Analytics it is necessary to pass a handler to the constructor. The handler will receive `default` and `custom` event objects.
227
+
228
+ ```ts
229
+ function sendEvents(event: AnalyticsEvent | AnalyticsEvent []) {
230
+ ...
231
+ }
232
+
233
+ <PageConstructorProvider
234
+ ...
235
+
236
+ analytics={{sendEvents}}
237
+
238
+ ...
239
+ />,
240
+ ```
241
+
242
+ An event object has only one required option - `name`. It also has predefined fields, which serve to help manage complex logic. For example, `counter.include` can help to send event in a particular counter if several are used in a project. The handler must be created on a project side.
243
+
244
+ ```ts
245
+ type AnalyticsCounters = {
246
+ include?: string[];
247
+ exclude?: string[];
248
+ };
249
+
250
+ type AnalyticsEvent<T = {}> = T & {
251
+ name: string;
252
+ type?: string;
253
+ counters?: AnalyticsCounters;
254
+ blockName?: string;
255
+ };
256
+ ```
257
+
258
+ ```ts
259
+ const isCounterAllowed = (counter: Counter, counters?: AnalyticsCounters) => {
260
+ if (!counters) {
261
+ return true;
262
+ }
263
+
264
+ if (counters.exclude?.includes(counter)) {
265
+ return false;
266
+ } else if (counters.include?.includes(counter)) {
267
+ return true;
268
+ }
269
+
270
+ return false;
271
+ };
272
+
273
+ if (isCounterAllowed(counterName, counters)) {
274
+ analyticsCounter.reachGoal(counterName, name, parameters);
275
+ }
276
+ ```
277
+
278
+ #### Reserved types
279
+
280
+ `default` - fires on every click
281
+ `play` - fires on
282
+ `stop` - fires on
283
+
214
284
  ## Development
215
285
 
216
286
  ```bash
@@ -8,13 +8,11 @@ const useAnalytics = (defaultEvent) => {
8
8
  if (!sendEvents) {
9
9
  return undefined;
10
10
  }
11
+ const defaultEvents = defaultEvent ? [defaultEvent] : [];
11
12
  return (e) => {
12
- let events = [];
13
- if (defaultEvent && e) {
14
- events = Array.isArray(e) ? [...e, defaultEvent] : [e, defaultEvent];
15
- }
16
- else if (e) {
17
- events = e;
13
+ let events = defaultEvents;
14
+ if (e) {
15
+ events = Array.isArray(e) ? [...events, ...e] : [...events, e];
18
16
  }
19
17
  if (!events) {
20
18
  return;
@@ -9,7 +9,7 @@ const components_1 = require("../../components");
9
9
  const ThemeValueContext_1 = require("../../context/theme/ThemeValueContext");
10
10
  const utils_2 = require("../../components/Media/Image/utils");
11
11
  const hooks_1 = require("../../hooks");
12
- const constants_1 = require("src/constants");
12
+ const constants_1 = require("../../constants");
13
13
  const b = (0, utils_1.block)('quote');
14
14
  const Quote = (props) => {
15
15
  const { theme: textTheme = 'light', color, image, border = 'shadow', text, logo, author, url, buttonText, blockName = models_1.SubBlockType.Quote, } = props;
@@ -5,13 +5,11 @@ export const useAnalytics = (defaultEvent) => {
5
5
  if (!sendEvents) {
6
6
  return undefined;
7
7
  }
8
+ const defaultEvents = defaultEvent ? [defaultEvent] : [];
8
9
  return (e) => {
9
- let events = [];
10
- if (defaultEvent && e) {
11
- events = Array.isArray(e) ? [...e, defaultEvent] : [e, defaultEvent];
12
- }
13
- else if (e) {
14
- events = e;
10
+ let events = defaultEvents;
11
+ if (e) {
12
+ events = Array.isArray(e) ? [...events, ...e] : [...events, e];
15
13
  }
16
14
  if (!events) {
17
15
  return;
@@ -6,7 +6,7 @@ import { Author, Image, HTML } from '../../components';
6
6
  import { ThemeValueContext } from '../../context/theme/ThemeValueContext';
7
7
  import { getMediaImage } from '../../components/Media/Image/utils';
8
8
  import { useAnalytics } from '../../hooks';
9
- import { DEFAULT_EVENT_TYPE } from 'src/constants';
9
+ import { DEFAULT_EVENT_TYPE } from '../../constants';
10
10
  import './Quote.css';
11
11
  const b = block('quote');
12
12
  const Quote = (props) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gravity-ui/page-constructor",
3
- "version": "1.15.0-alpha.6",
3
+ "version": "1.15.0-alpha.8",
4
4
  "description": "Gravity UI Page Constructor",
5
5
  "license": "MIT",
6
6
  "repository": {