Skip to content

Set up Google Analytics with Vitepress

Code Snippet

Change all G-XXXXXXXXXX with the Tracking ID provided by Google Analytics (GA4).

// src/.vitepress
module.exports = {
  head: [
        async: true,
        src: '',
      "window.dataLayer = window.dataLayer || [];\nfunction gtag(){dataLayer.push(arguments);}\ngtag('js', new Date());\ngtag('config', 'G-XXXXXXXXXX');",



If you're coming from Vuepress, you may be used to the plugin system that provides features such as Google Analytics. Vitepress is (arguably) simpler in this regard. Just add the script provided by Google to your HTML head, and it should work.