diff --git a/src/Gleap.js b/src/Gleap.js index 49deaf33..a84b22bf 100755 --- a/src/Gleap.js +++ b/src/Gleap.js @@ -1717,7 +1717,7 @@ class Gleap { .startProductTourConfig(tourId) .then((config) => { if (checkStartUrl && config?.startURL && !window?.location?.href?.includes(config?.baseURL)) { - window.location.href = `${config.startURL}?gleap_tour=${tourId}&gleap_tour_delay=1`; + window.location.href = Gleap.buildTourStartUrl(config.startURL, tourId); return; } @@ -1726,6 +1726,22 @@ class Gleap { .catch((error) => {}); } + // Adds the tour params to the start URL, keeping its own query (e.g. ?view=list) and hash. + static buildTourStartUrl(startURL, tourId) { + try { + const url = new URL(startURL, window.location.href); + url.searchParams.set('gleap_tour', tourId); + url.searchParams.set('gleap_tour_delay', '1'); + return url.toString(); + } catch (exp) { + const [base, hash] = startURL.split('#'); + const separator = base.includes('?') ? '&' : '?'; + return `${base}${separator}gleap_tour=${encodeURIComponent(tourId)}&gleap_tour_delay=1${ + hash !== undefined ? `#${hash}` : '' + }`; + } + } + static checkForUncompletedTour() { const tourData = GleapProductTours.getInstance().loadUncompletedTour(); if (tourData) { diff --git a/src/GleapProductTourStart.test.js b/src/GleapProductTourStart.test.js new file mode 100644 index 00000000..a2825ba2 --- /dev/null +++ b/src/GleapProductTourStart.test.js @@ -0,0 +1,60 @@ +/** + * @jest-environment jsdom + */ +import Gleap, { GleapSession } from './Gleap'; + +const flush = () => new Promise((resolve) => setTimeout(resolve, 0)); + +// A checklist step starts a tour on another page: the SDK redirects to the tour's start URL +// and the tour starts there from the gleap_tour query param (#229004). +describe('Gleap.startProductTour redirect to the start URL', () => { + const originalLocation = window.location; + let configSpy; + + const startFrom = async (currentUrl, config) => { + delete window.location; + window.location = { href: currentUrl }; + configSpy = jest.spyOn(GleapSession.getInstance(), 'startProductTourConfig').mockResolvedValue(config); + Gleap.startProductTour('tour-1', true); + await flush(); + return new URL(window.location.href); + }; + + afterEach(() => { + configSpy.mockRestore(); + window.location = originalLocation; + }); + + it('adds the tour params to a start URL that has no query', async () => { + const target = await startFrom('https://app.example.com/home', { + startURL: 'https://app.example.com/workspaces', + baseURL: 'https://app.example.com/workspaces', + }); + + expect(target.pathname).toBe('/workspaces'); + expect(target.searchParams.get('gleap_tour')).toBe('tour-1'); + expect(target.searchParams.get('gleap_tour_delay')).toBe('1'); + }); + + it('keeps the query of a start URL that already has one', async () => { + const target = await startFrom('https://app.example.com/home', { + startURL: 'https://app.example.com/workspaces?view=list', + baseURL: 'https://app.example.com/workspaces?view=list', + }); + + expect(target.searchParams.get('view')).toBe('list'); + expect(target.searchParams.get('gleap_tour')).toBe('tour-1'); + expect(target.searchParams.get('gleap_tour_delay')).toBe('1'); + }); + + it('keeps the hash of the start URL after the tour params', async () => { + const target = await startFrom('https://app.example.com/home', { + startURL: 'https://app.example.com/workspaces?view=list#board', + baseURL: 'https://app.example.com/workspaces', + }); + + expect(target.searchParams.get('view')).toBe('list'); + expect(target.searchParams.get('gleap_tour')).toBe('tour-1'); + expect(target.hash).toBe('#board'); + }); +});