ns-events-library
v1.2.10
Published
newton school event library
Downloads
32
Maintainers
Readme
ns-events-library
The ns-events-library
is a versatile React library that simplifies event tracking for various analytics providers, helping you gain valuable insights into user interactions in your React applications.
Installation
Before getting started, ensure that you have React version 18.2.0 or higher installed in your project. You can add the ns-events-library
package to your project using npm:
npm install ns-events-library
Supported Analytics Providers
The ns-events-library seamlessly integrates with the following analytics providers:
Google Analytics
Clevertap
Amplitude
Mixpanel
Facebook
Taboola
Quora
Clarity
Microsoft
Event Types
The ns-events-library supports various event types to track user interactions and activities in your React application. You can use these event types when sending analytics events. Here are the available event types:
CLICK: 'click'
PAGE_LOAD: 'load'
HOVER: 'hover'
SCROLL: 'scroll'
EVENT_SUCCESS: 'event_success'
EVENT_FAILURE: 'event_failure'
TIME_SPENT_ON_PAGE: 'time_spent_on_page'
TIME_SPENT_ON_COMPONENT: 'time_spent_on_component'
VIEW: 'view'
KEY_DOWN: 'keydown'
CUT: 'cut'
COPY: 'copy'
PASTE: 'paste'
COMPONENT_LOAD: 'component_load'
Usage
1. Create a Context
Begin by creating a context to manage the sending of analytics events in your React application:
import { createContext } from 'react';
const SendAnalyticsEventContext = createContext();
2. Create a Custom Hook
Create a custom hook to facilitate the sending of analytics events in your application:
import { useContext, useCallback } from 'react';
export const useSendAnalyticsEvent = () => {
const { trackingDataEnrichedEvent } = useContext(SendAnalyticsEventContext);
const sendAnalytics = useCallback((event) => {
const { eventType, eventName, eventData = {} } = event;
trackingDataEnrichedEvent(eventType, eventName, eventData);
}, []);
return sendAnalytics;
};
3. Integrate with Your React App
In your React application, import the EventContext component from ns-events-library and wrap your app with it:
import { EventContext } from 'ns-events-library';
// ...
<EventContext
contextProvider={SendAnalyticsEventContext}
config={{
courseHash: 'xyz', // Replace with your course hash
userName: 'xyz', // Replace with the user's name
enabledList: [
'GOOGLE_ANALYTICS',
'CLEVERTAP'
// Add other enabled providers from the list of supported analytics providers
],
enabledData: [
{ accountId: 'add-tracking-id-here' },
{ accountId: 'add-tracking-id-here', softTrack: true },
// Add tracking IDs for the enabled providers in the same order as enabledList
],
// Pass useHistory hook to track URLs (if needed)
history: useHistory(),
}}
>
{/* Your React app components go here */}
</EventContext>
If you don't have a userName or courseHash, you can pass empty strings or any default values that are appropriate for your use case.
4. Environment Variable for Production
For the production environment, make sure to add the following environment variable to your .env file:
NEXT_PUBLIC_NODE_ENV=production
5. Send Events
To send events, use the sendAnalytics function like this:
const sendAnalytics = useSendAnalyticsEvent();
sendAnalytics({
eventType: 'click',
eventName: 'button-clicked',
eventData: {
// Send data you want to be tracked.
},
});
With these steps, your React application is ready to send analytics events to the selected providers effortlessly.
Remember to replace the placeholders with actual values or descriptions that are specific to your React library and its use cases.
Debugging Events
To enable debugging for events in your application, follow these steps:
Open your browser's developer tools.
Go to the "Application" tab.
In the "Local Storage" section, add a new key-value pair:
Key: debugEvents
Value: 1
This will enable event debugging, and you can inspect the event tracking in your browser's console for debugging purposes.