Nuxt Page Views Personal Project
Nuxt Page Views is a Nuxt module that provides page views tracking using the Google Analytics Data API. It allows you to display the number of page views for any page on your Nuxt site, using a simple and elegant composable function. Nuxt Page Views is built with Nuxt/Vue 3, TypeScript, and H3.
Features
- Quick setup if you're already running Google Analytics on your site
- SSR Support: Fetch and render the page views on the server side
- Cached responses from the Google Analytics Data API: Reduce the number of requests and improve the performance
- Expose
usePageViews()
composable: Easily access the page views count for any page or path
How It Works
The module initializes by loading the Google service account credentials file, the Google Analytics property ID, and the
endpoint for the API that will serve the data from the module options. The module then registers a global composable
function called usePageViews()
, which takes an optional path parameter and returns the page views count for that path.
If no path is provided, the composable function uses the current route path. The module also creates an API route on the
endpoint specified in the module options, which handles the requests to the Google Analytics Data API and caches the
responses.
License
This module is licensed under the MIT License. See the LICENSE file for more information.