Skip to content

Commit

Permalink
Merge pull request #296 from liam-hq/feat/gtm-tag
Browse files Browse the repository at this point in the history
📈 Add Google Tag Manager scripts to update_dist_content.mjs
  • Loading branch information
MH4GF authored Dec 18, 2024
2 parents 12d215f + c81afb8 commit 7202b0a
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions frontend/apps/erd-sample/scripts/update_dist_content.mjs
Original file line number Diff line number Diff line change
@@ -1,43 +1,55 @@
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'

// Get the directory name of the current module
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)

// Path to the target HTML file
const filePath = path.join(__dirname, '..', 'dist', 'index.html');
const filePath = path.join(__dirname, '..', 'dist', 'index.html')

// Content to insert into <head>
const contentToInsertHead = `
<!-- example head -->
`;
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-T4N2TJXW');</script>
<!-- End Google Tag Manager -->
`

// Content to insert into <body>
const contentToInsertBody = `
<!-- example body -->
`;
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-T4N2TJXW"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
`

// Read the file
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Failed to read file:', err);
return;
console.error('Failed to read file:', err)
return
}

// Insert content immediately after the <head> tag
let updatedData = data.replace(/<head>/, `<head>\n ${contentToInsertHead}`);
let updatedData = data.replace(/<head>/, `<head>\n ${contentToInsertHead}`)

// Insert content immediately after the <body> tag
updatedData = updatedData.replace(/<body>/, `<body>\n ${contentToInsertBody}`);
updatedData = updatedData.replace(
/<body>/,
`<body>\n ${contentToInsertBody}`,
)

// Write the updated data back to the file
fs.writeFile(filePath, updatedData, 'utf8', (err) => {
if (err) {
console.error('Failed to write file:', err);
return;
console.error('Failed to write file:', err)
return
}
console.log('Content successfully inserted into <head> and <body>.');
});
});
console.log('Content successfully inserted into <head> and <body>.')
})
})

0 comments on commit 7202b0a

Please sign in to comment.