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

sanity-plugin-google-analytics

v0.2.6

Published

View data from Google Analytics in your Sanity.io studio

Downloads

127

Readme

Sanity Google Analytics Plugin

Analytics widget and components for showing Google Anlytics data in your studio.

sanity install google-analytics

How to config

You have to setup a google API, and all your studio users need to have access to the current Google Analytics View

Setup an API

  1. Open the API Library in the Google API Console. If prompted, select a project or create a new one.
  2. Find Google Analytics Reporting API and enable it
  3. Open the Credentials page in the API Console.
  4. Click Create credentials > OAuth client ID and select the appropriate Application type.

Detailed instructions for setup Google Analytics API

Your view id are available inside your Google Analytics. Go to adminView settings to find your view id.

Add config file

Add a google-analytics-plugin.json in your config folder.

{
  "clientId": "XXXXXXXXXX-xxxxxxxxxxxxxx.apps.googleusercontent.com",
  "views": "ga:xxxxxxxx"
}

Dashboard widgets

If you don't have dashboard, install it with sanity install @sanity/dashboard. Dashboard docs

Make a config file, and add the path yo your sanity.json

{
  "implements": "part:@sanity/dashboard/config",
  "path": "./myDashboardConfig.js"
}

Making queries

To make a query, try the Query explorer or find parameters in the Query reference

Example of Dashboard config

export default {
  widgets: [
    {
      name: 'google-analytics',
      layout: {
        width: 'large'
      },
      options: {
        title: 'Last 30 days',
        gaConfig: {
          reportType: 'ga',
          query: {
            dimensions: 'ga:date',
            metrics: 'ga:users, ga:sessions, ga:newUsers',
            'start-date': '30daysAgo',
            'end-date': 'yesterday'
          },
          chart: {
            type: 'LINE',
            options: {
              width: '100%',
            }
          }
        }
      }
    },
    {
      name: 'google-analytics',
      layout: {
        width: 'medium'
      },
      options: {
        title: 'World map',
        gaConfig: {
          reportType: 'ga',
          query: {
            dimensions: 'ga:country',
            metrics: 'ga:users',
            'start-date': '30daysAgo',
            'end-date': 'yesterday'
          },
          chart: {
            type: 'GEO',
            width: '100%'
          }
        }
      }
    }

Table of top bouncing pages

{
  name: 'google-analytics',
  layout: {
    width: 'medium'
  },
  options: {
    title: 'Top 10 bouncing blog posts',
    gaConfig: {
      reportType: 'ga',
      query: {
        dimensions: 'ga:pagePath',
        'max-results': 10,
        metrics: 'ga:bounceRate, ga:bounces, ga:pageViews',
        sort: '-ga:bounceRate',
        'start-date': '30daysAgo',
        'end-date': 'yesterday',
        filters: 'ga:pagePath=~^/blog;ga:bounces>50'
      },
      chart: {
        type: 'TABLE',
        labels: {
          0: 'Page path',
          1: 'Bounce rate',
          2: 'Bounces',
          3: 'Page views'
        },
        options: {
          width: '100%',
        }
      }
    }
  }
}

Make your own component

withAnalyticsData

By wrapping your component in withAnalyticsData a data-prop will be available in your plugin.

import withAnalyticsData from "part:@sanity/google-analytics/withAnalyticsData"

class MyComponent extends React.Component {
  render() {
    const {data} = this.props
    return (
      <pre>{JSON.stringify(data)}</pre>
    )
  }
}

export default withAnalyticsData(MyComponent)

When you use your component, you can specify what data you want.

<MyComponent config={{
  reportType: 'ga',
  query: {
    dimensions: 'ga:date',
    metrics: 'ga:users, ga:sessions, ga:newUsers',
    'start-date': '30daysAgo',
    'end-date': 'yesterday'
  }
}} />

Use the analytics widget in your own components

import AnalyticsWidget from 'part:@sanity/google-analytics/widget'
import sanityClient from 'part:@sanity/base/client'
<AnalyticsWidget
  config={{
    onSelect: (selectedItem, cell, chart, router) => {
      // Do something with the selected data
      console.log('select', selectedItem, cell, chart, router)
      // Example of finding the id based on slug and navigate to it
      sanityClient.fetch(`*[_type == 'post' && slug.current == $path][0]`, {path}).then(res => {
        router.navigateIntent('edit', {
          type: 'post',
          id: res._id
        })
      })
    },
    reportType: 'ga',
    query: {
      dimensions: 'ga:date',
      metrics: 'ga:users, ga:sessions',
      'start-date': '30daysAgo',
      'end-date': 'yesterday'
    },
    chart: {
      axes: {x: { 0: { label: 'Date' }}},
      type: 'LINE',
      series: {
        0: {title: 'Users', color: '#145eda'},
        1: {title: 'Sessions', color: '#16ae3c'}
      }
    }
}} />

Example of a table with top bouncing blog posts and navigate to them on click

{
    name: 'google-analytics',
    layout: {
      width: 'medium'
    },
    options: {
      title: 'Top bouncing posts',
      gaConfig: {
        reportType: 'ga',
        onSelect: (selectedItem, cell, chart, router) => {
          try {
            // Find url
            const path = cell.c[0].v.split('/blog/')[1]
            // Find the ID
            sanityClient.fetch(`*[_type == 'post' && slug.current == $path][0]`, {path}).then(res => {
              // Navigate to post in sanity studio
              router.navigateIntent('edit', {
                type: 'post',
                id: res._id
              })
            })
            
          } catch {
            console.error('Could not find post')
          }
        },
        query: {
          dimensions: 'ga:pagePath',
          'max-results': 10,
          metrics: 'ga:bounceRate, ga:bounces, ga:pageViews',
          sort: '-ga:bounceRate',
          'start-date': '30daysAgo',
          'end-date': 'yesterday',
          filters: 'ga:pagePath=~^/blog;ga:bounces>50'
        },
        chart: {
          type: 'TABLE',
          options: {
            width: '100%',
          }
        }
      }
    }
  }