Skip to content

Conversation

@DmitryGron
Copy link
Contributor

@DmitryGron DmitryGron commented Dec 12, 2025

Resolves JIRA: https://bbc.atlassian.net/browse/WS-1838

Summary

Adding flag for offline tracking

New Hooks:

useOfflinePageFlag - Sets flag when offline page visited in PWA
usePWAOfflineTracking - Fires pwa-offline-page-view event on offline→online transition
useSendPWAStatus - Sends PWA status to service worker
useServiceWorkerRegistration - Handles SW registration:

Added tests for all 4 hooks following codebase patterns

Fixed merge conflict in LatestMediaItem styles
Updated Storybook 10.0.8 → 10.1.10

Testing Instructions
How to test the offline flag and tracking
Setup:

Stop your dev server
Close Chrome completely and uninstall any existing PWA
Start the dev server: yarn dev
Go to http://localhost:7081/mundo/live/c7dkx155e626t?renderer_env=local
Install it as a PWA (click the install icon in the address bar)
Test the flag:

Once installed, refresh the page to make sure the offline page gets cached
Open DevTools, go to Network tab and set it to Offline
Refresh the page - you should see the custom offline page
Check Local Storage (in DevTools → Application → Local Storage)
You should see offline_page_visit with value "true"
Test the tracking event:

While still on the offline page with the flag set
Open the Console tab in DevTools
Switch Network back to Online
You should see a tracking beacon fire with event name pwa-offline-page-view and network type
Check Local Storage again - the offline_page_visit flag should be gone
Test it fires again:

Go offline again, refresh to see the offline page
The flag gets set again
Go back online
The event fires again and the flag is removed
That's it. The event should fire every time you go from offline → online after visiting the offline page.

Developer Checklist

  • UX
    • UX Criteria met (visual UX & screenreader UX)
  • Accessibility
    • Accessibility Acceptance Criteria met
    • Accessibility swarm completed
    • Component Health updated
    • P1 accessibility bugs resolved
    • P2/P3 accessibility bugs planned (if not resolved)
  • Security
    • Security issues addressed
    • Threat Model updated
  • Documentation
    • Docs updated (runbook, READMEs)
  • Testing
    • Feature tested on relevant environments
  • Comms
    • Relevant parties notified of changes

Testing

  • Manual Testing required?
    • Local (Ready-For-Test, Local)
    • Test (Ready-For-Test, Test)
    • Preview (Ready-For-Test, Preview)
    • Live (Ready-For-Test, Live)
  • Manual Testing complete?
    • Local
    • Test
    • Preview
    • Live

Additional Testing Steps

  1. List the steps required to test this PR.

Useful Links

@DmitryGron DmitryGron requested a review from elvinasv December 16, 2025 13:54
@amoore108
Copy link
Contributor

Can we add some tests around the new useOfflinePageFlag and usePWAOfflineTracking hooks?

@DmitryGron DmitryGron requested a review from a team as a code owner December 19, 2025 13:48
@elvinasv elvinasv changed the title Feature/ws 1838 pwa offline flag WS-1838: Add PWA Offline page tracking Dec 22, 2025
@LukasFrm LukasFrm self-requested a review December 23, 2025 10:00

const title = 'You are offline';
const message =
"It seems you don't have an internet connection at the moment. Please check your connection and reload the page.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a semantic suggestion - the word “connection” seems a bit repetitive, what if we simplified & removed the bullet points? E.g.:
You’re Offline
Looks like you’re not online right now. Please check your network and reconnect. Once you’re back, just refresh the page to continue.

statsDestination,
].every(Boolean);

console.log('Custom Event Tracker', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove the console.logs at this point.

}, [isPWA]);
};

export default useOfflinePageFlag;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: export { useOfflinePageFlag, OFFLINE_VISIT_FLAG };

Comment on lines 17 to 34
it('should set offline flag when in PWA mode', () => {
mockUseIsPWA.mockReturnValue(true);

renderHook(() => useOfflinePageFlag());

expect(localStorage.setItem).toHaveBeenCalledWith(
OFFLINE_VISIT_FLAG,
'true',
);
});

it('should not set flag when in browser mode', () => {
mockUseIsPWA.mockReturnValue(false);

renderHook(() => useOfflinePageFlag());

expect(localStorage.setItem).not.toHaveBeenCalled();
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is all this logic redundant as we only show the offline page in the PWA anyway? So the flag would only be set on offline pages that would only be visited in the PWA? Sorry I may be missing something here.

expect(mockTrackOfflinePageViewEvent).not.toHaveBeenCalled();
});

it('should fire event when in PWA mode, online, and flag is set', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the flag is set it must have been set while the PWA was offline, I'm not sure it matters if the event is sent from browser mode? I think this could potentially decrease the number of metrics we send as multiple offline visits will be treated as one if the event is not dispatched and the flag cleared.

So current logic (PWA state considered):
PWA == true && Online == false -> set flag
PWA == false && Online == true -> Do nothing as PWA is false
PWA == true && Online == false -> No need to set flag as already set
PWA == true && Online == true -> Dispatch tracking event

In this logic only one event is sent even though the user completed to offline browsing sessions that could have been tracked.

With alternative logic (PWA state ignored)
PWA == true && Online == false -> set flag
PWA == false && Online == true -> Dispatch tracking event as we are online and a previous offline session was recorded while using the PWA
PWA == true && Online == false -> set flag again as we have another offline session since the last one
PWA == true && Online == true -> Dispatch tracking event again as we've had another offline session since the first

This revised logic tracks both sessions seperately giving us more accurate analytics

'4g',
'5g',
'unknown',
] as const;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn be good to use jests each functionality here: https://jestjs.io/docs/api#testeachtablename-fn-timeout

eventName: OFFLINE_PAGE_VIEW_EVENT_NAME,
});

console.log('usePWAOfflineTracking invoked', {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be removed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.