Skip to content

Commit

Permalink
fix: fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
imsitnikov committed Sep 20, 2023
1 parent 9faa4ca commit ddb8ae5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ You can define environment variables for dev-mode in .env.development file withi

To start using any analytics, pass a handler to the constructor. The handler must be created on a project side. The handler will receive the `default` and `custom` event objects. The passed handler will be fired on a button, link, navigation, and control clicks. As one handler is used for all events treatment, pay attention to how to treat different events while creating the handler. There are predefined fields that serve to help you to build complex logic.

Pass `autoEvents: true` to constructor to fire automatically configured events. Pass `multipleEvents: true` to constructor to fire events multiple times.
Pass `autoEvents: true` to constructor to fire automatically configured events.

```ts
function sendEvents(events: MyEventType []) {
Expand All @@ -232,7 +232,7 @@ function sendEvents(events: MyEventType []) {
<PageConstructorProvider
...

analytics={{sendEvents, autoEvents: true, multipleEvents: true}}
analytics={{sendEvents, autoEvents: true}}

...
/>
Expand Down
1 change: 0 additions & 1 deletion src/context/analyticsContext/analyticsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {AnalyticsEvent} from '../../models';
export interface AnalyticsContextProps {
sendEvents?: (events: AnalyticsEvent[]) => void;
autoEvents?: boolean;
multipleEvents?: boolean;
}

export const AnalyticsContext = React.createContext<AnalyticsContextProps>({});
10 changes: 3 additions & 7 deletions src/hooks/useAnalytics.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import {useContext, useMemo} from 'react';

import {memoize} from 'lodash';

import {AnalyticsContext} from '../context/analyticsContext';
import {BlockIdContext} from '../context/blockIdContext';
import {AnalyticsEvent, PredefinedEventTypes} from '../models';

export const useAnalytics = (name = '', target?: string) => {
const {sendEvents, autoEvents, multipleEvents} = useContext(AnalyticsContext);
const {sendEvents, autoEvents} = useContext(AnalyticsContext);
const context = useContext(BlockIdContext);
const defaultEvent = useMemo(
() =>
Expand All @@ -23,12 +21,12 @@ export const useAnalytics = (name = '', target?: string) => {
);

if (!sendEvents) {
return memoize(() => {});
return () => {};
}

const defaultEvents = defaultEvent && autoEvents ? [defaultEvent] : [];

const handler = (
return (
e?: AnalyticsEvent | AnalyticsEvent[] | null,
additionalContext?: Record<string, string>,
) => {
Expand All @@ -51,6 +49,4 @@ export const useAnalytics = (name = '', target?: string) => {

sendEvents(preparedEvents);
};

return multipleEvents ? handler : memoize(handler);
};

0 comments on commit ddb8ae5

Please sign in to comment.