Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edgee 406 react sdk export edgee sdk methods #7

Merged
merged 6 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-edgee",
"version": "1.1.1",
"version": "1.1.2",
"description": "React component to use the Edgee SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
5 changes: 3 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import * as PropTypes from 'prop-types';
import * as React from 'react';
import { useEdgeeDataCollection } from './lib/data-collection/data-collection.hook';

/**
* Interface representing the Edgee analytics object.
* Provides methods for tracking page, track, and user events.
*/
interface Edgee {
export interface Edgee {
/**
* Tracks a page view event.
* @param arg - The event name or an object containing event properties.
Expand Down Expand Up @@ -156,7 +157,7 @@ const EdgeeDataLayer = ({ data }: EdgeeDataLayerProps): JSX.Element => {
);
};

export { EdgeeSdk, EdgeeDataLayer };
export { EdgeeSdk, EdgeeDataLayer, useEdgeeDataCollection };

EdgeeSdk.propTypes = {
src: PropTypes.string,
Expand Down
46 changes: 46 additions & 0 deletions src/lib/data-collection/data-collection.hook.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { useCallback } from 'react';
import { track, page, user } from './data-collection';
import { Page, Track, User } from './data-collection.types';

/**
* Custom hook `useEdgeeDataCollection`
*
* Provides access to Edgee's tracking methods: `track`, `page`, and `user`.
* - Returns memoized functions for tracking events and user identification.
*
* @returns {{
* track: (eventData: Track) => void;
* page: (pageData: Page) => void;
* user: (userData: User) => void;
* }} Object containing the tracking functions.
*/
export const useEdgeeDataCollection = () => {
/**
* Tracks a page event.
*
* @param {Page} pageData - The page details to be sent to Edgee.
*/
const pageEvent = useCallback((pageData: Page) => {
page(pageData);
}, []);

/**
* Tracks a custom event.
*
* @param {Track} eventData - The event details to be sent to Edgee.
*/
const trackEvent = useCallback((eventData: Track) => {
track(eventData);
}, []);

/**
* Identifies a user and associates events with them.
*
* @param {User} userData - The user data to be sent to Edgee.
*/
const setUser = useCallback((userData: User) => {
user(userData);
}, []);

return { track: trackEvent, page: pageEvent, user: setUser };
};
45 changes: 45 additions & 0 deletions src/lib/data-collection/data-collection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Edgee } from '../../index';
import { Page, User, Track } from './data-collection.types';

type EdgeeMethod<T> = (arg: T, components?: Record<string, boolean>) => void;
interface QueuedEvent<T> {
method: keyof Edgee;
args: [T, Record<string, boolean>?];
}

const eventQueue: QueuedEvent<Page | User | Track>[] = [];

/**
* Flushes the event queue and sends all stored events to `window.edgee` if available.
*/
const flushQueue = () => {
if (typeof window !== 'undefined' && window.edgee) {
while (eventQueue.length > 0) {
const { method, args } = eventQueue.shift()!;
(window.edgee as Edgee)[method](...args);
}
}
};

/**
* Creates a method that queues events if `window.edgee` is not available yet.
* @param {keyof Edgee} method - The name of the tracking method (`track`, `user`, or `page`).
* @returns {EdgeeMethod<T>} A function that queues or sends the event.
*/
const createMethod =
<T extends Page | User | Track>(method: keyof Edgee): EdgeeMethod<T> =>
(arg: T, components?: Record<string, boolean>) => {
if (typeof window !== 'undefined' && window.edgee) {
window.edgee[method](arg, components);
flushQueue();
} else {
eventQueue.push({ method, args: [arg, components] });
}
};

export const track: EdgeeMethod<Track> = createMethod<Track>('track');
export const user: EdgeeMethod<User> = createMethod<User>('user');
export const page: EdgeeMethod<Page> = createMethod<Page>('page');

const EdgeeSDK = { track, user, page };
export default EdgeeSDK;
23 changes: 23 additions & 0 deletions src/lib/data-collection/data-collection.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export interface Page {
name?: string;
category?: string;
keywords?: string[];
title?: string;
url?: string;
path?: string;
search?: string;
referrer?: string;
properties?: Record<string, unknown>;
}

export interface User {
user_id?: string;
anonymous_id?: string;
edgee_id: string;
properties?: Record<string, unknown>;
}

export interface Track {
name?: string;
properties?: Record<string, unknown>;
}
Loading