-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Edgee 406 react sdk export edgee sdk methods (#7)
* init * Add DataCollection Method Support with Queue and Types * update types * Add Consent handling to exports * update * Update Readme
- Loading branch information
Showing
6 changed files
with
197 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useCallback } from 'react'; | ||
import { track, page, user, consent } from './data-collection'; | ||
import { Consent, Page, Track, User } from './data-collection.types'; | ||
|
||
/** | ||
* Custom hook `useEdgeeDataCollection` | ||
* | ||
* Provides access to Edgee's tracking methods: `track`, `page`, `user`, and `consent`. | ||
* - Returns memoized functions for tracking events and user identification. | ||
* | ||
* @returns {{ | ||
* track: (eventData: Track) => void; | ||
* page: (pageData: Page) => void; | ||
* user: (userData: User) => void; | ||
* consent: (consent: Consent) => 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} trackData - The event details to be sent to Edgee. | ||
*/ | ||
const trackEvent = useCallback((trackData: Track) => { | ||
track(trackData); | ||
}, []); | ||
|
||
/** | ||
* Identifies a user and associates events with them. | ||
* | ||
* @param {User} userData - The user data to be sent to Edgee. | ||
*/ | ||
const userEvent = useCallback((userData: User) => { | ||
user(userData); | ||
}, []); | ||
|
||
/** | ||
* Set the consent for a user. | ||
* | ||
* @param {Consent} status - The status of the consent for the user. | ||
*/ | ||
const setConsent = useCallback((status: Consent) => { | ||
consent(status); | ||
}, []); | ||
|
||
return { track: trackEvent, page: pageEvent, user: userEvent, consent: setConsent }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Edgee } from '../../index'; | ||
import { | ||
Page, | ||
User, | ||
Track, | ||
Consent, | ||
EdgeeMethod, | ||
EdgeeConsentMethod, | ||
QueuedEvent, | ||
QueuedConsentEvent, | ||
} from './data-collection.types'; | ||
|
||
const eventQueue: (QueuedEvent<Page | User | Track | Consent> | QueuedConsentEvent)[] = []; | ||
|
||
/** | ||
* 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 event = eventQueue.shift(); | ||
if (!event) return; | ||
try { | ||
event.method === 'consent' | ||
? (window.edgee as Edgee)[event.method](event.args[0]) | ||
: (window.edgee as Edgee)[event.method](...event.args); | ||
} catch (e) { | ||
return e; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
/** | ||
* 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`, `page`). | ||
* @returns {EdgeeMethod<T>} A function that queues or sends the event. | ||
*/ | ||
const createMethod = | ||
<T extends Page | User | Track>(method: Exclude<keyof Edgee, 'consent'>): EdgeeMethod<T> => | ||
(arg: T, components?: Record<string, boolean>) => { | ||
if (typeof window !== 'undefined' && window.edgee) { | ||
flushQueue(); | ||
window.edgee[method](arg, components); | ||
} else { | ||
eventQueue.push({ method, args: [arg, components] }); | ||
} | ||
}; | ||
|
||
/** | ||
* Creates a consent method that queues events if `window.edgee` is not available yet. | ||
* @returns {EdgeeConsentMethod} A function that queues or sends the consent event. | ||
*/ | ||
const createConsentMethod = (): EdgeeConsentMethod => (arg: Consent) => { | ||
if (typeof window !== 'undefined' && window.edgee) { | ||
flushQueue(); | ||
window.edgee.consent(arg); | ||
} else { | ||
eventQueue.push({ method: 'consent', args: [arg] }); | ||
} | ||
}; | ||
|
||
export const track: EdgeeMethod<Track> = createMethod<Track>('track'); | ||
export const user: EdgeeMethod<User> = createMethod<User>('user'); | ||
export const page: EdgeeMethod<Page> = createMethod<Page>('page'); | ||
export const consent: EdgeeConsentMethod = createConsentMethod(); | ||
|
||
const EdgeeSDK = { track, user, page, consent }; | ||
export default EdgeeSDK; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Edgee } from '../../index'; | ||
|
||
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>; | ||
} | ||
|
||
export type Consent = 'granted' | 'denied' | 'pending'; | ||
|
||
export type EdgeeMethod<T> = (arg: T, components?: Record<string, boolean>) => void; | ||
export type EdgeeConsentMethod = (arg: Consent) => void; | ||
|
||
export type QueuedEvent<T> = { | ||
method: Exclude<keyof Edgee, 'consent'>; | ||
args: [T, Record<string, boolean>?]; | ||
}; | ||
|
||
export type QueuedConsentEvent = { | ||
method: 'consent'; | ||
args: [Consent]; | ||
}; |