-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from liam-hq/feat/gtm-tag
📈 Add Google Tag Manager scripts to update_dist_content.mjs
- Loading branch information
Showing
1 changed file
with
31 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>.') | ||
}) | ||
}) |