Skip to content

Commit 3b60656

Browse files
Keep the start URL's query when redirecting to start a product tour (#229004)
A start URL with a query string got a second '?', so the gleap_tour param was lost after the redirect and the tour never started. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 9b31399 commit 3b60656

2 files changed

Lines changed: 77 additions & 1 deletion

File tree

‎src/Gleap.js‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,7 @@ class Gleap {
17171717
.startProductTourConfig(tourId)
17181718
.then((config) => {
17191719
if (checkStartUrl && config?.startURL && !window?.location?.href?.includes(config?.baseURL)) {
1720-
window.location.href = `${config.startURL}?gleap_tour=${tourId}&gleap_tour_delay=1`;
1720+
window.location.href = Gleap.buildTourStartUrl(config.startURL, tourId);
17211721
return;
17221722
}
17231723

@@ -1726,6 +1726,22 @@ class Gleap {
17261726
.catch((error) => {});
17271727
}
17281728

1729+
// Adds the tour params to the start URL, keeping its own query (e.g. ?view=list) and hash.
1730+
static buildTourStartUrl(startURL, tourId) {
1731+
try {
1732+
const url = new URL(startURL, window.location.href);
1733+
url.searchParams.set('gleap_tour', tourId);
1734+
url.searchParams.set('gleap_tour_delay', '1');
1735+
return url.toString();
1736+
} catch (exp) {
1737+
const [base, hash] = startURL.split('#');
1738+
const separator = base.includes('?') ? '&' : '?';
1739+
return `${base}${separator}gleap_tour=${encodeURIComponent(tourId)}&gleap_tour_delay=1${
1740+
hash !== undefined ? `#${hash}` : ''
1741+
}`;
1742+
}
1743+
}
1744+
17291745
static checkForUncompletedTour() {
17301746
const tourData = GleapProductTours.getInstance().loadUncompletedTour();
17311747
if (tourData) {

‎src/GleapProductTourStart.test.js‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* @jest-environment jsdom
3+
*/
4+
import Gleap, { GleapSession } from './Gleap';
5+
6+
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
7+
8+
// A checklist step starts a tour on another page: the SDK redirects to the tour's start URL
9+
// and the tour starts there from the gleap_tour query param (#229004).
10+
describe('Gleap.startProductTour redirect to the start URL', () => {
11+
const originalLocation = window.location;
12+
let configSpy;
13+
14+
const startFrom = async (currentUrl, config) => {
15+
delete window.location;
16+
window.location = { href: currentUrl };
17+
configSpy = jest.spyOn(GleapSession.getInstance(), 'startProductTourConfig').mockResolvedValue(config);
18+
Gleap.startProductTour('tour-1', true);
19+
await flush();
20+
return new URL(window.location.href);
21+
};
22+
23+
afterEach(() => {
24+
configSpy.mockRestore();
25+
window.location = originalLocation;
26+
});
27+
28+
it('adds the tour params to a start URL that has no query', async () => {
29+
const target = await startFrom('https://app.example.com/home', {
30+
startURL: 'https://app.example.com/workspaces',
31+
baseURL: 'https://app.example.com/workspaces',
32+
});
33+
34+
expect(target.pathname).toBe('/workspaces');
35+
expect(target.searchParams.get('gleap_tour')).toBe('tour-1');
36+
expect(target.searchParams.get('gleap_tour_delay')).toBe('1');
37+
});
38+
39+
it('keeps the query of a start URL that already has one', async () => {
40+
const target = await startFrom('https://app.example.com/home', {
41+
startURL: 'https://app.example.com/workspaces?view=list',
42+
baseURL: 'https://app.example.com/workspaces?view=list',
43+
});
44+
45+
expect(target.searchParams.get('view')).toBe('list');
46+
expect(target.searchParams.get('gleap_tour')).toBe('tour-1');
47+
expect(target.searchParams.get('gleap_tour_delay')).toBe('1');
48+
});
49+
50+
it('keeps the hash of the start URL after the tour params', async () => {
51+
const target = await startFrom('https://app.example.com/home', {
52+
startURL: 'https://app.example.com/workspaces?view=list#board',
53+
baseURL: 'https://app.example.com/workspaces',
54+
});
55+
56+
expect(target.searchParams.get('view')).toBe('list');
57+
expect(target.searchParams.get('gleap_tour')).toBe('tour-1');
58+
expect(target.hash).toBe('#board');
59+
});
60+
});

0 commit comments

Comments
 (0)