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

gatsby-plugin-google-analytics-data-reporting-api

v1.3.1

Published

Reporting API for Google Analytics 4 (GA4)

Downloads

855

Readme

gatsby-plugin-google-analytics-data-reporting-api

Description

This is Reporting API plugin to get your page views count from Google Analytics Data API (GA4). I used to use "https://www.gatsbyjs.com/plugins/gatsby-plugin-google-analytics-reporter" for my blog, but it's only support Universal Analytics v4. Since Universal Analytics would be deprecated soon, and I didn't found the replacement for GA4 yet, so I decide to create one.

How to install

  1. Make sure you already set up Google Analytics 4 and anything related properly;
  2. Enable Google Analytics Data API in your console cloud apis in "https://console.cloud.google.com/apis/library/analyticsdata.googleapis.com";
  3. Make sure your Service Account email has "Viewer" permissions. It's on Google Analytics page > Admin > Account Access Management > choose the Service Account Email > View user's Account details on Roles and Data Restrictions > Choose your GA 4 property > Set it as "Viewer";
  4. run npm i gatsby-plugin-google-analytics-data-reporting-api;
  5. add this to your gatsby-config:
plugins: [{
  //other plugins...

  resolve: `gatsby-plugin-google-analytics-data-reporting-api`,
  options: {
    serviceAccountEmail: process.env.ANALYTICS_EMAIL,
    privateKey: process.env.ANALYTICS_PRIVATE_KEY,
    property: process.env.ANALYTICS_GA4,
    startDate: `2020-01-01`,
    endDate: `yesterday`,
    limit: 100,
    metric: `screenPageViews`,
    desc: true
  }
}]

I recommend you to set the sensitive value above in environment variables, or use "(dot)env" in local for security.

Options

serviceAccountEmail

required. it's your service account email, like [email protected].

privateKey

required. it's your private key from Google cloud console. download the json, and copy and paste the "private_key" here. it's start with "-----BEGIN PRIVATE KEY-----".

property

required. it's your GA4 property id from Google Analytics Page.

startDate

required since v1.3.0. it's based on Google Analytics date value. Could be '30daysAgo', 'today', 'yesterday', or ISO date format (yyyy-MM-dd) like '2022-12-31'. Default value '365daysAgo'. Google changed its API and for version < v1.3.0 using startDate default value will throw error. Since v1.3.0 I encourage to specify your startDate in config or use dynamic value like '365daysAgo'.

endDate

required since v1.3.0. it's based on Google Analytics date value. Could be '30daysAgo', 'today', 'yesterday', or ISO date format (yyyy-MM-dd) like '2022-12-31'. Default value 'today'. Since v1.3.0 I encourage to specify your endDate.

metric

optional. Metric calculation for reporting. Default value is 'screenPageViews' to calculate metric of the number of app screens or web pages your users viewed. Repeated views of a single page or screen are counted. Since v1.1.0 we can use custom metric like 'totalUsers' to calculate based on distinct users, or 'scrolledUsers' to calculate based on total users who scrolled your pages to at least 90% of page. Visit "https://developers.google.com/analytics/devguides/reporting/data/v1/api-schema#metrics" for more info about metric values can be used.

limit

optional. you can skip this option. it's to limit data fetch from the API. Default value is null which means no limit.

desc

optional. you can skip this option. it's boolean value to determine you want to order the result by ascending or descending. Default value is true.

Examples of usage

query the page views like this:

query AnalyticsPageQuery {
    allPageViews(sort: { totalCount: DESC }) {
        nodes {
            path
            totalCount
        }
    }
}

the response would be this:

{
  "data": {
    "allPageViews": {
      "nodes": [
        {
          "path": "/blog/first-blog",
          "totalCount": 190
        },
        {
          "path": "/blog/second-blog",
          "totalCount": 55
        }
      ]
    }
  }
}

How to contribute

any ideas or recommendations are welcome.