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 418 react sdk add flush at init #10

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all 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.2.0",
"version": "1.2.1",
"description": "React component to use the Edgee SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
5 changes: 5 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,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 { flushQueue } from './lib/data-collection/data-collection';

/**
* Interface representing the Edgee analytics object.
Expand Down Expand Up @@ -100,8 +101,12 @@ const EdgeeSdk = ({ src, dataInline }: EdgeeSdkProps): JSX.Element => {
};
})((window as Window).history);

if (window.edgee) flushQueue();
else window.addEventListener('edgee:loaded', flushQueue);

// Cleanup function to restore the original pushState and replaceState methods
return (): void => {
window.removeEventListener('edgee:loaded', flushQueue);
// No cleanup actions are defined here, but this is where you would restore the original methods if needed
};
}, []);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/data-collection/data-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const eventQueue: (QueuedEvent<Page | User | Track | Consent> | QueuedConsentEve
/**
* Flushes the event queue and sends all stored events to `window.edgee` if available.
*/
const flushQueue = () => {
if (typeof window !== 'undefined' && window.edgee) {
export const flushQueue = () => {
if (typeof window !== 'undefined' && window.edgee && eventQueue.length > 0) {
while (eventQueue.length > 0) {
const event = eventQueue.shift();
if (!event) return;
Expand Down
Loading