In fact, you can also use variables from DefinePlugin in your ejs template using HtmlWebpackPlugin .
EJS:
<script async src="https://www.googletagmanager.com/gtag/js?id=<%= GA_TRACKING_ID %>"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', '<%= GA_TRACKING_ID %>'); </script>
webpack.config.js:
new webpack.DefinePlugin({ 'GA_TRACKING_ID': JSON.stringify(process.env.GA_TRACKING_ID), }),
source share