You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The next major will provide an easier way to inject html tags to the head or body.
For now you can already use the htmlWebpackPluginAlterAssetTags hook.
For now I wrote down a solution quickly without testing - so it might not work properly.
But hopefully you still get the idea and will be able to finalize it:
This could look like the following (in a new file or in your webpack config)
classGoogleTagManagerHtmlPlugin{apply(compiler){// Hook into a webpack compilationcompiler.hooks.compilation.tap('GoogleTagManagerHtmlPlugin',(compilation)=>{// Hook into `htmlWebpackPluginAlterAssetTags`// !! Careful this will change in the upcoming html webpack plugin version !!compilation.hooks.htmlWebpackPluginAlterAssetTags.tapAsync('GoogleTagManagerHtmlPlugin',(pluginArgs,cb)=>{// You can use either `head` or `body` and either `push` or `unshift`:pluginArgs.head.push({tagName: 'script',voidTag: false,attributes: {},innerHTML: 'alert("hello world")'});cb(null,pluginArgs)})})}}
The plugin section of your config
[newHtmlWebpackPlugin({/* your config */}),newGoogleTagManagerHtmlPlugin()}
Description
I would like to add custom script at different parts of
<head>
and<body>
tags.Use case: trying to add GTM tags to different pages.
Example:
How do I,
<script> alert('in head tag') </script>
inside the<head>
tag as the first child<script> alert('in body tag') </script>
inside the<body>
tag as the first childHere is the stackoverflow link: https://stackoverflow.com/questions/51285711/how-do-i-add-certain-script-tags-inside-head-and-body-tags-when-using-htmlwe
Here is the snippet in my Webpack config
Config
The text was updated successfully, but these errors were encountered: