Skip to content

Commit

Permalink
Merge branch 'develop' into feat/gainsight-px
Browse files Browse the repository at this point in the history
  • Loading branch information
shrouti1507 authored Oct 24, 2024
2 parents 5ff98ce + 5590af7 commit c6c8e3f
Show file tree
Hide file tree
Showing 16 changed files with 406 additions and 48 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ npm install @rudderstack/analytics-js --save
```javascript
import { RudderAnalytics } from '@rudderstack/analytics-js';

const rudderanalytics = new RudderAnalytics();
const rudderAnalytics = new RudderAnalytics();
rudderAnalytics.load(<WRITE_KEY>, <DATA_PLANE_URL>, {});

export { rudderanalytics };
export { rudderAnalytics };
```

- **For CJS using the `require` method**:
Expand All @@ -92,7 +92,7 @@ var RudderAnalytics = require('@rudderstack/analytics-js');
const rudderAnalytics = new RudderAnalytics();
rudderAnalytics.load(<WRITE_KEY>, <DATA_PLANE_URL>, {});

exports.rudderanalytics = rudderAnalytics;
exports.rudderAnalytics = rudderAnalytics;
```

### Sample implementations
Expand Down
19 changes: 19 additions & 0 deletions packages/analytics-js-common/__tests__/utilities/page.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('onPageLeave', () => {
};

beforeEach(() => {
jest.useFakeTimers();
setVisibilityState('visible');
});

Expand Down Expand Up @@ -182,4 +183,22 @@ describe('onPageLeave', () => {

expect(evCallback).toHaveBeenNthCalledWith(2, false);
});

it('should fire the callback on beforeunload event on the next tick even when the tab is inactive', () => {
const evCallback = jest.fn();
onPageLeave(evCallback);

setVisibilityState('hidden');
dispatchDocumentEvent('visibilitychange');

expect(evCallback).toHaveBeenCalledTimes(1);
expect(evCallback).toHaveBeenNthCalledWith(1, true);

jest.runAllTimers();

dispatchWindowEvent('beforeunload');

expect(evCallback).toHaveBeenCalledTimes(2);
expect(evCallback).toHaveBeenNthCalledWith(2, false);
});
});
12 changes: 12 additions & 0 deletions packages/analytics-js-common/src/types/ApplicationState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ export type LifecycleState = {
dataPlaneUrl: Signal<string | undefined>;
};

export type AutoTrackState = {
enabled: Signal<boolean>;
pageLifecycle: PageLifecycleState;
};

export type PageLifecycleState = {
enabled: Signal<boolean>;
visitId: Signal<string | undefined>;
pageLoadedTimestamp: Signal<number | undefined>;
};

export type LoadOptionsState = Signal<LoadOptions>;

// TODO: define the metrics that we need to track
Expand Down Expand Up @@ -191,6 +202,7 @@ export interface ApplicationState {
storage: StorageState;
serverCookies: ServerCookiesState;
dataPlaneEvents: DataPlaneEventsState;
autoTrack: AutoTrackState;
}

export type DebouncedFunction = (...args: any[]) => void;
18 changes: 18 additions & 0 deletions packages/analytics-js-common/src/types/LoadOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,23 @@ export type PreConsentOptions = {
events?: PreConsentEventsOptions;
};

export enum PageLifecycleEvents {
LOADED = 'Page Loaded',
UNLOADED = 'Page Unloaded',
}

export type PageLifecycleOptions = {
enabled: boolean; // default false
events?: PageLifecycleEvents[];
options?: ApiOptions;
};

export type AutoTrackOptions = {
enabled?: boolean; // default false
options?: ApiOptions;
pageLifecycle?: PageLifecycleOptions;
};

/**
* Represents the options parameter in the load API
*/
Expand Down Expand Up @@ -150,6 +167,7 @@ export type LoadOptions = {
externalAnonymousIdCookieName?: string;
useServerSideCookies?: boolean;
dataServiceEndpoint?: string;
autoTrack?: AutoTrackOptions;
};

export type ConsentOptions = {
Expand Down
7 changes: 7 additions & 0 deletions packages/analytics-js-common/src/utilities/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ const onPageLeave = (callback: (isAccessible: boolean) => void) => {
pageLeft = true;

callback(isAccessible);

// Reset pageLeft on the next tick
// to ensure callback executes for other listeners
// when closing an inactive browser tab.
setTimeout(() => {
pageLeft = false;
}, 0);
}

// Catches the unloading of the page (e.g., closing the tab or navigating away).
Expand Down
30 changes: 15 additions & 15 deletions packages/analytics-js/.size-limit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,53 @@ export default [
name: 'Core - Legacy - NPM (ESM)',
path: 'dist/npm/legacy/esm/index.mjs',
import: '*',
limit: '49 KiB',
limit: '48.5 KiB',
},
{
name: 'Core - Legacy - NPM (CJS)',
path: 'dist/npm/legacy/cjs/index.cjs',
import: '*',
limit: '49 KiB',
limit: '48.5 KiB',
},
{
name: 'Core - Legacy - NPM (UMD)',
path: 'dist/npm/legacy/umd/index.js',
import: '*',
limit: '48 KiB',
limit: '48.5 KiB',
},
{
name: 'Core - Legacy - CDN',
path: 'dist/cdn/legacy/iife/rsa.min.js',
limit: '48 KiB',
limit: '48.5 KiB',
},
{
name: 'Core - Modern - NPM (ESM)',
path: 'dist/npm/modern/esm/index.mjs',
import: '*',
limit: '24.5 KiB',
limit: '25 KiB',
},
{
name: 'Core - Modern - NPM (CJS)',
path: 'dist/npm/modern/cjs/index.cjs',
import: '*',
limit: '24.5 KiB',
limit: '25 KiB',
},
{
name: 'Core - Modern - NPM (UMD)',
path: 'dist/npm/modern/umd/index.js',
import: '*',
limit: '24.5 KiB',
limit: '25 KiB',
},
{
name: 'Core - Modern - CDN',
path: 'dist/cdn/modern/iife/rsa.min.js',
limit: '24.5 KiB',
limit: '25 KiB',
},
{
name: 'Core (Bundled) - Legacy - NPM (ESM)',
path: 'dist/npm/legacy/bundled/esm/index.mjs',
import: '*',
limit: '48 KiB',
limit: '48.5 KiB',
},
{
name: 'Core (Bundled) - Legacy - NPM (CJS)',
Expand All @@ -65,19 +65,19 @@ export default [
name: 'Core (Bundled) - Legacy - NPM (UMD)',
path: 'dist/npm/legacy/bundled/umd/index.js',
import: '*',
limit: '48 KiB',
limit: '48.5 KiB',
},
{
name: 'Core (Bundled) - Modern - NPM (ESM)',
path: 'dist/npm/modern/bundled/esm/index.mjs',
import: '*',
limit: '39 KiB',
limit: '39.5 KiB',
},
{
name: 'Core (Bundled) - Modern - NPM (CJS)',
path: 'dist/npm/modern/bundled/cjs/index.cjs',
import: '*',
limit: '39.5 KiB',
limit: '40 KiB',
},
{
name: 'Core (Bundled) - Modern - NPM (UMD)',
Expand All @@ -101,13 +101,13 @@ export default [
name: 'Core (Content Script) - Legacy - NPM (UMD)',
path: 'dist/npm/legacy/content-script/umd/index.js',
import: '*',
limit: '47.5 KiB',
limit: '48 KiB',
},
{
name: 'Core (Content Script) - Modern - NPM (ESM)',
path: 'dist/npm/modern/content-script/esm/index.mjs',
import: '*',
limit: '38.5 KiB',
limit: '39 KiB',
},
{
name: 'Core (Content Script) - Modern - NPM (CJS)',
Expand All @@ -119,6 +119,6 @@ export default [
name: 'Core (Content Script) - Modern - NPM (UMD)',
path: 'dist/npm/modern/content-script/umd/index.js',
import: '*',
limit: '38.5 KiB',
limit: '39 KiB',
},
];
Loading

0 comments on commit c6c8e3f

Please sign in to comment.