npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nuxt-pageviews

v1.0.17

Published

Nuxt Page View Counts sourced from Google Analytics

Downloads

36

Readme

Nuxt Page Views Module

npm version npm downloads License Nuxt

This is a module for Nuxt that provides page views tracking using Google Analytics Data API.

Built with Nuxt/Vue 3, TypeScript, and H3.

Features

  • Quick setup if you're already running Google Analytics
  • SSR Support
  • Cached responses from the Google Analytics Data API
  • Expose usePageViews() composable

Installation

  1. Add nuxt-pageviews dependency to your project
# Using pnpm
pnpm add -D nuxt-pageviews

# Using yarn
yarn add --dev nuxt-pageviews

# Using npm
npm install --save-dev nuxt-pageviews
  1. Add nuxt-pageviews to the modules section of nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    'nuxt-pageviews'
  ]
})

Configuration

Google Analytics Setup

You will need to configure a new service account, and add its email address to the GA property you would like to access.

Here are the steps to set up credentials for use with @google-analytics/data:

  1. Go to the Google Cloud Console.
  2. Create a new project or select an existing project.
  3. Enable the Google Analytics API for your project:
  4. Go to the APIs & Services dashboard, click on "Enable APIs and Services", and search for "Google Analytics API". Then click "Enable".
  5. Create credentials for your project:
  6. Go to the "Credentials" page in the APIs & Services dashboard and click "Create credentials". Select "Service Account" as the credential type and enter the required information.
  7. Make sure to add the appropriate scopes for the API you're using (in this case, https://www.googleapis.com/auth/analytics.readonly).
  8. Once you've created the service account, download the JSON key file for the service account. This file will contain the private key that you'll need to authenticate with the API, as well as the service account "email" address you will need to add as a user to your Analytics property.

Nuxt Configuration

To use this plugin, you need to provide a Google service account credentials file, a Google Analytics property ID, and an endpoint for the API that will serve the data.

In your nuxt.config.ts file, configure the options for the module:

export default defineNuxtConfig({
  pageViews: {
    credentialsFile: "./src/creds.json",
    // OR
    credentials: {/* contents of credentials file */},
    propertyId: "12345678",
    endpoint: "/api/views",
    preload: true, // preload analytics data on startup (default: true)
    exact: false, // if exact is false, the module will merge urls that are the same when the trailing slash is removed
    startDate: "2021-01-01", // Get data starting at this date
    cacheTimeout: 15 * 60, // Cache half-life (in seconds)
    debug: false // Debug mode, will show timings and refreshes
  }
})

Using

You can use the usePageViews composable to access the page views count for a specific page:

<script lang="ts" setup>
const views = await usePageViews()
</script>

<template>
  <div>
    <div>Blog views: {{ views }}</div>
  </div>
</template>

You can also pull the counts for other paths:


<template>
  <div>
    <div>Page views: {{ views }}</div>
    <div>Blog views: {{ blogViews }}</div>
    <div>Project List views: {{ projectViews }}</div>
  </div>
</template>

<script setup>
const views = await usePageViews()
const blogViews = await usePageViews("/blog")
const projectViews = await usePageViews("/projects")
</script>

Development

# Install dependencies
npm install

# Generate type stubs
npm run dev:prepare

# Develop with the playground
npm run dev

# Build the playground
npm run dev:build

# Run ESLint
npm run lint

# Run Vitest
npm run test
npm run test:watch

# Release new version
npm run release

✨ Change Log

License

This plugin is licensed under the MIT License. See the LICENSE file for more information.