Commit b5597c91564 for woocommerce

commit b5597c915644c8a024ae79e1da3ab5dd6248dbeb
Author: Chi-Hsuan Huang <chihsuan.tw@gmail.com>
Date:   Fri Dec 4 10:22:29 2026 +0800

    Fix analytics sessions ending 30 minutes after they start (#71632)

    * End analytics sessions after 30 minutes of inactivity

    * Add changelog entry for analytics session inactivity timeout

    * Keep session tests valid past their dates and update the README

    * Avoid Array.prototype.at in session tests for the package's TS lib

    * Guard the session fields the expiry refresh must keep and trim overlapping tests

    * Say what merchants will see in the session inactivity changelog entry

    * Switch pages before the second view so the landing page test can fail

diff --git a/packages/php/woocommerce-analytics/README.md b/packages/php/woocommerce-analytics/README.md
index be6d24df5a8..13d38569d7c 100644
--- a/packages/php/woocommerce-analytics/README.md
+++ b/packages/php/woocommerce-analytics/README.md
@@ -261,7 +261,7 @@ The package uses a hierarchical approach to identify users for analytics:

 **Session Management:**

--   Session cookies (`woocommerceanalytics_session`) expire after 30 minutes or at midnight UTC (whichever comes first)
+-   Session cookies (`woocommerceanalytics_session`) expire after 30 minutes without a page view, or at midnight UTC (whichever comes first)
 -   Used for tracking user journey within a session (separate from user identification)
 -   Contains session ID, landing page, and engagement status

diff --git a/packages/php/woocommerce-analytics/changelog/uni-1130-session-inactivity b/packages/php/woocommerce-analytics/changelog/uni-1130-session-inactivity
new file mode 100644
index 00000000000..eeee1081f3c
--- /dev/null
+++ b/packages/php/woocommerce-analytics/changelog/uni-1130-session-inactivity
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fixed
+
+Keep a visit in one analytics session until 30 minutes pass without a page view, so session counts may drop and conversion rates rise.
diff --git a/packages/php/woocommerce-analytics/src/client/session-manager.ts b/packages/php/woocommerce-analytics/src/client/session-manager.ts
index 03db21c30e7..97946d61611 100644
--- a/packages/php/woocommerce-analytics/src/client/session-manager.ts
+++ b/packages/php/woocommerce-analytics/src/client/session-manager.ts
@@ -36,7 +36,8 @@ export default class SessionManager {
 		const cookie = this.getSessionCookie();

 		if ( cookie && cookie.session_id ) {
-			// Load existing session
+			// A page view is activity: the session ends 30 minutes after the last one, not the first.
+			this.setSessionCookie( { ...cookie, expires: this.getSessionExpirationTime() } );
 			this.sessionId = cookie.session_id;
 			this.landingPage = cookie.landing_page || null;
 			this.isEngaged = cookie.is_engaged || false;
diff --git a/packages/php/woocommerce-analytics/src/client/test/session-manager.test.ts b/packages/php/woocommerce-analytics/src/client/test/session-manager.test.ts
new file mode 100644
index 00000000000..fb43a74a15a
--- /dev/null
+++ b/packages/php/woocommerce-analytics/src/client/test/session-manager.test.ts
@@ -0,0 +1,96 @@
+/**
+ * @jest-environment-options {"url": "https://shop.example.com/"}
+ */
+
+/**
+ * External dependencies
+ */
+import { afterEach, beforeEach, describe, expect, it, jest } from '@jest/globals';
+
+/**
+ * Internal dependencies
+ */
+import SessionManager from '../session-manager';
+
+const MINUTE = 60 * 1000;
+const DAY = 24 * 60 * MINUTE;
+
+// jsdom expires cookies on the real clock, so every faked time is tomorrow or later.
+const TOMORROW = new Date( Date.now() + DAY ).toISOString().slice( 0, 10 );
+const tomorrowAt = ( time: string ) => new Date( `${ TOMORROW }T${ time }Z` );
+
+const startPageView = () => {
+	const sessionManager = new SessionManager();
+	sessionManager.init();
+	return sessionManager;
+};
+
+const readCookie = () => new SessionManager().getSessionCookie();
+
+// The cookie jar hides the expiry, so read the one the browser is told.
+const lastWrittenExpiry = ( setCookie: jest.SpiedSetter< string > ) =>
+	String( setCookie.mock.calls[ setCookie.mock.calls.length - 1 ]?.[ 0 ] ).match( /expires=([^;]+)/ )?.[ 1 ];
+
+describe( 'SessionManager', () => {
+	beforeEach( () => {
+		jest.useFakeTimers();
+	} );
+
+	afterEach( () => {
+		new SessionManager().clearSession();
+		delete window.wcAnalytics;
+		jest.restoreAllMocks();
+		jest.useRealTimers();
+	} );
+
+	it( 'keeps the session and moves its expiry 30 minutes past each page view', () => {
+		window.wcAnalytics = {
+			trackEndpoint: '',
+			eventQueue: [],
+			commonProps: {},
+			features: {},
+			pages: {},
+			assets_url: '',
+			breadcrumbs: [ 'Shop' ],
+		};
+		jest.setSystemTime( tomorrowAt( '10:00:00' ) );
+		const first = startPageView();
+		first.setEngaged();
+
+		jest.setSystemTime( tomorrowAt( '10:20:00' ) );
+		const setCookie = jest.spyOn( Document.prototype, 'cookie', 'set' );
+		window.wcAnalytics.breadcrumbs = [ 'Cart' ];
+		const second = startPageView();
+
+		const expected = tomorrowAt( '10:50:00' ).toUTCString();
+		expect( second.isNewSession ).toBe( false );
+		expect( lastWrittenExpiry( setCookie ) ).toBe( expected );
+		expect( readCookie() ).toEqual( {
+			session_id: first.sessionId,
+			landing_page: '["Shop"]',
+			is_engaged: true,
+			expires: expected,
+		} );
+	} );
+
+	it( 'moves the expiry 30 minutes past the latest page view, not the first', () => {
+		jest.setSystemTime( tomorrowAt( '10:00:00' ) );
+		startPageView();
+
+		for ( let i = 0; i < 4; i++ ) {
+			jest.advanceTimersByTime( 20 * MINUTE );
+			startPageView();
+			expect( readCookie()?.expires ).toBe( new Date( Date.now() + 30 * MINUTE ).toUTCString() );
+		}
+	} );
+
+	it( 'never extends the session past the next UTC midnight', () => {
+		jest.setSystemTime( tomorrowAt( '23:20:00' ) );
+		startPageView();
+
+		jest.setSystemTime( tomorrowAt( '23:45:00' ) );
+		startPageView();
+
+		expect( readCookie()?.expires ).toBe( new Date( tomorrowAt( '00:00:00' ).getTime() + DAY ).toUTCString() );
+	} );
+} );