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

feat: track time spent on a page #1876

Merged
merged 27 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
db562b0
feat: track time spent on a page
MoumitaM Oct 8, 2024
9c4a494
chore: moved the comment to appt place
MoumitaM Oct 8, 2024
02ffa04
refactor: trackPageLifecycleEvents fn
MoumitaM Oct 9, 2024
5813d01
chore: review comment addressed
MoumitaM Oct 9, 2024
f864f6a
chore: review comment addressed
MoumitaM Oct 9, 2024
eafe04f
chore: review comment addressed
MoumitaM Oct 9, 2024
5615d68
refactor: trackPageLifecycleEvents fn will be called from load api
MoumitaM Oct 14, 2024
e7e41aa
Merge branch 'develop' into feat/SDK-2478-track-timespent-on-a-page
MoumitaM Oct 14, 2024
72555d5
chore: addressed review comment
MoumitaM Oct 14, 2024
a390642
feat: modified load options structure
MoumitaM Oct 14, 2024
40c683c
chore: trackPageLifecycleEvents fn has been refactored and unit tests…
MoumitaM Oct 16, 2024
cc62338
chore: size limit updated
MoumitaM Oct 16, 2024
ab72611
Merge branch 'develop' into feat/SDK-2478-track-timespent-on-a-page
MoumitaM Oct 16, 2024
e4d5492
chore: typo corrected
MoumitaM Oct 16, 2024
6c7e207
chore: unit tests
MoumitaM Oct 17, 2024
8da4e56
Merge branch 'develop' into feat/SDK-2478-track-timespent-on-a-page
MoumitaM Oct 17, 2024
26a26c5
chore: size limit update
MoumitaM Oct 18, 2024
6455d95
chore: addressed review comments
MoumitaM Oct 18, 2024
56b1859
chore: eslint issue fixed
MoumitaM Oct 18, 2024
92b7442
fix: execution of cb when multiplt event listeners are attached
MoumitaM Oct 21, 2024
82fb915
chore: inline doc updated
MoumitaM Oct 21, 2024
6c9a94d
chore: readme updated
MoumitaM Oct 21, 2024
412beeb
Merge branch 'develop' into feat/SDK-2478-track-timespent-on-a-page
MoumitaM Oct 21, 2024
7c97388
chore: unit test added
MoumitaM Oct 22, 2024
90c97ec
Merge branch 'develop' into feat/SDK-2478-track-timespent-on-a-page
MoumitaM Oct 22, 2024
ebcc0c3
chore: unit test updated
MoumitaM Oct 22, 2024
7aea66e
Merge branch 'feat/SDK-2478-track-timespent-on-a-page' of https://git…
MoumitaM Oct 22, 2024
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
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 = {
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
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 @@
events?: PreConsentEventsOptions;
};

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

Check warning on line 113 in packages/analytics-js-common/src/types/LoadOptions.ts

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js-common/src/types/LoadOptions.ts#L112-L113

Added lines #L112 - L113 were not covered by tests
}

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 @@
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
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
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
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
// when closing an inactive browser tab.
setTimeout(() => {
pageLeft = false;
}, 0);
MoumitaM marked this conversation as resolved.
Show resolved Hide resolved
}

// 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: '48 KiB',
limit: '48.5 KiB',
},
{
name: 'Core - Legacy - NPM (CJS)',
path: 'dist/npm/legacy/cjs/index.cjs',
import: '*',
limit: '48 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