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 1 commit
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
7 changes: 7 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import * as PropTypes from 'prop-types';
import * as React from 'react';
import { useEdgeeDataCollection } from './lib/data-collection/data-collection.hook';
import { Consent } from './lib/data-collection/data-collection.types';

/**
* Interface representing the Edgee analytics object.
Expand All @@ -29,6 +30,12 @@ export interface Edgee {
* @param components - Optional object specifying components for the event.
*/
user: (arg?: string | object, components?: object) => void;

/**
* Consent management system that allows you to control data collection and anonymization based on user consent status.
* @param {Consent} status - Consent state. Could be pending, denied or granted.
*/
consent: (status: Consent) => void;
}

// Extends the global Window interface to include the Edgee analytics object.
Expand Down
11 changes: 8 additions & 3 deletions src/lib/data-collection/data-collection.hook.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback } from 'react';
import { track, page, user } from './data-collection';
import { Page, Track, User } from './data-collection.types';
import { track, page, user, consent } from './data-collection';
import { Consent, Page, Track, User } from './data-collection.types';

/**
* Custom hook `useEdgeeDataCollection`
Expand All @@ -12,6 +12,7 @@ import { Page, Track, User } from './data-collection.types';
* track: (eventData: Track) => void;
* page: (pageData: Page) => void;
* user: (userData: User) => void;
* consent: (consent: Consent) => void;
* }} Object containing the tracking functions.
*/
export const useEdgeeDataCollection = () => {
Expand Down Expand Up @@ -42,5 +43,9 @@ export const useEdgeeDataCollection = () => {
user(userData);
}, []);

return { track: trackEvent, page: pageEvent, user: setUser };
const setConsent = useCallback((status: Consent) => {
consent(status);
}, []);

return { track: trackEvent, page: pageEvent, user: setUser, consent: setConsent };
};
52 changes: 38 additions & 14 deletions src/lib/data-collection/data-collection.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,69 @@
import { Edgee } from '../../index';
import { Page, User, Track } from './data-collection.types';
import {
Page,
User,
Track,
Consent,
EdgeeMethod,
EdgeeConsentMethod,
QueuedEvent,
QueuedConsentEvent,
} 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>[] = [];
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 { method, args } = eventQueue.shift()!;
(window.edgee as Edgee)[method](...args);
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`, or `page`).
* @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: keyof Edgee): EdgeeMethod<T> =>
<T extends Page | User | Track>(method: Exclude<keyof Edgee, 'consent'>): EdgeeMethod<T> =>
(arg: T, components?: Record<string, boolean>) => {
if (typeof window !== 'undefined' && window.edgee) {
window.edgee[method](arg, components);
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 };
const EdgeeSDK = { track, user, page, consent };
export default EdgeeSDK;
17 changes: 17 additions & 0 deletions src/lib/data-collection/data-collection.types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Edgee } from '../../index';

export interface Page {
name?: string;
category?: string;
Expand All @@ -21,3 +23,18 @@ 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];
};
Loading