Skip to content

Commit

Permalink
feat: add multipleEvents prop for useAnalytics
Browse files Browse the repository at this point in the history
  • Loading branch information
imsitnikov committed Sep 20, 2023
1 parent fa570c8 commit 9faa4ca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 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 `autoEvents: true` to constructor to fire automatically configured events. Pass `multipleEvents: true` to constructor to fire events multiple times.

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

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

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

export const AnalyticsContext = React.createContext<AnalyticsContextProps>({});
52 changes: 26 additions & 26 deletions src/hooks/useAnalytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {BlockIdContext} from '../context/blockIdContext';
import {AnalyticsEvent, PredefinedEventTypes} from '../models';

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

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

return memoize(
(
e?: AnalyticsEvent | AnalyticsEvent[] | null,
additionalContext?: Record<string, string>,
) => {
let events: AnalyticsEvent[] = defaultEvents;

if (e) {
events = Array.isArray(e) ? [...events, ...e] : [...events, e];
}

if (!events) {
return;
}

const preparedEvents = additionalContext
? events.map((event) => ({
...event,
...additionalContext,
}))
: events;

sendEvents(preparedEvents);
},
);
const handler = (
e?: AnalyticsEvent | AnalyticsEvent[] | null,
additionalContext?: Record<string, string>,
) => {
let events: AnalyticsEvent[] = defaultEvents;

if (e) {
events = Array.isArray(e) ? [...events, ...e] : [...events, e];
}

if (!events) {
return;
}

const preparedEvents = additionalContext
? events.map((event) => ({
...event,
...additionalContext,
}))
: events;

sendEvents(preparedEvents);
};

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

0 comments on commit 9faa4ca

Please sign in to comment.