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

gitbook-plugin-analytics

v0.2.1

Published

GitBook plugin for analytics (e.g. Google Analytics) purpose

Downloads

30

Readme

GitBook Plugin Analytics

NPM

Table Of Contents

Links

Get Started

Firstly, edit book.json which should be located at the root dir of a gitbook. If it does not exist, create it!

// book.json
{
	// ...

	"plugins": [
		// ...,
		"analytics"
	],

	"pluginsConfig": {
		// ...,
		"analytics": {
			"google": "TOKEN-1",
			"baidu": "TOKEN-2"
		}
	}
}

Secondly, run the next commands to active the plugin:

# Change dir to the gitbook's root dir.

# Install the plugins.
gitbook install

# Build the the book,
gitbook build
# OR, serve it.
gitbook serve

Advanced Configs

As the previous section described, it's so easy to active Google Analytics or Baidu Tongji by adding responding tokens in pluginsConfig.

However, sometimes the _book folder generated by gitbook may be copied and released to locations other than gitbook.com. So, THE PLUGIN allows to be enabled in some situations and disabled in others. And, it also allows you to use different analytics for different situations.

Example 1, Different Analytics In Different Contexts

A context is defined by a base URL:

{
	"pluginsConfig": {
		// ...,
		"analytics": {
			/* Here "context" is a fixed property name, NOT name of vendor. */
			"context": [
				// ...,
				{
					"vendor": "google",
					"token": "TOKEN-1",
					"base": "http://home.example.com/mybook/"
				},
				{
					"vendor": "google",
					"token": "TOKEN-2",
					"base": "http://mybook.example.com:8080/"
				}
			]
		}
	}
}

The base property may be in one of next forms:

  • full base url, e.g.
    http://home.example.com/mybook/

  • base url without protocol, e.g.
    //home.example.com/mybook/

  • only protocol, which should be one of the following:

    • http
    • https
  • only hostname, e.g.
    home.example.com

  • only port, e.g.
    8080

  • only host (hostname and port), e.g.
    mybook.example.com:8080

  • only base path (must begin with a slash "/"), e.g.
    /mybook/

The base property may also be an array, e.g.

{
	"pluginsConfig": {
		// ...,
		"analytics": {
			/* Here "context" is a fixed property name, NOT name of vendor. */
			"context": [
				// ...,
				{
					"vendor": "google",
					"token": "TOKEN-1",
					"base": [
						"http://home.example.com/mybook/",
						"http://www.example.com/mybook/"
					]
				},
				{
					"vendor": "google",
					"token": "TOKEN-2",
					"base": "http://mybook.example.com:8080/"
				}
			]
		}
	}
}

Example 2, Split The Context Into Parts

{
	"pluginsConfig": {
		// ...,
		"analytics": {
			/* Here "context" is a fixed property name, NOT name of vendor. */
			"context": [
				// ...,
				{
					"vendor": "google",
					"token": "TOKEN-1",
					"hostname": "home.example.com",
					"path": "/mybook/"
				},
				{
					"vendor": "google",
					"token": "TOKEN-2",
					"hostname": "mybook.example.com",
					"port": "8080"
				}
			]
		}
	}
}

Actually, we may define the context with next details:

  • protocol
  • hostname
  • host
  • port
  • path

The bundled analytics will only be enabled when all details specified matching. Each context property may be a string or an array, e.g.

{
	"pluginsConfig": {
		// ...,
		"analytics": {
			/* Here "context" is a fixed property name, NOT name of vendor. */
			"context": [
				// ...,
				{
					"vendor": "google",
					"token": "TOKEN-1",
					"hostname": [ "home.example.com", "www.example.com" ],
					"path": "/mybook/"
				},
				{
					"vendor": "google",
					"token": "TOKEN-2",
					"hostname": "mybook.example.com",
					"port": [ "8080", "8088" ]
				}
			]
		}
	}
}

Example 3, Simplify The Config, Extracting Bases

The base values may be extracted to be the keyname of analytics object, as long as it DOES NOT EQUAL TO:

  • vendor codes, including
    • baidu
    • google
  • or other preserved keywords, including
    • context
    • contexts
    • vendor
    • vendors
{
	"pluginsConfig": {
		// ...,
		"analytics": {
			// ...,
			"//home.example.com/mybook/": {
				"vendor": "google",
				"token": "TOKEN-1"
			},
			"mybook.example.com:8080": {
				"vendor": "google",
				"token": "TOKEN-2"
			}
		}
	}
}

In these cases, vendor and token can also be expressed with an array. e.g.

{
	"pluginsConfig": {
		// ...,
		"analytics": {
			// ...,
			"//home.example.com/mybook": [ "google", "TOKEN-1" ],
			"mybook.example.com": [ "google", "TOKEN-2" ] /* vendor first, and token follows */
		}
	}
}

Example 4, Simplify The Config, Predefining Vendors

{
	"pluginsConfig": {
		// ...,
		"analytics": {
			// ...,
			/* "vendors" and "vendor" are preserved keywords. */
			"vendors": {
				/* This keyname is user defined. */
				/* The value is an object made up of "vendor" and "token". */
				"MY_ANALITICS_NAME_1": { "vendor": "google", "token": "TOKEN-1" },

				/* The value may also be an array
				   vendor code first and token follows. */
				"MY_ANALITICS_NAME_2": [ "google", "TOKEN-2" ],

				/* The value may also be a group of analytics. */
				"MY_ANALITICS_NAME_3": [
					[ "google", "TOKEN-3" ],
					[ "baidu", "TOKEN-4" ]
				]
			},
			/* Apply analytics to specified base. */
			"//home.example.com/mybook/": "MY_ANALITICS_NAME_1",
			"mybook.example.com:8080": "MY_ANALITICS_NAME_2",
			"mybook.example.com": "MY_ANALITICS_NAME_3"
		}
	}
}

Before We Start

ATTENTION: Gitbook project config file book.json is JSON format, in which no comment is allowed. So, the C-style comments in example configs should be deleted in real config file.

THE PLUGIN is compitable with gitbook >= 3.x.x. If the gitbook engine you installed is an older one, THE PLUGIN does not work, and the next message will be printed in console when you build or serve your book:

# ...
info: load plugin gitbook-plugin-analytics ....ERROR
# ...
Error: Error loading plugins: gitbook-plugin-analytics. Run 'gitbook install' to install plugins from NPM.

Stop trying to run gitbook install again and again, it's vain.

DO NOT know what is the current version of gitbook? Run the next command to see:

# List installed versions of gitbook engine.
gitbook verions
# One or more versions will be listed row by row.
# The one prefixed with * is the currrent version.

# Force install specific version of gitbook engine if necessary.
gitbook versions:install 3.2.2