Skip to content

How to add analytics scripts only in production? #3955

Closed Answered by khanetor
khanetor asked this question in Q&A
Discussion options

You must be logged in to vote

I finally settled on this approach

// .vitepress/config.mts

const umamiScript: HeadConfig = ["script", {
  defer: "true",
  src: "https://.../script.js",
  "data-website-id": "..."
}]

const baseHeaders: HeadConfig[] = []

const headers = process.env.NODE_ENV === "production" ?
  [...baseHeaders, umamiScript] :
  baseHeaders

export default defineConfig({
  ...,
  head: headers
})

And then set this to the head in config.

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@khanetor
Comment options

@khanetor
Comment options

@khanetor
Comment options

Answer selected by khanetor
@khanetor
Comment options

@brc-dd
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants