Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 17 additions & 1 deletion src/Gleap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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) {
Expand Down
60 changes: 60 additions & 0 deletions src/GleapProductTourStart.test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
Loading