Skip to content

A simple Hexo plugin to inject Plausible Analytics script in your pages.

License

Notifications You must be signed in to change notification settings

maaaaarco/hexo-plausible-analytics

Repository files navigation

hexo-plausible-analytics

A simple Hexo plugin to inject Plausible Analytics script in your pages.

General

Compatibility: Hexo 3 and above

Features

  • Extremely simple configuration
  • Supports custom domain
  • Supports exclude directive
  • Supports self-hosting
  • Inject the script only on specific page types (page, post, draft, archive, ...)

Install

In the root directory of your project run:

$ npm install hexo-plausible-analytics

Configurations

Enable the plugin

To enable the plugin add this to your config.yml:

plausible:
  enable: true
  domain: YOUR_DOMAIN

domain is the domain name of your site and is the only required property. Be sure to remove https and www from the URL. If your site is https://www.yourdomain.com then your configuration should look like this:

plausible:
  enable: true
  domain: yourdomain.com

Will result in:

<script async defer data-domain="yourdomain.com" src="https://plausible.io/js/plausible.js"></script>

Specify page types

The pages property is an array that allows you to specify on which page types to inject the script. Valid values are:

  • default: Inject to every page. This is the default configuration.
  • home: Only inject to home page (which has is_home() helper being true)
  • post: Only inject to post pages (which has is_post() helper being true)
  • page: Only inject to pages (which has is_page() helper being true)
  • archive: Only inject to archive pages (which has is_archive() helper being true)
  • category: Only inject to category pages (which has is_category() helper being true)
  • tag: Only inject to tag pages (which has is_tag() helper being true)

The following configuration will inject the scripts only on the home page and all posts:

plausible:
  enable: true
  domain: yourdomain.com
  pages:
    - home
    - post

Serve as first-party connection

Add the subdomain property and specify your subdomain if you want to serve the Plausible script as a first-party connection.

plausible:
  enable: true
  domain: yourdomain.com
  subdomain: stats

Will result in:

<script async defer data-domain="yourdomain.com" src="https://stats.yourdomain.com/js/index.js"></script>

Exclude pages from being tracked

Specify in the exclude property the pages that you don't want to be tracked. You can enter them as a comma-separated string or as an array. Check the official guide on how to format them.

plausible:
  enable: true
  domain: yourdomain.com
  exclude: '/blog4, /rule/*, /how-to-*, /*/admin'

Or as an array:

plausible:
  enable: true
  domain: yourdomain.com
  exclude: 
    - /blog4
    - /rule/*
    - /how-to-*
    - /*/admin'

Both these configurations will result in:

<script async defer data-domain="yourdomain.com" src="https://plausible.io/js/plausible.exclusions.js" data-exclude="/blog4, /rule/*, /how-to-*, /*/admin"></script>

The exclude property also works if you configured a subdomain:

plausible:
  enable: true
  domain: yourdomain.com
  subdomain: stats
  exclude: '/blog4, /rule/*, /how-to-*, /*/admin'

Will result in:

<script async defer data-domain="yourdomain.com" src="https://stats.yourdomain.com/js/index.exclusions.js" data-exclude="/blog4, /rule/*, /how-to-*, /*/admin"></script>

Self-hosting

In case you are self hosting you can specify the src property:

plausible:
  enable: true
  domain: yourdomain.com
  src: https://yourdomain.com/plausible.js

Will result in:

<script async defer data-domain="yourdomain.com" src="https://yourdomain.com/plausible.js"></script>

In case set the exclude property:

plausible:
  enable: true
  domain: yourdomain.com
  src: https://yourdomain.com/plausible.js
  exclude: '/blog4, /rule/*, /how-to-*, /*/admin'

Will result in:

<script async defer data-domain="yourdomain.com" src="https://yourdomain.com/plausible.js" data-exclude="/blog4, /rule/*, /how-to-*, /*/admin"></script>

Contributing

I work on the develop branch and release from the main. If you want to contribute fork this repository and create a new branch from develop. Once you are done open a Pull Request to develop.

About

A simple Hexo plugin to inject Plausible Analytics script in your pages.

Resources

License

Stars

Watchers

Forks

Packages

No packages published