Skip to content

Commit

Permalink
ga.
Browse files Browse the repository at this point in the history
  • Loading branch information
b4rtaz committed Oct 20, 2023
1 parent a75f4b3 commit 5508952
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
43 changes: 43 additions & 0 deletions scripts/append-ga.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const fs = require('fs');
const path = require('path');

const SCRIPT =
`<script async src="https://www.googletagmanager.com/gtag/js?id=G-CDBK7LBGM8"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-CDBK7LBGM8');
</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('</body>');
if (pos < 0) {
throw new Error('Could not find </body> 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);
}
});
});

0 comments on commit 5508952

Please sign in to comment.