Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions src/dispatchers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { providersList } from '../providers';
import { EventData, PropertiesType, UserPropertiesType } from './dispatchers.types';
import {
EventData,
PropertiesType,
UserPropertiesType,
} from './dispatchers.types';
import { checkIfMixPanelIsInitialized } from '../utils';

/**
Expand All @@ -10,7 +14,9 @@ import { checkIfMixPanelIsInitialized } from '../utils';
* @returns {void}
*/
export const dispatchEventToAllProviders = (eventData: EventData): void => {
const localStorageProvidersList = JSON.parse(localStorage?.getItem('_bl_providers') as string);
const localStorageProvidersList = JSON.parse(
localStorage?.getItem('_bl_providers') as string,
);

const providersFiltered = localStorageProvidersList
? providersList.filter((item) => localStorageProvidersList.includes(item.name))
Expand Down Expand Up @@ -48,13 +54,39 @@ const sendScreenEvent = (screen: string): void => {
}
};

let defaultProperties: PropertiesType = {};

const saveDefaultPropertiesToLocalStorage = (properties: PropertiesType): void => {
localStorage.setItem('_bl_props', JSON.stringify(properties));
};

const loadDefaultPropertiesFromLocalStorage = (): PropertiesType => {
const storedProperties = localStorage.getItem('_bl_props');
return storedProperties ? JSON.parse(storedProperties) : {};
};

const setDefaultProperties = (properties: PropertiesType): void => {
defaultProperties = { ...properties };
saveDefaultPropertiesToLocalStorage(defaultProperties);
};

const sendCustomEvent = (event: string, properties: PropertiesType): void => {
if (Object.keys(defaultProperties).length === 0) {
defaultProperties = loadDefaultPropertiesFromLocalStorage();
}
const mergedProperties = {
...defaultProperties,
...properties,
};

if (currentEnvironment === 'development') {
console.log(
`[blu-lytics]: Custom event: ${event} - ${JSON.stringify(properties)}`,
`[blu-lytics]: Custom event: ${event} - ${JSON.stringify(
mergedProperties,
)}`,
);
} else {
dispatchEventToAllProviders({ event, properties });
dispatchEventToAllProviders({ event, properties: mergedProperties });
}
};

Expand All @@ -64,11 +96,18 @@ const sendUserIdentification = (
): void => {
if (currentEnvironment === 'development') {
console.log(
`[blu-lytics]: User identification: ${id} - ${JSON.stringify(userProperties)}`,
`[blu-lytics]: User identification: ${id} - ${JSON.stringify(
userProperties,
)}`,
);
} else {
dispatchEventToAllProviders({ id, userProperties });
}
};

export { sendCustomEvent, sendScreenEvent, sendUserIdentification };
export {
sendCustomEvent,
sendScreenEvent,
sendUserIdentification,
setDefaultProperties,
};
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export { initializeProviders } from './initializers';
export { sendCustomEvent, sendScreenEvent, sendUserIdentification } from './dispatchers';
export {
sendCustomEvent, sendScreenEvent, sendUserIdentification, setDefaultProperties,
} from './dispatchers';
Loading