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
4 changes: 4 additions & 0 deletions .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ jobs:
- run: npm ci
- run: npx playwright install --with-deps chromium
- run: npm run check
- name: Build production artifact with Tag Manager
run: npm run build
env:
DASK_PAGES_ANALYTICS: '1'
- uses: actions/upload-pages-artifact@7b1f4a764d45c48632c6b24a0339c27f5614fb0b # v4
with:
path: dist
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ GitHub Actions CI checks pull requests. The Pages workflow builds and publishes
upstream main, with separate build and deployment jobs and main-only manual
runs. Read the Docs builds native Astro PR previews with version-mounted links,
assets and canonical URLs. The production canonical origin is https://dask.org/.
The production Pages artifact loads Google Tag Manager on content pages only
when JavaScript runs on that origin. There is no no-script tracking fallback;
regular local builds and Read the Docs previews omit Tag Manager.

Blog, events and the news archive routes target https://blog.dask.org/.
Known routes include HTML redirects; unknown scoped paths use the static 404
Expand Down
23 changes: 23 additions & 0 deletions src/layouts/Site.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,38 @@ import Header from '../components/Header.astro';
import Footer from '../components/Footer.astro';
import Dialogs from '../components/Dialogs.astro';
const {
analytics = true,
title = 'Dask | Parallel Python',
description = 'Dask is a flexible library for parallel computing in Python.',
} = Astro.props;
const tagManagerId = 'GTM-P4GQM59';
const enableTagManager =
analytics &&
import.meta.env.PROD &&
process.env.DASK_PAGES_ANALYTICS === '1' &&
!process.env.READTHEDOCS_CANONICAL_URL &&
Astro.site?.origin === 'https://dask.org' &&
import.meta.env.BASE_URL === '/';
---

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
{enableTagManager && <script define:vars={{ tagManagerId }}>
if (window.location.origin === 'https://dask.org') {
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'gtm.start': Date.now(),
event: 'gtm.js',
});
const loader = document.createElement('script');
loader.async = true;
loader.src =
'https://www.googletagmanager.com/gtm.js?id=' + tagManagerId;
document.head.appendChild(loader);
}
</script>}
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content={description} />
<title>{title}</title>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/404.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Hero from '../components/Hero.astro';
const base = import.meta.env.BASE_URL.replace(/\/$/, '');
---

<Site title="Dask | Page not available">
<Site title="Dask | Page not available" analytics={false}>
<Hero title="Page not available" />
<section class="unavailable light-grid">
<h2>We couldn’t find that page.</h2>
Expand Down
132 changes: 132 additions & 0 deletions tests/analytics.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { execFile } from 'node:child_process';
import { mkdtemp, readFile, readdir, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { promisify } from 'node:util';
import { expect, pages, test } from './support/site.mjs';

const canonical = 'https://dask.org';
const id = 'GTM-P4GQM59';
const tagManager = 'https://www.googletagmanager.com';
const loader = `${tagManager}/gtm.js?id=${id}`;
let output;

test.beforeAll(async () => {
output = await mkdtemp(path.join(tmpdir(), 'dask-analytics-'));
await promisify(execFile)(
process.execPath,
['node_modules/astro/bin/astro.mjs', 'build', '--outDir', output],
{
env: {
...process.env,
DASK_PAGES_ANALYTICS: '1',
READTHEDOCS_CANONICAL_URL: '',
},
},
);
});

test.afterAll(async () => {
if (output) await rm(output, { recursive: true, force: true });
});

test('only the six content pages in the production artifact contain the original container', async () => {
const tracked = pages.map(({ path: route }) =>
path.join(route, 'index.html'),
);
const files = await readdir(output, { recursive: true });
expect(files).toEqual(expect.arrayContaining(tracked));
for (const file of files) {
if (!file.endsWith('.html')) continue;
const html = await readFile(path.join(output, file), 'utf8');
expect(html).not.toMatch(/UA-18218894-5|G-M6Z7MD5JX6|3553212/);
expect(html).not.toContain(`${tagManager}/ns.html`);
if (tracked.includes(file)) {
expect(html.split(id)).toHaveLength(2);
expect(html.match(/googletagmanager\.com\/gtm\.js/g)).toHaveLength(1);
expect(html).toMatch(/<head>[\s\S]*gtm\.js[\s\S]*<\/head>/);
} else {
expect(html).not.toContain(tagManager);
}
}
});

test('regular builds and Read the Docs previews omit Tag Manager', async ({
request,
baseURL,
mountedURL,
}) => {
for (const root of [baseURL, mountedURL]) {
for (const { path: route } of pages) {
const response = await request.get(new URL(route, root).href);
expect(response.ok()).toBe(true);
expect(await response.text()).not.toContain(tagManager);
}
}
});

for (const scenario of [
{
name: 'canonical host',
origin: canonical,
script: true,
expected: [loader],
},
{
name: 'alternate host',
origin: 'https://dask.github.io',
script: true,
expected: [],
},
{
name: 'canonical host without JavaScript',
origin: canonical,
script: false,
expected: [],
},
{
name: 'alternate host without JavaScript',
origin: 'https://dask.github.io',
script: false,
expected: [],
},
]) {
test(`Tag Manager requests on ${scenario.name} are scoped and stubbed`, async ({
browser,
}) => {
const context = await browser.newContext({
javaScriptEnabled: scenario.script,
serviceWorkers: 'block',
});
try {
const requests = [];
const homepage = `${scenario.origin}/`;
const html = await readFile(path.join(output, 'index.html'));
// No network request leaves the browser, including undocumented vendors.
await context.route('**/*', async (route) => {
const url = route.request().url();
if (url === homepage)
return route.fulfill({ contentType: 'text/html', body: html });
if (new URL(url).origin === scenario.origin) return route.abort();
requests.push(url);
if (url === loader)
return route.fulfill({
contentType: 'application/javascript',
body: '',
});
return route.abort();
});
const page = await context.newPage();
await page.goto(homepage);
expect(requests).toEqual(scenario.expected);
if (scenario.script) {
const events = await page.evaluate(() =>
(window.dataLayer || []).map((entry) => entry.event),
);
expect(events).toEqual(scenario.expected.length ? ['gtm.js'] : []);
}
} finally {
await context.close();
}
});
}