Skip to content

Adding analytics snippets only ot production build #8773

Discussion options

You must be logged in to vote

several options

  • Consider adding analysis code dynamically in js
if(import.meta.PRO){
 const script = document.createElement('script')
.....
}
  • Add scripts with project build capabilities.
// vite.config.js
import { createHtmlPlugin } from 'vite-plugin-html'

createHtmlPlugin({
        inject: {
          data: {
            injectScript: process.env.NODE_ENV === 'production'  ? '<script  src="/xx/xxx.js"></script>': '',
          },
        },
      }),
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <%- injectScript %>
</head>

<body>
  <div id="app"></div>
  
</body>

</html>

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@easypay-mrosmane
Comment options

Answer selected by easypay-mrosmane
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants