Skip to content

Commit

Permalink
feat: add multipleEvents prop for useAnalytics (#580)
Browse files Browse the repository at this point in the history
  • Loading branch information
imsitnikov authored Sep 20, 2023
1 parent fa570c8 commit 31d68be
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions src/hooks/useAnalytics.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import {useContext, useMemo} from 'react';

import {memoize} from 'lodash';

import {AnalyticsContext} from '../context/analyticsContext';
import {BlockIdContext} from '../context/blockIdContext';
import {AnalyticsEvent, PredefinedEventTypes} from '../models';
Expand All @@ -23,34 +21,32 @@ export const useAnalytics = (name = '', target?: string) => {
);

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

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);
},
);
return (
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);
};
};

0 comments on commit 31d68be

Please sign in to comment.