diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml
index 2c5da26..becdcdc 100644
--- a/.github/workflows/pages.yml
+++ b/.github/workflows/pages.yml
@@ -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
diff --git a/README.md b/README.md
index 0b13a27..643eb16 100644
--- a/README.md
+++ b/README.md
@@ -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
diff --git a/src/layouts/Site.astro b/src/layouts/Site.astro
index b4e4ec3..a46a855 100644
--- a/src/layouts/Site.astro
+++ b/src/layouts/Site.astro
@@ -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 === '/';
---
+ {enableTagManager && }
{title}
diff --git a/src/pages/404.astro b/src/pages/404.astro
index f0c3a17..3fd9370 100644
--- a/src/pages/404.astro
+++ b/src/pages/404.astro
@@ -4,7 +4,7 @@ import Hero from '../components/Hero.astro';
const base = import.meta.env.BASE_URL.replace(/\/$/, '');
---
-
+
We couldn’t find that page.
diff --git a/tests/analytics.spec.mjs b/tests/analytics.spec.mjs
new file mode 100644
index 0000000..681c212
--- /dev/null
+++ b/tests/analytics.spec.mjs
@@ -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(/[\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();
+ }
+ });
+}