Git Product home page Git Product logo

l422y / nuxt-analytics-data Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 236 KB

SSR-enabled Nuxt module which allows running of reports via the Google Analytics Data API, and includes pre-defined reports and composables for trending and popular posts, as well as an AnalyticeWidget component for trending and popular posts.

Vue 16.56% TypeScript 83.44%
nuxt vue typescript ga4 google-analytics-api google-analytics-data popular trending

nuxt-analytics-data's Introduction

Nuxt Analytics Data Module

npm version npm downloads License Nuxt

This SSR-enabled Nuxt module allows running of reports via the Google Analytics Data API, and includes pre-defined reports and composables for trending and popular posts, as well as an AnalyticsWidget component for trending and popular posts.

API responses are cached on the server side and expire

Features

  • Filtering of paths (exact strings or RegEx) from report data, filtering parameters are supplied to the API
  • String removal (exact string or RegEx)
  • Trending and Popular reports, composables and a AnalyticeWidget component.

TODO:

  • Ability to define your own reports: this is partially implemented, need to figure out the best way to pass functions to the module from the configuration.

Quick Setup

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

# Using yarn
yarn add --dev nuxt-analytics-data

# Using npm
npm install --save-dev nuxt-analytics-data
  1. Add nuxt-analytics-data to the modules section of nuxt.config.ts
export default defineNuxtConfig({
  modules: [
    'nuxt-analytics-data'
  ]
})
  1. Add configuration for the module:
export default defineNuxtConfig({
  // ...
  analyticsData: {
    // Specify the path to your GA credentials file or use `credentials` with the data itself
    credentialsFile: "./playground/src/google-service-account.json",
    // The GA4 property ID you are accessing
    propertyId: "331054024",
    // Clear API response cache after 15 minutes (value is in seconds)
    cacheTimeout: 15 * 60,
    // Use `filteredPaths` to exclude paths from your query, i.e. if you
    // don't want the homepage or contact page.
    filteredPaths: {
      // Exact path filters
      exact: ["/references"],
      // RegEx Path filters -- ex: filter any first-level paths (i.e. "/blog" "/projects")
      regEx: [`^/(\\w+)?\\/?$`]
    },

    // removeStrings will remove any matching text from the pageTitles in results
    removeStrings: {
      // Exact matches, ex: removing trailing tags from the title
      exact: [` - Larry Williamson`],
      // and/or RegEx matches
      regEx: [` - .* - Larry Williamson`]
    }
    // ...
  }
});

That's it! You can now use Nuxt Analytics Data Module in your Nuxt app โœจ

Usage

$analyticsData

The retrieved data is stored in useState('analyticsData') as well as provided in the Nuxt application as $analyticsData

const {$analyticsData} = useNuxtApp()

const analyticsData = useState("analyticsData")

Data is formatted like this:

{
  "popular": {
    "/blog": {
      "pageTitle": "Blog",
      "screenPageViews": "129"
    },
    "/blog/countdown-for-vue-with-composables": {
      "pageTitle": "Building a Countdown Component for Vue with Composables",
      "screenPageViews": "96"
    }
  },
  "trending": {
    "/": {
      "pageTitle": "Home",
      "screenPageViews": "999999"
    }
  }
}

usePopular and useTrending composables

Provides the specific data from the analyticsData object

const allPopular = await usePopular()
const allTrending = await useTrending()

usePagePopular and usePageTrending composables

Returns a boolean indicating whether a page is within popular or trending page lists. If no argument is supplied it uses the current route path.

<template>
  <nav>
    <NuxtLink :to="/blog">Blog<span v-if="trendingBlog"> ๐Ÿ”ฅ</span></NuxtLink>
  </nav>
</template>
<script lang="ts" setup>
  const popular = await usePagePopular()
  const trending = await usePageTrending()

  const popularBlog = await usePagePopular("/blog")
  const trendingBlog = await usePageTrending("/blog")
</script>

Then

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

nuxt-analytics-data's People

Contributors

l422y avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.