gitbook-plugin-analytics
v0.2.1
Published
GitBook plugin for analytics (e.g. Google Analytics) purpose
Downloads
27
Maintainers
Readme
GitBook Plugin Analytics
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
- 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