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: 14 additions & 4 deletions website/src/scripts/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ async function initializeConsent(allowAnalytics: boolean): Promise<void> {
});
};
const theme = () => document.documentElement.dataset.theme === 'light' ? 'light' : 'dark';
const initialTheme = theme();
wcp.init(document.documentElement.lang || 'en-US', banner, (error, consent) => {
if (error || !consent) {
console.error('Scope cookie consent could not initialize', error ?? new Error('Missing consent manager'));
Expand All @@ -150,10 +151,19 @@ async function initializeConsent(allowAnalytics: boolean): Promise<void> {
manageCookies.hidden = false;
manageCookies.addEventListener('click', () => consent.manageConsent());
onConsentChanged();
new MutationObserver(() => {
consent.applyTheme(theme());
}).observe(document.documentElement, { attributes: true, attributeFilter: ['data-theme'] });
}, onConsentChanged, theme());
let appliedTheme = initialTheme;
const applyTheme = () => {
const nextTheme = theme();
if (nextTheme === appliedTheme) return;
appliedTheme = nextTheme;
consent.applyTheme(nextTheme);
};
applyTheme();
new MutationObserver(applyTheme).observe(
document.documentElement,
{ attributes: true, attributeFilter: ['data-theme'] },
);
}, onConsentChanged, initialTheme);
}

const base = import.meta.env.BASE_URL.replace(/\/?$/, '/');
Expand Down
16 changes: 12 additions & 4 deletions website/tests/analytics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,18 @@ test('stored consent initializes once and can be withdrawn and granted again', a
test('consent UI follows Starlight theme changes', async ({ page }) => {
await mockPublishedSite(page);
await page.goto('https://microsoft.github.io/scope/');
await page.evaluate(() => { document.documentElement.dataset.theme = 'light'; });
await expect.poll(() => page.evaluate(() => window.scopeAnalyticsTest.appliedThemes)).toEqual(['light']);
await page.evaluate(() => { document.documentElement.dataset.theme = 'dark'; });
await expect.poll(() => page.evaluate(() => window.scopeAnalyticsTest.appliedThemes)).toEqual(['light', 'dark']);
await expect.poll(() => page.evaluate(() => Boolean(window.scopeAnalyticsTest.changeConsent))).toBe(true);
const initialTheme = await page.evaluate(
() => document.documentElement.dataset.theme === 'light' ? 'light' : 'dark',
);
const nextTheme = initialTheme === 'light' ? 'dark' : 'light';

await page.evaluate((theme) => { document.documentElement.dataset.theme = theme; }, initialTheme);
await expect.poll(() => page.evaluate(() => window.scopeAnalyticsTest.appliedThemes)).toEqual([]);
await page.evaluate((theme) => { document.documentElement.dataset.theme = theme; }, nextTheme);
await expect.poll(() => page.evaluate(() => window.scopeAnalyticsTest.appliedThemes)).toEqual([nextTheme]);
await page.evaluate((theme) => { document.documentElement.dataset.theme = theme; }, initialTheme);
await expect.poll(() => page.evaluate(() => window.scopeAnalyticsTest.appliedThemes)).toEqual([nextTheme, initialTheme]);
});

for (const options of [
Expand Down
Loading