From 5508952fa4a7aa7b945cf8780fd5cf397ba9182a Mon Sep 17 00:00:00 2001 From: b4rtaz Date: Fri, 20 Oct 2023 23:47:31 +0200 Subject: [PATCH] ga. --- .github/workflows/examples.yml | 3 +++ scripts/append-ga.cjs | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 scripts/append-ga.cjs diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index c121249..b0d24ca 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -34,6 +34,9 @@ jobs: run: pnpm install - name: Build run: pnpm build + - name: Append Analytics + run: | + node scripts/append-ga.cjs demos/webpack-app/public - name: Upload artifact uses: actions/upload-pages-artifact@v1 with: diff --git a/scripts/append-ga.cjs b/scripts/append-ga.cjs new file mode 100644 index 0000000..51709c4 --- /dev/null +++ b/scripts/append-ga.cjs @@ -0,0 +1,43 @@ +const fs = require('fs'); +const path = require('path'); + +const SCRIPT = +` +`; + +function appendGa(path) { + let html = fs.readFileSync(path, 'utf8'); + if (html.includes(SCRIPT)) { + console.log(`👌 ${path} already has tracking`); + return; + } + const pos = html.lastIndexOf(''); + if (pos < 0) { + throw new Error('Could not find tag'); + } + html = html.substring(0, pos) + SCRIPT + html.substring(pos); + fs.writeFileSync(path, html, 'utf8'); + console.log(`✅ ${path} updated`); +} + +const directory = process.argv[2]; +if (!directory) { + throw new Error('Please specify a directory'); +} + +const dirPath = path.join(__dirname, '..', directory); +fs.readdir(dirPath, (_, files) => { + files.forEach(file => { + if (file.endsWith('.html')) { + const filePath = path.join(dirPath, file); + appendGa(filePath); + } + }); +});