Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add Google Analytics Meta Tags Generating Function #4526

Open
4 tasks done
lumirlumir opened this issue Jan 27, 2025 · 0 comments
Open
4 tasks done

Feature: Add Google Analytics Meta Tags Generating Function #4526

lumirlumir opened this issue Jan 27, 2025 · 0 comments

Comments

@lumirlumir
Copy link

lumirlumir commented Jan 27, 2025

Is your feature request related to a problem? Please describe.

Currently, there is no straightforward way to add Google Analytics meta tags to the <head> section in VitePress projects. Manually adding these tags requires additional boilerplate code, which could be automated for better developer experience.

Describe the solution you'd like

A function like generateGoogleAnalyticsMeta that returns the necessary Google Analytics <script> tags to be included in the config.js head configuration. This function would make it easy to integrate Google Analytics by simply providing the googleAnalyticsID.

Example:

function generateGoogleAnalyticsMeta(googleAnalyticsID) {
  return [
    ['script', { async: '', src: `https://www.googletagmanager.com/gtag/js?id=${googleAnalyticsID}` }],
    [
      'script',
      {},
      `window.dataLayer = window.dataLayer || [];
      function gtag(){dataLayer.push(arguments);}
      gtag('js', new Date());
      gtag('config', '${googleAnalyticsID}');`,
    ],
  ];
}

We can use it like this using spread operator:

import { generateGoogleAnalyticsMeta } from 'vitepress';

head: [
  ...generateGoogleAnalyticsMeta(GOOGLE_ANALYTICS_ID);
]

Describe alternatives you've considered

Manually adding the Google Analytics meta tags in the config.js file as part of the head configuration. However, this approach is repetitive and error-prone, especially for projects with multiple environments or contributors.

Additional context

This feature would improve developer productivity by simplifying the integration of Google Analytics into VitePress projects. It aligns with the philosophy of VitePress being a lightweight and easy-to-use documentation framework.

Validations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant