From 27f4e82886315cd7899cc02ba126316692c19d8e Mon Sep 17 00:00:00 2001 From: Jer1605 Date: Thu, 6 Feb 2025 16:30:07 +0100 Subject: [PATCH] Export Types & Rework Type names --- package.json | 2 +- src/index.tsx | 13 +++++----- .../data-collection/data-collection.hook.ts | 26 +++++++++---------- src/lib/data-collection/data-collection.ts | 20 +++++++------- .../data-collection/data-collection.types.ts | 12 ++++----- 5 files changed, 37 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 8d2cbb6..032ce0d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-edgee", - "version": "1.2.1", + "version": "1.2.2", "description": "React component to use the Edgee SDK", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/index.tsx b/src/index.tsx index 19efca1..e2de7f9 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -3,7 +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'; +import { ConsentStatus } from './lib/data-collection/data-collection.types'; import { flushQueue } from './lib/data-collection/data-collection'; /** @@ -33,10 +33,10 @@ export interface Edgee { 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. + * ConsentStatus management system that allows you to control data collection and anonymization based on user consent status. + * @param {ConsentStatus} status - ConsentStatus state. Could be pending, denied or granted. */ - consent: (status: Consent) => void; + consent: (status: ConsentStatus) => void; } // Extends the global Window interface to include the Edgee analytics object. @@ -79,7 +79,7 @@ const EdgeeSdk = ({ src, dataInline }: EdgeeSdkProps): JSX.Element => { history.pushState = (...args) => { // Call the original pushState method const result = pushState.apply(history, args); - // Track the page view after a short delay to ensure the page has changed + // TrackData the page view after a short delay to ensure the page has changed setTimeout(() => { window.edgee.page(); }, 200); @@ -93,7 +93,7 @@ const EdgeeSdk = ({ src, dataInline }: EdgeeSdkProps): JSX.Element => { history.replaceState = (...args) => { // Call the original replaceState method const result = replaceState.apply(history, args); - // Track the page view after a short delay + // TrackData the page view after a short delay setTimeout(() => { window.edgee.page(); }, 200); @@ -180,4 +180,5 @@ EdgeeDataLayer.propTypes = { data: PropTypes.object, }; +export * from './lib/data-collection/data-collection.types'; export default EdgeeSdk; diff --git a/src/lib/data-collection/data-collection.hook.ts b/src/lib/data-collection/data-collection.hook.ts index 17229cd..3c85d04 100644 --- a/src/lib/data-collection/data-collection.hook.ts +++ b/src/lib/data-collection/data-collection.hook.ts @@ -1,6 +1,6 @@ import { useCallback } from 'react'; import { track, page, user, consent } from './data-collection'; -import { Consent, Page, Track, User } from './data-collection.types'; +import { ConsentStatus, PageData, TrackData, UserData } from './data-collection.types'; /** * Custom hook `useEdgeeDataCollection` @@ -9,46 +9,46 @@ import { Consent, Page, Track, User } from './data-collection.types'; * - 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; + * track: (eventData: TrackData) => void; + * page: (pageData: PageData) => void; + * user: (userData: UserData) => void; + * consent: (consent: ConsentStatus) => void; * }} Object containing the tracking functions. */ export const useEdgeeDataCollection = () => { /** * Tracks a page event. * - * @param {Page} pageData - The page details to be sent to Edgee. + * @param {PageData} pageData - The page details to be sent to Edgee. */ - const pageEvent = useCallback((pageData: Page) => { + const pageEvent = useCallback((pageData: PageData) => { page(pageData); }, []); /** * Tracks a custom event. * - * @param {Track} trackData - The event details to be sent to Edgee. + * @param {TrackData} trackData - The event details to be sent to Edgee. */ - const trackEvent = useCallback((trackData: Track) => { + const trackEvent = useCallback((trackData: TrackData) => { track(trackData); }, []); /** * Identifies a user and associates events with them. * - * @param {User} userData - The user data to be sent to Edgee. + * @param {UserData} userData - The user data to be sent to Edgee. */ - const userEvent = useCallback((userData: User) => { + const userEvent = useCallback((userData: UserData) => { user(userData); }, []); /** * Set the consent for a user. * - * @param {Consent} status - The status of the consent for the user. + * @param {ConsentStatus} status - The status of the consent for the user. */ - const setConsent = useCallback((status: Consent) => { + const setConsent = useCallback((status: ConsentStatus) => { consent(status); }, []); diff --git a/src/lib/data-collection/data-collection.ts b/src/lib/data-collection/data-collection.ts index ed208a9..6a4b9b9 100644 --- a/src/lib/data-collection/data-collection.ts +++ b/src/lib/data-collection/data-collection.ts @@ -1,16 +1,16 @@ import { Edgee } from '../../index'; import { - Page, - User, - Track, - Consent, + PageData, + UserData, + TrackData, + ConsentStatus, EdgeeMethod, EdgeeConsentMethod, QueuedEvent, QueuedConsentEvent, } from './data-collection.types'; -const eventQueue: (QueuedEvent | QueuedConsentEvent)[] = []; +const eventQueue: (QueuedEvent | QueuedConsentEvent)[] = []; /** * Flushes the event queue and sends all stored events to `window.edgee` if available. @@ -37,7 +37,7 @@ export const flushQueue = () => { * @returns {EdgeeMethod} A function that queues or sends the event. */ const createMethod = - (method: Exclude): EdgeeMethod => + (method: Exclude): EdgeeMethod => (arg: T, components?: Record) => { if (typeof window !== 'undefined' && window.edgee) { flushQueue(); @@ -51,7 +51,7 @@ const createMethod = * 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) => { +const createConsentMethod = (): EdgeeConsentMethod => (arg: ConsentStatus) => { if (typeof window !== 'undefined' && window.edgee) { flushQueue(); window.edgee.consent(arg); @@ -60,9 +60,9 @@ const createConsentMethod = (): EdgeeConsentMethod => (arg: Consent) => { } }; -export const track: EdgeeMethod = createMethod('track'); -export const user: EdgeeMethod = createMethod('user'); -export const page: EdgeeMethod = createMethod('page'); +export const track: EdgeeMethod = createMethod('track'); +export const user: EdgeeMethod = createMethod('user'); +export const page: EdgeeMethod = createMethod('page'); export const consent: EdgeeConsentMethod = createConsentMethod(); const EdgeeSDK = { track, user, page, consent }; diff --git a/src/lib/data-collection/data-collection.types.ts b/src/lib/data-collection/data-collection.types.ts index cda5c7f..b9657d7 100644 --- a/src/lib/data-collection/data-collection.types.ts +++ b/src/lib/data-collection/data-collection.types.ts @@ -1,6 +1,6 @@ import { Edgee } from '../../index'; -export interface Page { +export interface PageData { name?: string; category?: string; keywords?: string[]; @@ -12,22 +12,22 @@ export interface Page { properties?: Record; } -export interface User { +export interface UserData { user_id?: string; anonymous_id?: string; edgee_id: string; properties?: Record; } -export interface Track { +export interface TrackData { name?: string; properties?: Record; } -export type Consent = 'granted' | 'denied' | 'pending'; +export type ConsentStatus = 'granted' | 'denied' | 'pending'; export type EdgeeMethod = (arg: T, components?: Record) => void; -export type EdgeeConsentMethod = (arg: Consent) => void; +export type EdgeeConsentMethod = (arg: ConsentStatus) => void; export type QueuedEvent = { method: Exclude; @@ -36,5 +36,5 @@ export type QueuedEvent = { export type QueuedConsentEvent = { method: 'consent'; - args: [Consent]; + args: [ConsentStatus]; };