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

@princevish/google-apis

v1.0.3

Published

@princevish/google-apis is a Node.js client library that simplifies accessing the latest Google APIs, including Google Analytics and Google Search Console. It provides easy-to-use methods to interact with these services using URL-based queries. The librar

Downloads

246

Readme

Google APIs Node.js Client

Overview

@princevish/google-apis is a Node.js client library that simplifies accessing the latest Google APIs, including Google Analytics and Google Search Console. It provides easy-to-use methods to interact with these services using URL-based queries. The library also includes built-in support for OAuth 2.0 authentication, API keys, and JWT tokens, ensuring secure and efficient interaction with Google APIs.

Key Features:

  • Latest Google API Support: Works with the latest versions of Google APIs.
  • Easy-to-use functions for Google APIs like Analytics and Search Console.
  • OAuth 2.0 integration for secure authentication.
  • Automatic token refresh management.
  • Simple API query building and request handling using axios.

Installation

npm install @princevish/google-apis

OAuth2

This library comes with an OAuth2 client that allows you to retrieve an access token and refreshes the token and retry the request seamlessly if you also provide an expiry_date and the token is expired. The basics of Google's OAuth2 implementation is explained on Google Authorization and Authentication documentation.

In the following examples, you may need a CLIENT_ID, CLIENT_SECRET and REDIRECT_URL. You can find these pieces of information by going to the Developer Console, clicking your project > APIs & auth > credentials.

For more information about OAuth2 and how it works, see here.

A complete OAuth2 example

Let's take a look at a complete example.

Usage

Example: OAuth 2.0 Setup

import { ClientGoogleApi } from "@princevish/google-apis";
import express from "express";

const app = express();
const client = new ClientGoogleApi({
  client_id: process.env.CLIENT_ID,
  client_secret: process.env.CLIENT_SECRET,
  redirect_uris: process.env.REDIRECT_URIS,
});

// Redirect users to the OAuth2 authorization URL
app.get("/", async (req, res) => {
  const url = await client.getVerificationUrl();
  res.redirect(url);
});

// Handle OAuth2 callback
app.get("/callback", async (req, res) => {
  const code = req.query.code;
  const token = await client.getAccessToken(code);
  res.send(token);
});

// Fetch Google Analytics data
app.get("/analytics", async (req, res) => {
  const data = await client.getAnalyticsData({
    propertyId: "PROPERTY_ID",
    query: {
      startDate: "2024-08-01",
      endDate: "2024-10-01",
      metrics: ["sessions"],
      dimensions: ["country"],
    },
  });
  res.send(data);
});

// Fetch Google Search Console data
app.get("/search-console", async (req, res) => {
  const data = await client.getSearchConsoleData({
    siteUrl: process.env.SITE_URL,
    query: {
      startDate: "2024-08-01",
      endDate: "2024-10-01",
      dimensions: ["query", "page"],
    },
  });
  res.send(data);
});

app.listen(8080, () => {
  console.log("Listening on port 8080");
});

API Methods

1. getVerificationUrl()

Generates a URL for the user to visit and authenticate their Google account.

const url = client.getVerificationUrl();

2. getAccessToken(code)

Exchanges an authorization code for an access token.

const token = await client.getAccessToken("AUTH_CODE_OR_REDIRECT_URL");

3. getAnalyticsData({ propertyId, query })

Fetches Google Analytics data.

const analyticsData = await client.getAnalyticsData({
  propertyId: "PROPERTY_ID",
  query: {
    startDate: "2024-08-01",
    endDate: "2024-10-01",
    metrics: ["sessions"],
    dimensions: ["country"],
  },
});

4. getSearchConsoleData({ siteUrl, query })

Fetches data from Google Search Console for a given site URL.

const searchConsoleData = await client.getSearchConsoleData({
  siteUrl: "https://example.com",
  query: {
    startDate: "2024-08-01",
    endDate: "2024-10-01",
    dimensions: ["query", "page"],
  },
});

Authentication Methods

OAuth 2.0: Supports user consent flows and token refreshing. Ideal for accessing data on behalf of users.

Examples

Detailed examples are available in the examples folder of the repository.

License

This project is licensed under the MIT License.


Feel free to explore the examples and extend this library to other Google APIs!


Distributed under the MIT License. See LICENSE for more information.

Happy Coding! 🚀

Made with ❤️ by @princevish