Skip to content

Commit bb35deb

Browse files
authored
0.3.0. (#4)
1 parent 8a225ce commit bb35deb

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

scripts/append-ga.cjs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const SCRIPT =
5+
`<script async src="https://www.googletagmanager.com/gtag/js?id=G-8N9MBTQKCK"></script>
6+
<script>
7+
window.dataLayer = window.dataLayer || [];
8+
function gtag() {
9+
dataLayer.push(arguments);
10+
}
11+
gtag('js', new Date());
12+
gtag('config', 'G-8N9MBTQKCK');
13+
</script>`;
14+
15+
function appendGa(path) {
16+
let html = fs.readFileSync(path, 'utf8');
17+
if (html.includes(SCRIPT)) {
18+
console.log(`👌 ${path} already has tracking`);
19+
return;
20+
}
21+
const pos = html.lastIndexOf('</body>');
22+
if (pos < 0) {
23+
throw new Error('Could not find </body> tag');
24+
}
25+
html = html.substring(0, pos) + SCRIPT + html.substring(pos);
26+
fs.writeFileSync(path, html, 'utf8');
27+
console.log(`✅ ${path} updated`);
28+
}
29+
30+
const directory = process.argv[2];
31+
if (!directory) {
32+
throw new Error('Please specify a directory');
33+
}
34+
35+
const dirPath = path.join(__dirname, '..', directory);
36+
fs.readdir(dirPath, (_, files) => {
37+
files.forEach(file => {
38+
if (file.endsWith('.html')) {
39+
const filePath = path.join(dirPath, file);
40+
appendGa(filePath);
41+
}
42+
});
43+
});

0 commit comments

Comments
 (0)