Skip to content

Commit

Permalink
0.3.0. (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
b4rtaz authored Jan 14, 2024
1 parent 8a225ce commit bb35deb
Showing 1 changed file with 43 additions and 0 deletions.
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-8N9MBTQKCK"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-8N9MBTQKCK');
</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 bb35deb

Please sign in to comment.