diff --git a/package-lock.json b/package-lock.json index a8272e48ff..3bf24359f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@rudderstack/analytics-js-monorepo", - "version": "3.31.0", + "version": "3.32.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@rudderstack/analytics-js-monorepo", - "version": "3.31.0", + "version": "3.32.0", "hasInstallScript": true, "license": "Elastic-2.0", "workspaces": [ @@ -24225,7 +24225,7 @@ }, "packages/analytics-js": { "name": "@rudderstack/analytics-js", - "version": "3.7.9", + "version": "3.7.10", "license": "Elastic-2.0", "dependencies": { "@preact/signals-core": "1.6.1", @@ -24239,7 +24239,7 @@ }, "packages/analytics-js-common": { "name": "@rudderstack/analytics-js-common", - "version": "3.9.0", + "version": "3.9.1", "license": "Elastic-2.0", "dependencies": { "@lukeed/uuid": "2.0.1", @@ -24256,7 +24256,7 @@ }, "packages/analytics-js-cookies": { "name": "@rudderstack/analytics-js-cookies", - "version": "0.3.6", + "version": "0.3.7", "license": "Elastic-2.0", "dependencies": { "@rudderstack/analytics-js-common": "*" @@ -24265,7 +24265,7 @@ }, "packages/analytics-js-integrations": { "name": "@rudderstack/analytics-js-integrations", - "version": "3.8.0", + "version": "3.8.1", "license": "Elastic-2.0", "dependencies": { "@lukeed/uuid": "2.0.1", @@ -24285,7 +24285,7 @@ }, "packages/analytics-js-plugins": { "name": "@rudderstack/analytics-js-plugins", - "version": "3.4.9", + "version": "3.4.10", "license": "Elastic-2.0", "dependencies": { "@rudderstack/analytics-js-common": "*", @@ -24299,7 +24299,7 @@ }, "packages/analytics-js-service-worker": { "name": "@rudderstack/analytics-js-service-worker", - "version": "3.1.9", + "version": "3.1.10", "license": "Elastic-2.0", "dependencies": { "@lukeed/uuid": "2.0.1", @@ -24329,7 +24329,7 @@ }, "packages/analytics-v1.1": { "name": "rudder-sdk-js", - "version": "2.48.16", + "version": "2.48.17", "license": "Elastic-2.0", "dependencies": { "@rudderstack/analytics-js-common": "*" @@ -24338,7 +24338,7 @@ }, "packages/loading-scripts": { "name": "@rudderstack/analytics-js-loading-scripts", - "version": "3.0.26", + "version": "3.0.27", "license": "Elastic-2.0", "dependencies": { "@rudderstack/analytics-js": "*" @@ -24347,7 +24347,7 @@ }, "packages/sanity-suite": { "name": "@rudderstack/analytics-js-sanity-suite", - "version": "3.1.16", + "version": "3.1.17", "license": "Elastic-2.0", "dependencies": { "@rudderstack/analytics-js": "*", diff --git a/package.json b/package.json index f3173c401d..cb21d1e756 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@rudderstack/analytics-js-monorepo", - "version": "3.31.0", + "version": "3.32.0", "private": true, "description": "Monorepo for RudderStack Analytics JS SDK", "workspaces": [ diff --git a/packages/analytics-js-common/CHANGELOG.md b/packages/analytics-js-common/CHANGELOG.md index eedc929372..d1c7422010 100644 --- a/packages/analytics-js-common/CHANGELOG.md +++ b/packages/analytics-js-common/CHANGELOG.md @@ -2,6 +2,13 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). +## [3.9.1](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-common@3.9.0...@rudderstack/analytics-js-common@3.9.1) (2024-08-28) + + +### Bug Fixes + +* handle blur and focus events to detect page leave ([#1837](https://github.com/rudderlabs/rudder-sdk-js/issues/1837)) ([57e735c](https://github.com/rudderlabs/rudder-sdk-js/commit/57e735ced4fb51ec895fbb196b1b879996cc10dd)) + ## [3.9.0](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-common@3.8.1...@rudderstack/analytics-js-common@3.9.0) (2024-08-16) diff --git a/packages/analytics-js-common/CHANGELOG_LATEST.md b/packages/analytics-js-common/CHANGELOG_LATEST.md index ad225609d8..ee3a4fa6f7 100644 --- a/packages/analytics-js-common/CHANGELOG_LATEST.md +++ b/packages/analytics-js-common/CHANGELOG_LATEST.md @@ -1,7 +1,7 @@ -## [3.9.0](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-common@3.8.1...@rudderstack/analytics-js-common@3.9.0) (2024-08-16) +## [3.9.1](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-common@3.9.0...@rudderstack/analytics-js-common@3.9.1) (2024-08-28) -### Features +### Bug Fixes -* onboarded XPixel Integration ([#1783](https://github.com/rudderlabs/rudder-sdk-js/issues/1783)) ([cf9b8cc](https://github.com/rudderlabs/rudder-sdk-js/commit/cf9b8cc41de341be781fae3108e4a07f2b553dda)) +* handle blur and focus events to detect page leave ([#1837](https://github.com/rudderlabs/rudder-sdk-js/issues/1837)) ([57e735c](https://github.com/rudderlabs/rudder-sdk-js/commit/57e735ced4fb51ec895fbb196b1b879996cc10dd)) diff --git a/packages/analytics-js-common/__tests__/utilities/page.test.ts b/packages/analytics-js-common/__tests__/utilities/page.test.ts index 5c21bc9847..1608ee8cbd 100644 --- a/packages/analytics-js-common/__tests__/utilities/page.test.ts +++ b/packages/analytics-js-common/__tests__/utilities/page.test.ts @@ -5,7 +5,7 @@ describe('onPageLeave', () => { document.dispatchEvent(new Event(event)); }; - const dispatchWindowEvent = (event: 'beforeunload') => { + const dispatchWindowEvent = (event: 'beforeunload' | 'blur' | 'focus') => { window.dispatchEvent(new Event(event)); }; @@ -26,6 +26,17 @@ describe('onPageLeave', () => { dispatchDocumentEvent('pagehide'); expect(evCallback).toHaveBeenCalledTimes(1); + expect(evCallback).toHaveBeenCalledWith(false); + }); + + it('should fire the callback on pagehide event based on visibility state', () => { + setVisibilityState('hidden'); + const evCallback = jest.fn(); + onPageLeave(evCallback); + + dispatchDocumentEvent('pagehide'); + expect(evCallback).toHaveBeenCalledTimes(1); + expect(evCallback).toHaveBeenCalledWith(true); }); it('should fire the callback on beforeunload event', () => { @@ -34,6 +45,7 @@ describe('onPageLeave', () => { dispatchWindowEvent('beforeunload'); expect(evCallback).toHaveBeenCalledTimes(1); + expect(evCallback).toHaveBeenCalledWith(false); }); it('should fire the callback on visibilitychange event', () => { @@ -43,6 +55,7 @@ describe('onPageLeave', () => { setVisibilityState('hidden'); dispatchDocumentEvent('visibilitychange'); expect(evCallback).toHaveBeenCalledTimes(1); + expect(evCallback).toHaveBeenCalledWith(true); }); it('should not fire the callback on visibilitychange event if visibilityState is visible', () => { @@ -54,6 +67,23 @@ describe('onPageLeave', () => { expect(evCallback).not.toHaveBeenCalled(); }); + it('should fire the callback on blur event', () => { + const evCallback = jest.fn(); + onPageLeave(evCallback); + + dispatchWindowEvent('blur'); + expect(evCallback).toHaveBeenCalledTimes(1); + expect(evCallback).toHaveBeenCalledWith(true); + }); + + it('should not fire the callback on focus event', () => { + const evCallback = jest.fn(); + onPageLeave(evCallback); + + dispatchWindowEvent('focus'); + expect(evCallback).not.toHaveBeenCalled(); + }); + it('should not fire the callback twice on pagehide event', () => { const evCallback = jest.fn(); onPageLeave(evCallback); @@ -61,6 +91,7 @@ describe('onPageLeave', () => { dispatchDocumentEvent('pagehide'); dispatchDocumentEvent('pagehide'); expect(evCallback).toHaveBeenCalledTimes(1); + expect(evCallback).toHaveBeenCalledWith(false); }); it('should not fire the callback twice on beforeunload event', () => { @@ -70,6 +101,7 @@ describe('onPageLeave', () => { dispatchWindowEvent('beforeunload'); dispatchWindowEvent('beforeunload'); expect(evCallback).toHaveBeenCalledTimes(1); + expect(evCallback).toHaveBeenCalledWith(false); }); it('should fire the callback only once if even multiple events are fired', () => { @@ -81,6 +113,7 @@ describe('onPageLeave', () => { setVisibilityState('hidden'); dispatchDocumentEvent('visibilitychange'); expect(evCallback).toHaveBeenCalledTimes(1); + expect(evCallback).toHaveBeenCalledWith(false); }); it('should fire the callback reliably when visibility changes multiple times', () => { @@ -96,6 +129,23 @@ describe('onPageLeave', () => { setVisibilityState('hidden'); dispatchDocumentEvent('visibilitychange'); expect(evCallback).toHaveBeenCalledTimes(2); + + expect(evCallback).toHaveBeenNthCalledWith(1, true); + expect(evCallback).toHaveBeenNthCalledWith(2, true); + }); + + it('should fire the callback reliably when blur and focus events are fired multiple times', () => { + const evCallback = jest.fn(); + onPageLeave(evCallback); + + dispatchWindowEvent('blur'); + dispatchWindowEvent('focus'); + dispatchWindowEvent('blur'); + dispatchWindowEvent('focus'); + expect(evCallback).toHaveBeenCalledTimes(2); + + expect(evCallback).toHaveBeenNthCalledWith(1, true); + expect(evCallback).toHaveBeenNthCalledWith(2, true); }); it('should fire the callback on pagehide event even after multiple visibility changes happen', () => { @@ -110,6 +160,9 @@ describe('onPageLeave', () => { dispatchDocumentEvent('pagehide'); expect(evCallback).toHaveBeenCalledTimes(2); + + expect(evCallback).toHaveBeenNthCalledWith(1, true); + expect(evCallback).toHaveBeenNthCalledWith(2, false); }); it('should fire the callback on beforeunload event even after multiple visibility changes happen', () => { @@ -124,5 +177,9 @@ describe('onPageLeave', () => { dispatchWindowEvent('beforeunload'); expect(evCallback).toHaveBeenCalledTimes(2); + + expect(evCallback).toHaveBeenNthCalledWith(1, true); + + expect(evCallback).toHaveBeenNthCalledWith(2, false); }); }); diff --git a/packages/analytics-js-common/package.json b/packages/analytics-js-common/package.json index 5ccae234a7..31137db8d6 100644 --- a/packages/analytics-js-common/package.json +++ b/packages/analytics-js-common/package.json @@ -1,6 +1,6 @@ { "name": "@rudderstack/analytics-js-common", - "version": "3.9.0", + "version": "3.9.1", "private": true, "description": "RudderStack JavaScript SDK common code", "module": "dist/npm/index.js", diff --git a/packages/analytics-js-common/project.json b/packages/analytics-js-common/project.json index c2f325aecb..4d86654ad0 100644 --- a/packages/analytics-js-common/project.json +++ b/packages/analytics-js-common/project.json @@ -51,9 +51,9 @@ "github": { "executor": "@jscutlery/semver:github", "options": { - "tag": "@rudderstack/analytics-js-common@3.9.0", - "title": "@rudderstack/analytics-js-common@3.9.0", - "discussion-category": "@rudderstack/analytics-js-common@3.9.0", + "tag": "@rudderstack/analytics-js-common@3.9.1", + "title": "@rudderstack/analytics-js-common@3.9.1", + "discussion-category": "@rudderstack/analytics-js-common@3.9.1", "notesFile": "./packages/analytics-js-common/CHANGELOG_LATEST.md" } } diff --git a/packages/analytics-js-common/src/utilities/page.ts b/packages/analytics-js-common/src/utilities/page.ts index 7bda95a005..c31902def0 100644 --- a/packages/analytics-js-common/src/utilities/page.ts +++ b/packages/analytics-js-common/src/utilities/page.ts @@ -1,7 +1,8 @@ -const onPageLeave = (callback: (...args: unknown[]) => void) => { +const onPageLeave = (callback: (isAccessible: boolean) => void) => { // To ensure the callback is only called once even if more than one events // are fired at once. let pageLeft = false; + let isAccessible = false; function handleOnLeave() { if (pageLeft) { @@ -10,7 +11,7 @@ const onPageLeave = (callback: (...args: unknown[]) => void) => { pageLeft = true; - callback(); + callback(isAccessible); } // Catches the unloading of the page (e.g., closing the tab or navigating away). @@ -18,13 +19,29 @@ const onPageLeave = (callback: (...args: unknown[]) => void) => { // refreshing the page, or closing the browser tab // Note that 'pagehide' is not supported in IE. // So, this is a fallback. - (globalThis as typeof window).addEventListener('beforeunload', handleOnLeave); + (globalThis as typeof window).addEventListener('beforeunload', () => { + isAccessible = false; + handleOnLeave(); + }); + + (globalThis as typeof window).addEventListener('blur', () => { + isAccessible = true; + handleOnLeave(); + }); + + (globalThis as typeof window).addEventListener('focus', () => { + pageLeft = false; + }); // Catches the page being hidden, including scenarios like closing the tab. - document.addEventListener('pagehide', handleOnLeave); + document.addEventListener('pagehide', () => { + isAccessible = document.visibilityState === 'hidden'; + handleOnLeave(); + }); // Catches visibility changes, such as switching tabs or minimizing the browser. document.addEventListener('visibilitychange', () => { + isAccessible = true; if (document.visibilityState === 'hidden') { handleOnLeave(); } else { diff --git a/packages/analytics-js-cookies/CHANGELOG.md b/packages/analytics-js-cookies/CHANGELOG.md index 0840cf5d27..5706ffbf24 100644 --- a/packages/analytics-js-cookies/CHANGELOG.md +++ b/packages/analytics-js-cookies/CHANGELOG.md @@ -2,6 +2,11 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). +## [0.3.7](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-cookies@0.3.6...@rudderstack/analytics-js-cookies@0.3.7) (2024-08-28) + +### Dependency Updates + +* `@rudderstack/analytics-js-common` updated to version `3.9.1` ## [0.3.6](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-cookies@0.3.5...@rudderstack/analytics-js-cookies@0.3.6) (2024-08-16) ### Dependency Updates diff --git a/packages/analytics-js-cookies/CHANGELOG_LATEST.md b/packages/analytics-js-cookies/CHANGELOG_LATEST.md index 574b5cb24c..950b964065 100644 --- a/packages/analytics-js-cookies/CHANGELOG_LATEST.md +++ b/packages/analytics-js-cookies/CHANGELOG_LATEST.md @@ -1,5 +1,5 @@ -## [0.3.6](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-cookies@0.3.5...@rudderstack/analytics-js-cookies@0.3.6) (2024-08-16) +## [0.3.7](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-cookies@0.3.6...@rudderstack/analytics-js-cookies@0.3.7) (2024-08-28) ### Dependency Updates -* `@rudderstack/analytics-js-common` updated to version `3.9.0` +* `@rudderstack/analytics-js-common` updated to version `3.9.1` diff --git a/packages/analytics-js-cookies/package.json b/packages/analytics-js-cookies/package.json index 46eb374dd5..31f2bec8aa 100644 --- a/packages/analytics-js-cookies/package.json +++ b/packages/analytics-js-cookies/package.json @@ -1,6 +1,6 @@ { "name": "@rudderstack/analytics-js-cookies", - "version": "0.3.6", + "version": "0.3.7", "description": "RudderStack JavaScript SDK Cookies Utilities", "main": "dist/npm/modern/cjs/index.cjs", "module": "dist/npm/modern/esm/index.mjs", diff --git a/packages/analytics-js-cookies/project.json b/packages/analytics-js-cookies/project.json index 2540a2f118..2eff3bb1e3 100644 --- a/packages/analytics-js-cookies/project.json +++ b/packages/analytics-js-cookies/project.json @@ -51,9 +51,9 @@ "github": { "executor": "@jscutlery/semver:github", "options": { - "tag": "@rudderstack/analytics-js-cookies@0.3.6", - "title": "@rudderstack/analytics-js-cookies@0.3.6", - "discussion-category": "@rudderstack/analytics-js-cookies@0.3.6", + "tag": "@rudderstack/analytics-js-cookies@0.3.7", + "title": "@rudderstack/analytics-js-cookies@0.3.7", + "discussion-category": "@rudderstack/analytics-js-cookies@0.3.7", "notesFile": "./packages/analytics-js-cookies/CHANGELOG_LATEST.md" } } diff --git a/packages/analytics-js-integrations/CHANGELOG.md b/packages/analytics-js-integrations/CHANGELOG.md index 078408a093..c11fb76f86 100644 --- a/packages/analytics-js-integrations/CHANGELOG.md +++ b/packages/analytics-js-integrations/CHANGELOG.md @@ -2,6 +2,11 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). +## [3.8.1](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-integrations@3.8.0...@rudderstack/analytics-js-integrations@3.8.1) (2024-08-28) + +### Dependency Updates + +* `@rudderstack/analytics-js-common` updated to version `3.9.1` ## [3.8.0](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-integrations@3.7.0...@rudderstack/analytics-js-integrations@3.8.0) (2024-08-16) ### Dependency Updates diff --git a/packages/analytics-js-integrations/CHANGELOG_LATEST.md b/packages/analytics-js-integrations/CHANGELOG_LATEST.md index b3d295f33c..643c94a9de 100644 --- a/packages/analytics-js-integrations/CHANGELOG_LATEST.md +++ b/packages/analytics-js-integrations/CHANGELOG_LATEST.md @@ -1,10 +1,5 @@ -## [3.8.0](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-integrations@3.7.0...@rudderstack/analytics-js-integrations@3.8.0) (2024-08-16) +## [3.8.1](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-integrations@3.8.0...@rudderstack/analytics-js-integrations@3.8.1) (2024-08-28) ### Dependency Updates -* `@rudderstack/analytics-js-common` updated to version `3.9.0` - -### Features - -* onboarded XPixel Integration ([#1783](https://github.com/rudderlabs/rudder-sdk-js/issues/1783)) ([cf9b8cc](https://github.com/rudderlabs/rudder-sdk-js/commit/cf9b8cc41de341be781fae3108e4a07f2b553dda)) - +* `@rudderstack/analytics-js-common` updated to version `3.9.1` diff --git a/packages/analytics-js-integrations/package.json b/packages/analytics-js-integrations/package.json index ea4494253b..6dca10a943 100644 --- a/packages/analytics-js-integrations/package.json +++ b/packages/analytics-js-integrations/package.json @@ -1,6 +1,6 @@ { "name": "@rudderstack/analytics-js-integrations", - "version": "3.8.0", + "version": "3.8.1", "private": true, "description": "RudderStack JavaScript SDK device mode integrations", "main": "dist/npm/modern/cjs/index.js", diff --git a/packages/analytics-js-integrations/project.json b/packages/analytics-js-integrations/project.json index 02bf4d5568..88769c8d48 100644 --- a/packages/analytics-js-integrations/project.json +++ b/packages/analytics-js-integrations/project.json @@ -51,9 +51,9 @@ "github": { "executor": "@jscutlery/semver:github", "options": { - "tag": "@rudderstack/analytics-js-integrations@3.8.0", - "title": "@rudderstack/analytics-js-integrations@3.8.0", - "discussion-category": "@rudderstack/analytics-js-integrations@3.8.0", + "tag": "@rudderstack/analytics-js-integrations@3.8.1", + "title": "@rudderstack/analytics-js-integrations@3.8.1", + "discussion-category": "@rudderstack/analytics-js-integrations@3.8.1", "notesFile": "./packages/analytics-js-integrations/CHANGELOG_LATEST.md" } } diff --git a/packages/analytics-js-plugins/CHANGELOG.md b/packages/analytics-js-plugins/CHANGELOG.md index 8ff134d6be..ba645c4c7d 100644 --- a/packages/analytics-js-plugins/CHANGELOG.md +++ b/packages/analytics-js-plugins/CHANGELOG.md @@ -2,6 +2,18 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). +## [3.4.10](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-plugins@3.4.9...@rudderstack/analytics-js-plugins@3.4.10) (2024-08-28) + +### Dependency Updates + +* `@rudderstack/analytics-js-common` updated to version `3.9.1` +* `@rudderstack/analytics-js` updated to version `3.7.9` +* `@rudderstack/analytics-js-cookies` updated to version `0.3.7` + +### Bug Fixes + +* handle blur and focus events to detect page leave ([#1837](https://github.com/rudderlabs/rudder-sdk-js/issues/1837)) ([57e735c](https://github.com/rudderlabs/rudder-sdk-js/commit/57e735ced4fb51ec895fbb196b1b879996cc10dd)) + ## [3.4.9](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-plugins@3.4.8...@rudderstack/analytics-js-plugins@3.4.9) (2024-08-16) ### Dependency Updates diff --git a/packages/analytics-js-plugins/CHANGELOG_LATEST.md b/packages/analytics-js-plugins/CHANGELOG_LATEST.md index 32007f2af0..ed3bc94ff9 100644 --- a/packages/analytics-js-plugins/CHANGELOG_LATEST.md +++ b/packages/analytics-js-plugins/CHANGELOG_LATEST.md @@ -1,7 +1,12 @@ -## [3.4.9](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-plugins@3.4.8...@rudderstack/analytics-js-plugins@3.4.9) (2024-08-16) +## [3.4.10](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-plugins@3.4.9...@rudderstack/analytics-js-plugins@3.4.10) (2024-08-28) ### Dependency Updates -* `@rudderstack/analytics-js-common` updated to version `3.9.0` -* `@rudderstack/analytics-js` updated to version `3.7.8` -* `@rudderstack/analytics-js-cookies` updated to version `0.3.6` +* `@rudderstack/analytics-js-common` updated to version `3.9.1` +* `@rudderstack/analytics-js` updated to version `3.7.9` +* `@rudderstack/analytics-js-cookies` updated to version `0.3.7` + +### Bug Fixes + +* handle blur and focus events to detect page leave ([#1837](https://github.com/rudderlabs/rudder-sdk-js/issues/1837)) ([57e735c](https://github.com/rudderlabs/rudder-sdk-js/commit/57e735ced4fb51ec895fbb196b1b879996cc10dd)) + diff --git a/packages/analytics-js-plugins/__tests__/utilities/retryQueue/RetryQueue.test.ts b/packages/analytics-js-plugins/__tests__/utilities/retryQueue/RetryQueue.test.ts index be642aac18..999950b53b 100644 --- a/packages/analytics-js-plugins/__tests__/utilities/retryQueue/RetryQueue.test.ts +++ b/packages/analytics-js-plugins/__tests__/utilities/retryQueue/RetryQueue.test.ts @@ -142,6 +142,62 @@ describe('Queue', () => { ]); }); + it('should flush queued batch events', () => { + const batchQueue = new RetryQueue( + 'test', + { batch: { enabled: true, maxSize: 2 } }, + jest.fn(), + defaultStoreManager, + undefined, + undefined, + (items: []) => items.length, + ); + + batchQueue.addItem('a'); + + batchQueue.flushBatch(); + + expect(batchQueue.getStorageEntry('batchQueue')).toEqual([]); + + expect(batchQueue.getStorageEntry('queue')).toEqual([ + { + item: ['a'], + attemptNumber: 0, + time: expect.any(Number), + id: expect.any(String), + }, + ]); + }); + + it('should not flush queued batch events if another flush is in progress', () => { + const batchQueue = new RetryQueue( + 'test', + { batch: { enabled: true, maxSize: 2 } }, + jest.fn(), + defaultStoreManager, + undefined, + undefined, + (items: []) => items.length, + ); + + batchQueue.batchingInProgress = true; + + batchQueue.addItem('a'); + + batchQueue.flushBatch(); + + expect(batchQueue.getStorageEntry('batchQueue')).toEqual([ + { + item: 'a', + attemptNumber: 0, + time: expect.any(Number), + id: expect.any(String), + }, + ]); + + expect(batchQueue.getStorageEntry('queue')).toEqual([]); + }); + it('should run a task', () => { queue.start(); diff --git a/packages/analytics-js-plugins/package.json b/packages/analytics-js-plugins/package.json index 25245d81f3..d764128bc9 100644 --- a/packages/analytics-js-plugins/package.json +++ b/packages/analytics-js-plugins/package.json @@ -1,6 +1,6 @@ { "name": "@rudderstack/analytics-js-plugins", - "version": "3.4.9", + "version": "3.4.10", "private": true, "description": "RudderStack JavaScript SDK plugins", "main": "dist/npm/modern/cjs/index.cjs", diff --git a/packages/analytics-js-plugins/project.json b/packages/analytics-js-plugins/project.json index 42506dc7df..87f6238d6f 100644 --- a/packages/analytics-js-plugins/project.json +++ b/packages/analytics-js-plugins/project.json @@ -51,9 +51,9 @@ "github": { "executor": "@jscutlery/semver:github", "options": { - "tag": "@rudderstack/analytics-js-plugins@3.4.9", - "title": "@rudderstack/analytics-js-plugins@3.4.9", - "discussion-category": "@rudderstack/analytics-js-plugins@3.4.9", + "tag": "@rudderstack/analytics-js-plugins@3.4.10", + "title": "@rudderstack/analytics-js-plugins@3.4.10", + "discussion-category": "@rudderstack/analytics-js-plugins@3.4.10", "notesFile": "./packages/analytics-js-plugins/CHANGELOG_LATEST.md" } } diff --git a/packages/analytics-js-plugins/src/utilities/retryQueue/RetryQueue.ts b/packages/analytics-js-plugins/src/utilities/retryQueue/RetryQueue.ts index f49598efd6..721301960e 100644 --- a/packages/analytics-js-plugins/src/utilities/retryQueue/RetryQueue.ts +++ b/packages/analytics-js-plugins/src/utilities/retryQueue/RetryQueue.ts @@ -73,6 +73,7 @@ class RetryQueue implements IQueue { batchSizeCalcCb?: QueueBatchItemsSizeCalculatorCallback; reclaimStartVal?: Nullable; reclaimEndVal?: Nullable; + isPageAccessible: boolean; constructor( name: string, @@ -139,6 +140,8 @@ class RetryQueue implements IQueue { this.processHead = this.processHead.bind(this); this.flushBatch = this.flushBatch.bind(this); + this.isPageAccessible = true; + // Flush the queue on page leave this.flushBatchOnPageLeave(); @@ -239,8 +242,9 @@ class RetryQueue implements IQueue { /** * Flushes the batch queue */ - flushBatch() { + flushBatch(isAccessible = true) { if (!this.batchingInProgress) { + this.isPageAccessible = isAccessible; this.batchingInProgress = true; let batchQueue = (this.getStorageEntry(QueueStatuses.BATCH_QUEUE) as Nullable) ?? []; @@ -458,8 +462,6 @@ class RetryQueue implements IQueue { // Pop the head off the queue let queue = (this.getStorageEntry(QueueStatuses.QUEUE) as Nullable[]>) ?? []; - const inProgress = - (this.getStorageEntry(QueueStatuses.IN_PROGRESS) as Nullable>) ?? {}; const now = this.schedule.now(); const toRun: InProgressQueueItem[] = []; @@ -484,6 +486,13 @@ class RetryQueue implements IQueue { }); }; + let inProgress = + (this.getStorageEntry(QueueStatuses.IN_PROGRESS) as Nullable>) ?? {}; + // If the page is unloading, clear the previous in progress queue also to avoid any stale data + // Otherwise, the next page load will retry the items which were in progress previously + if (!this.isPageAccessible) { + inProgress = {}; + } let inProgressSize = Object.keys(inProgress).length; // eslint-disable-next-line no-plusplus @@ -496,12 +505,15 @@ class RetryQueue implements IQueue { if (el) { const id = generateUUID(); - // Save this to the in progress map - inProgress[id] = { - item: el.item, - attemptNumber: el.attemptNumber, - time: this.schedule.now(), - }; + // If the page is unloading, don't add items to the in progress queue + if (this.isPageAccessible) { + // Save this to the in progress map + inProgress[id] = { + item: el.item, + attemptNumber: el.attemptNumber, + time: this.schedule.now(), + }; + } enqueueItem(el, id); } diff --git a/packages/analytics-js-service-worker/.size-limit.mjs b/packages/analytics-js-service-worker/.size-limit.mjs index cecaa2b7cb..3396eb12c5 100644 --- a/packages/analytics-js-service-worker/.size-limit.mjs +++ b/packages/analytics-js-service-worker/.size-limit.mjs @@ -7,36 +7,36 @@ export default [ name: 'Service Worker - Legacy - NPM (ESM)', path: 'dist/npm/legacy/esm/index.mjs', import: '*', - limit: '30 KiB', + limit: '31 KiB', }, { name: 'Service Worker - Legacy - NPM (CJS)', path: 'dist/npm/legacy/cjs/index.cjs', import: '*', - limit: '30 KiB', + limit: '31 KiB', }, { name: 'Service Worker - Legacy - NPM (UMD)', path: 'dist/npm/legacy/umd/index.js', import: '*', - limit: '30 KiB', + limit: '31 KiB', }, { name: 'Service Worker - Modern - NPM (ESM)', path: 'dist/npm/modern/esm/index.mjs', import: '*', - limit: '25 KiB', + limit: '26 KiB', }, { name: 'Service Worker - Modern - NPM (CJS)', path: 'dist/npm/modern/cjs/index.cjs', import: '*', - limit: '25 KiB', + limit: '26 KiB', }, { name: 'Service Worker - Modern - NPM (UMD)', path: 'dist/npm/modern/umd/index.js', import: '*', - limit: '25 KiB', + limit: '26 KiB', }, ]; diff --git a/packages/analytics-js-service-worker/CHANGELOG.md b/packages/analytics-js-service-worker/CHANGELOG.md index b30e67b80c..77fe41a599 100644 --- a/packages/analytics-js-service-worker/CHANGELOG.md +++ b/packages/analytics-js-service-worker/CHANGELOG.md @@ -2,6 +2,16 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). +## [3.1.10](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-service-worker@3.1.9...@rudderstack/analytics-js-service-worker@3.1.10) (2024-08-28) + +### Dependency Updates + +* `@rudderstack/analytics-js-common` updated to version `3.9.1` + +### Bug Fixes + +* handle blur and focus events to detect page leave ([#1837](https://github.com/rudderlabs/rudder-sdk-js/issues/1837)) ([57e735c](https://github.com/rudderlabs/rudder-sdk-js/commit/57e735ced4fb51ec895fbb196b1b879996cc10dd)) + ## [3.1.9](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-service-worker@3.1.8...@rudderstack/analytics-js-service-worker@3.1.9) (2024-08-16) ### Dependency Updates diff --git a/packages/analytics-js-service-worker/CHANGELOG_LATEST.md b/packages/analytics-js-service-worker/CHANGELOG_LATEST.md index a082206621..6a425db674 100644 --- a/packages/analytics-js-service-worker/CHANGELOG_LATEST.md +++ b/packages/analytics-js-service-worker/CHANGELOG_LATEST.md @@ -1,5 +1,10 @@ -## [3.1.9](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-service-worker@3.1.8...@rudderstack/analytics-js-service-worker@3.1.9) (2024-08-16) +## [3.1.10](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-service-worker@3.1.9...@rudderstack/analytics-js-service-worker@3.1.10) (2024-08-28) ### Dependency Updates -* `@rudderstack/analytics-js-common` updated to version `3.9.0` +* `@rudderstack/analytics-js-common` updated to version `3.9.1` + +### Bug Fixes + +* handle blur and focus events to detect page leave ([#1837](https://github.com/rudderlabs/rudder-sdk-js/issues/1837)) ([57e735c](https://github.com/rudderlabs/rudder-sdk-js/commit/57e735ced4fb51ec895fbb196b1b879996cc10dd)) + diff --git a/packages/analytics-js-service-worker/package.json b/packages/analytics-js-service-worker/package.json index 2f789a38a4..8a016b1cfe 100644 --- a/packages/analytics-js-service-worker/package.json +++ b/packages/analytics-js-service-worker/package.json @@ -1,6 +1,6 @@ { "name": "@rudderstack/analytics-js-service-worker", - "version": "3.1.9", + "version": "3.1.10", "description": "RudderStack JavaScript Service Worker SDK", "main": "dist/npm/modern/cjs/index.cjs", "module": "dist/npm/modern/esm/index.mjs", diff --git a/packages/analytics-js-service-worker/project.json b/packages/analytics-js-service-worker/project.json index 2c575e7e1d..a908de9953 100644 --- a/packages/analytics-js-service-worker/project.json +++ b/packages/analytics-js-service-worker/project.json @@ -51,9 +51,9 @@ "github": { "executor": "@jscutlery/semver:github", "options": { - "tag": "@rudderstack/analytics-js-service-worker@3.1.9", - "title": "rudderstack/analytics-js-service-worker@3.1.9", - "discussion-category": "rudderstack/analytics-js-service-worker@3.1.9", + "tag": "@rudderstack/analytics-js-service-worker@3.1.10", + "title": "rudderstack/analytics-js-service-worker@3.1.10", + "discussion-category": "rudderstack/analytics-js-service-worker@3.1.10", "notesFile": "./packages/analytics-js-service-worker/CHANGELOG_LATEST.md" } } diff --git a/packages/analytics-js/CHANGELOG.md b/packages/analytics-js/CHANGELOG.md index 76485a8470..b2e2a9c95e 100644 --- a/packages/analytics-js/CHANGELOG.md +++ b/packages/analytics-js/CHANGELOG.md @@ -2,6 +2,18 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). +## [3.7.10](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js@3.7.9...@rudderstack/analytics-js@3.7.10) (2024-08-28) + +### Dependency Updates + +* `@rudderstack/analytics-js-cookies` updated to version `0.3.7` +* `@rudderstack/analytics-js-common` updated to version `3.9.1` +* `@rudderstack/analytics-js-plugins` updated to version `3.4.10` + +### Bug Fixes + +* handle blur and focus events to detect page leave ([#1837](https://github.com/rudderlabs/rudder-sdk-js/issues/1837)) ([57e735c](https://github.com/rudderlabs/rudder-sdk-js/commit/57e735ced4fb51ec895fbb196b1b879996cc10dd)) + ## [3.7.9](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js@3.7.8...@rudderstack/analytics-js@3.7.9) (2024-08-16) ### Dependency Updates diff --git a/packages/analytics-js/CHANGELOG_LATEST.md b/packages/analytics-js/CHANGELOG_LATEST.md index 2c5048db40..31ff6606f1 100644 --- a/packages/analytics-js/CHANGELOG_LATEST.md +++ b/packages/analytics-js/CHANGELOG_LATEST.md @@ -1,7 +1,12 @@ -## [3.7.9](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js@3.7.8...@rudderstack/analytics-js@3.7.9) (2024-08-16) +## [3.7.10](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js@3.7.9...@rudderstack/analytics-js@3.7.10) (2024-08-28) ### Dependency Updates -* `@rudderstack/analytics-js-cookies` updated to version `0.3.6` -* `@rudderstack/analytics-js-common` updated to version `3.9.0` -* `@rudderstack/analytics-js-plugins` updated to version `3.4.9` +* `@rudderstack/analytics-js-cookies` updated to version `0.3.7` +* `@rudderstack/analytics-js-common` updated to version `3.9.1` +* `@rudderstack/analytics-js-plugins` updated to version `3.4.10` + +### Bug Fixes + +* handle blur and focus events to detect page leave ([#1837](https://github.com/rudderlabs/rudder-sdk-js/issues/1837)) ([57e735c](https://github.com/rudderlabs/rudder-sdk-js/commit/57e735ced4fb51ec895fbb196b1b879996cc10dd)) + diff --git a/packages/analytics-js/package.json b/packages/analytics-js/package.json index bc91edce8f..ffa2ebbb6d 100644 --- a/packages/analytics-js/package.json +++ b/packages/analytics-js/package.json @@ -1,6 +1,6 @@ { "name": "@rudderstack/analytics-js", - "version": "3.7.9", + "version": "3.7.10", "description": "RudderStack JavaScript SDK", "main": "dist/npm/modern/cjs/index.cjs", "module": "dist/npm/modern/esm/index.mjs", diff --git a/packages/analytics-js/project.json b/packages/analytics-js/project.json index e089f08a05..0a74487d64 100644 --- a/packages/analytics-js/project.json +++ b/packages/analytics-js/project.json @@ -59,9 +59,9 @@ "github": { "executor": "@jscutlery/semver:github", "options": { - "tag": "@rudderstack/analytics-js@3.7.9", - "title": "@rudderstack/analytics-js@3.7.9", - "discussion-category": "@rudderstack/analytics-js@3.7.9", + "tag": "@rudderstack/analytics-js@3.7.10", + "title": "@rudderstack/analytics-js@3.7.10", + "discussion-category": "@rudderstack/analytics-js@3.7.10", "notesFile": "./packages/analytics-js/CHANGELOG_LATEST.md" } } diff --git a/packages/analytics-js/public/index.html b/packages/analytics-js/public/index.html index 5fc96a57cf..1507748f30 100644 --- a/packages/analytics-js/public/index.html +++ b/packages/analytics-js/public/index.html @@ -272,6 +272,8 @@

Test HTML file

+ +

@@ -359,6 +361,24 @@

Test HTML file

} ); } + + function reloadPageWithEvents() { + rudderanalytics.track('Event 1 before reload', function (rudderElement) { + console.log('Event 1 before reload'); + + document.getElementById('action').innerHTML = 'Event 1 before reload'; + document.getElementById('rudderElement').innerHTML = JSON.stringify(rudderElement); + }); + + rudderanalytics.track('Event 2 before reload', function (rudderElement) { + console.log('Event 2 before reload'); + + document.getElementById('action').innerHTML = 'Event 2 before reload'; + document.getElementById('rudderElement').innerHTML = JSON.stringify(rudderElement); + }); + + window.location.reload(); + } diff --git a/packages/analytics-v1.1/CHANGELOG.md b/packages/analytics-v1.1/CHANGELOG.md index ce6b15d69b..ae41e01e61 100644 --- a/packages/analytics-v1.1/CHANGELOG.md +++ b/packages/analytics-v1.1/CHANGELOG.md @@ -2,6 +2,11 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). +## [2.48.17](https://github.com/rudderlabs/rudder-sdk-js/compare/rudder-sdk-js@2.48.16...rudder-sdk-js@2.48.17) (2024-08-28) + +### Dependency Updates + +* `@rudderstack/analytics-js-common` updated to version `3.9.1` ## [2.48.16](https://github.com/rudderlabs/rudder-sdk-js/compare/rudder-sdk-js@2.48.15...rudder-sdk-js@2.48.16) (2024-08-16) ### Dependency Updates diff --git a/packages/analytics-v1.1/CHANGELOG_LATEST.md b/packages/analytics-v1.1/CHANGELOG_LATEST.md index 74849ee2cc..0a6d9c3549 100644 --- a/packages/analytics-v1.1/CHANGELOG_LATEST.md +++ b/packages/analytics-v1.1/CHANGELOG_LATEST.md @@ -1,5 +1,5 @@ -## [2.48.16](https://github.com/rudderlabs/rudder-sdk-js/compare/rudder-sdk-js@2.48.15...rudder-sdk-js@2.48.16) (2024-08-16) +## [2.48.17](https://github.com/rudderlabs/rudder-sdk-js/compare/rudder-sdk-js@2.48.16...rudder-sdk-js@2.48.17) (2024-08-28) ### Dependency Updates -* `@rudderstack/analytics-js-common` updated to version `3.9.0` +* `@rudderstack/analytics-js-common` updated to version `3.9.1` diff --git a/packages/analytics-v1.1/package.json b/packages/analytics-v1.1/package.json index adb14446b1..b58b9d8efc 100644 --- a/packages/analytics-v1.1/package.json +++ b/packages/analytics-v1.1/package.json @@ -1,6 +1,6 @@ { "name": "rudder-sdk-js", - "version": "2.48.16", + "version": "2.48.17", "description": "RudderStack JavaScript SDK", "main": "dist/npm/index.js", "module": "dist/npm/index.es.js", diff --git a/packages/analytics-v1.1/project.json b/packages/analytics-v1.1/project.json index 4d0626ee32..0792b47a09 100644 --- a/packages/analytics-v1.1/project.json +++ b/packages/analytics-v1.1/project.json @@ -59,9 +59,9 @@ "github": { "executor": "@jscutlery/semver:github", "options": { - "tag": "rudder-sdk-js@2.48.16", - "title": "rudder-sdk-js@2.48.16", - "discussion-category": "rudder-sdk-js@2.48.16", + "tag": "rudder-sdk-js@2.48.17", + "title": "rudder-sdk-js@2.48.17", + "discussion-category": "rudder-sdk-js@2.48.17", "notesFile": "./packages/analytics-v1.1/CHANGELOG_LATEST.md" } } diff --git a/packages/loading-scripts/CHANGELOG.md b/packages/loading-scripts/CHANGELOG.md index 3bbb6251ee..f1ee1f40d6 100644 --- a/packages/loading-scripts/CHANGELOG.md +++ b/packages/loading-scripts/CHANGELOG.md @@ -2,6 +2,11 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). +## [3.0.27](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-loading-scripts@3.0.26...@rudderstack/analytics-js-loading-scripts@3.0.27) (2024-08-28) + +### Dependency Updates + +* `@rudderstack/analytics-js` updated to version `3.7.10` ## [3.0.26](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-loading-scripts@3.0.25...@rudderstack/analytics-js-loading-scripts@3.0.26) (2024-08-16) ### Dependency Updates diff --git a/packages/loading-scripts/CHANGELOG_LATEST.md b/packages/loading-scripts/CHANGELOG_LATEST.md index 0c58ad7f31..b0160d256f 100644 --- a/packages/loading-scripts/CHANGELOG_LATEST.md +++ b/packages/loading-scripts/CHANGELOG_LATEST.md @@ -1,5 +1,5 @@ -## [3.0.26](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-loading-scripts@3.0.25...@rudderstack/analytics-js-loading-scripts@3.0.26) (2024-08-16) +## [3.0.27](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-loading-scripts@3.0.26...@rudderstack/analytics-js-loading-scripts@3.0.27) (2024-08-28) ### Dependency Updates -* `@rudderstack/analytics-js` updated to version `3.7.9` +* `@rudderstack/analytics-js` updated to version `3.7.10` diff --git a/packages/loading-scripts/package.json b/packages/loading-scripts/package.json index b5f724af4a..ef8f9ce87f 100644 --- a/packages/loading-scripts/package.json +++ b/packages/loading-scripts/package.json @@ -1,6 +1,6 @@ { "name": "@rudderstack/analytics-js-loading-scripts", - "version": "3.0.26", + "version": "3.0.27", "private": true, "description": "Loading script for RudderStack JavaScript SDK", "main": "./src/index.js", diff --git a/packages/loading-scripts/project.json b/packages/loading-scripts/project.json index 65cb068f91..a96c58dc9f 100644 --- a/packages/loading-scripts/project.json +++ b/packages/loading-scripts/project.json @@ -51,9 +51,9 @@ "github": { "executor": "@jscutlery/semver:github", "options": { - "tag": "@rudderstack/analytics-js-loading-scripts@3.0.26", - "title": "@rudderstack/analytics-js-loading-scripts@3.0.26", - "discussion-category": "@rudderstack/analytics-js-loading-scripts@3.0.26", + "tag": "@rudderstack/analytics-js-loading-scripts@3.0.27", + "title": "@rudderstack/analytics-js-loading-scripts@3.0.27", + "discussion-category": "@rudderstack/analytics-js-loading-scripts@3.0.27", "notesFile": "./packages/loading-scripts/CHANGELOG_LATEST.md" } } diff --git a/packages/sanity-suite/CHANGELOG.md b/packages/sanity-suite/CHANGELOG.md index 3af2b07a32..30a2ff06da 100644 --- a/packages/sanity-suite/CHANGELOG.md +++ b/packages/sanity-suite/CHANGELOG.md @@ -2,6 +2,11 @@ This file was generated using [@jscutlery/semver](https://github.com/jscutlery/semver). +## [3.1.17](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-sanity-suite@3.1.16...@rudderstack/analytics-js-sanity-suite@3.1.17) (2024-08-28) + +### Dependency Updates + +* `@rudderstack/analytics-js` updated to version `3.7.10` ## [3.1.16](https://github.com/rudderlabs/rudder-sdk-js/compare/@rudderstack/analytics-js-sanity-suite@3.1.15...@rudderstack/analytics-js-sanity-suite@3.1.16) (2024-08-16) ### Dependency Updates diff --git a/packages/sanity-suite/package.json b/packages/sanity-suite/package.json index 0d6a3b49df..a1fe3c8021 100644 --- a/packages/sanity-suite/package.json +++ b/packages/sanity-suite/package.json @@ -1,6 +1,6 @@ { "name": "@rudderstack/analytics-js-sanity-suite", - "version": "3.1.16", + "version": "3.1.17", "private": true, "description": "Sanity suite for testing JS SDK package", "main": "./dist/v3/cdn/testBook.js", diff --git a/sonar-project.properties b/sonar-project.properties index a229c06ac8..07f9165b73 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -6,7 +6,7 @@ sonar.qualitygate.wait=false sonar.projectKey=rudderlabs_rudder-sdk-js sonar.organization=rudderlabs sonar.projectName=rudder-sdk-js -sonar.projectVersion=3.31.0 +sonar.projectVersion=3.32.0 # Meta-data for the project sonar.links.scm=https://github.com/rudderlabs/rudder-sdk-js