Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
0c1246c
Added required files
z3nth10n Mar 20, 2019
fdcbe4d
Changed VERSION const
z3nth10n Mar 20, 2019
6a5f067
Updated && migrated code
z3nth10n Mar 20, 2019
24a6d27
Some tests
z3nth10n Mar 20, 2019
2b12e82
Impl queryString
z3nth10n Mar 21, 2019
556aeb8
Removed some lines
z3nth10n Mar 21, 2019
62baf53
Impl difference dates
z3nth10n Mar 21, 2019
b05aff9
Removed debug statments
z3nth10n Mar 21, 2019
922f714
Injecting transposed data
z3nth10n Mar 21, 2019
e8c4396
Calculating diff and perc
z3nth10n Mar 21, 2019
a84d8be
Injecting data into site
z3nth10n Mar 21, 2019
8f8fa8d
Removing code
z3nth10n Mar 21, 2019
86ced51
Renamed '@@response_data' to '@response_data'
z3nth10n Mar 21, 2019
52db732
Saving file
z3nth10n Mar 21, 2019
0c004f5
Loading data
z3nth10n Mar 21, 2019
ff056f4
Added site globals
z3nth10n Mar 21, 2019
f3d2603
Some fixes
z3nth10n Mar 21, 2019
40b0ee0
Pre-storing values
z3nth10n Mar 21, 2019
015bf2b
Documented last part
z3nth10n Mar 21, 2019
9350c33
Removed some lines
z3nth10n Mar 21, 2019
18950c8
More fixes
z3nth10n Mar 21, 2019
8e8823e
Updated readme
z3nth10n Mar 22, 2019
52c327b
Publishing gem && updating README again
z3nth10n Mar 22, 2019
72e4f53
Fixed little mispell
z3nth10n Mar 22, 2019
def7c3f
Removed line
z3nth10n Mar 22, 2019
1d29953
Updated line
z3nth10n Mar 22, 2019
a1b9e5a
Removed sort key
z3nth10n Mar 22, 2019
68829d2
Update LICENSE
z3nth10n Mar 30, 2019
f70ab93
Update LICENSE
z3nth10n Mar 30, 2019
a600ace
Updated README
z3nth10n Mar 31, 2019
553ed19
Updated plugin
z3nth10n Mar 31, 2019
064fe95
Raising error on illegal dates
z3nth10n Apr 2, 2019
946e88c
Updated dependencies
z3nth10n Nov 22, 2019
7f0c885
...
z3nth10n Nov 25, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.gem

/versions/
11 changes: 11 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
deploy:
provider: rubygems
gem: jekyll-ga-v2
on:
tags: true
all_branches: true
repo: uta-org/jekyll-ga-v2
language: ruby
rvm:
- 2.3.1
script: JEKYLL_ENV="production" bundle exec rspec spec
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true
source "https://rubygems.org"

# Specify your gem's dependencies in jekyll-patreon.gemspec
gemspec

# gem 'jekyll-patreon', :git => 'git://github.com/uta-org/jekyll-patreon.git'
675 changes: 675 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

254 changes: 254 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
# jekyll-ga-v2 [![Build Status](https://travis-ci.org/uta-org/jekyll-ga-v2.svg?branch=master)](https://travis-ci.org/uta-org/jekyll-ga-v2) [![Gem Version](https://badge.fury.io/rb/jekyll-ga-v2.svg)](http://badge.fury.io/rb/jekyll-ga-v2)

Requires Ruby 2.5+ and Jekyll 3.8+

> A Jekyll plugin that downloads Google Analytics data and adds it to your Jekyll website. The Google Analytics metric is added to each post/page's metadata and is accessible as `page.stats`. It can be printed in a template.

## Installation

This plugin requires three Ruby gems:

```bash
$ sudo gem install chronic
$ sudo gem install google-api-client
$ sudo gem install googleauth
```

Add this line to your site's Gemfile:

```ruby
gem 'jekyll-ga-v2'
```

### Set up a service account for the Google data API

- Go to https://code.google.com/apis/console/b/0/ and create a new project.
- Turn on the Analytics API and accept the terms of service
- Go to `API Access` on the left sidebar menu, create a new oauth 2.0 client ID, give your project a name, and click `next`.
- Select Application type: `Service account`, and click `Create client ID`
- note the private key's password. It will probably be `notasecret` unless Google changes something. You'll need to use this value to decrypt the PCKS12 file (later explanined).
- Download the private key. Save this file because you can only download it once.
- Note the `Email address` for the Service account. You'll need this for your configuration settings and in the next step.
- Log into Google Analytics and add the service account email address as a user of your Google Analytics profile: From a report page, `Admin > select a profile > Users > New User`

#### Configuration of the environment variables

[GoogleAuth needs the following environment variables to work.](https://github.com/googleapis/google-auth-library-ruby#example-environment-variables)

There is an easy way to implement this using CircleCI (maybe you are using similar to deploy your Jekyll website). If you're not familiar with CircleCI you'll need to read carefully this post on my blog about "[How To Use Any Jekyll Plugins on GitHub Pages with CircleCI](https://z3nth10n.github.io/en/2019/03/20/jekyll-plugin-issue-with-github-pages)".

Once you implement it, you'll need to go to your [CircleCI dashboard](https://circleci.com/dashboard) search your project settings and go under "**Organization > Contexts**" and create [a new Context](https://circleci.com/docs/2.0/contexts/).

Look at my website [CircleCI.yml configuration here](https://github.com/z3nth10n/z3nth10n.github.io/blob/b9f7ef42e5fce33800aab80f8eabe6868b38f8e5/circle.yml#L54). The only thing remaining is to create the appropiate Context name, and then, create the required env vars:

![](https://i.gyazo.com/3ad97b8e09ee7e05b8496f1cd631affa.png)

**Note:** The `GOOGLE_PRIVATE_KEY` value is the output from OpenSSL. You'll need to execute the following command to get it from the `*.p12` file:

```bash
$ openssl pkcs12 -in filename.p12 -clcerts -nodes -nocerts
```

You'll need to replace all the new lines characters by `\n`. This can be easily done with Sublime Text 3 specifying the Regex options and the replacing `\n` by `\\n`.

## Configuration

To configure `jekyll-ga-v2`, you need to specify some information about your Google Analytics service account (as set up above) and your report settings.

Add the following block to your Jekyll site's `_config.yml` file:

```yaml
####################
# Google Analytics #
####################

jekyll_ga:
profileID: ga:<user_id> # Profile ID
start: last week # Beginning of report
end: now # End of report
compare_period: true
metrics: ga:pageviews # Metrics code
dimensions: ga:pagePath # Dimensions
segment: # Optional
filters: # Optional
max_results: 10000 # Number of the maximum results get by the API
debug: false # Debug mode
```

* `profileID` is the specific report profile from which you want to pull data. Find it by going to the report page in Google Analytics. Look at the URL. It will look something like `https://www.google.com/analytics/web/?hl=en&pli=1#report/visitors-overview/###########p######/`. The number after the `p` at the end of the URL is your `profileID`.
* The `start` and `end` indicate the time range of data you want to query. They are parsed using Ruby's `Chronic` gem, so you can include relative or absolute dates, such as `now`, `yesterday`, `last month`, `2 weeks ago`. See [Chronic's documentation](https://github.com/mojombo/chronic#examples) for more options.
* The `metrics` value is what you want to measure from your Google Analytics data. Usually this will be `ga:pageviews` or `ga:visits`, but it can be any metric available in Google Analytics. Specify only one. See the [Google Analytics Query Explorer](http://ga-dev-tools.appspot.com/explorer/?csw=1) to experiment with different metrics. (Your `dimension` should always be `ga:pagePath`). I recommend you the following string `ga:pageviews,ga:bounceRate,ga:sessions,ga:users,ga:newUsers`.
* The `segment` and `filters` keys are optional parameters for your query. See the [Google Analytics Query Explorer](http://ga-dev-tools.appspot.com/explorer/?csw=1) for a description of how to use them, or just leave them out.

New params in v2:

* If `compare_period` is to true, then this will create two reports (**example:** if start is set to "last month", this will create one report from "end" to "start" and the second report its end will be at the start of the first report, with this data a comparation will be created).

### Do you need to automatize this?

Maybe you're thinking that you'll need to make a new push everytime you need to update your stats. And you're right, but CircleCI comes here again for the rescue. All you need is to [schedule a nightly build](https://circleci.com/docs/2.0/workflows/#nightly-example).

Here is my own implementation on [my CircleCI.yml configuration, again](https://github.com/z3nth10n/z3nth10n.github.io/blob/b9f7ef42e5fce33800aab80f8eabe6868b38f8e5/circle.yml#L56).

```yaml
nightly:
triggers:
- schedule:
cron: "0 0 * * *"
filters:
branches:
only:
- gh-pages-ci
ignore:
- master
jobs:
- build:
context: "Google Analytics Sensitive Data"
```

Of course, you'll need to specify the context again.

### Need help for examples?

Look at those two HTML files I created to render my settings:

```html
<div id="genstats" class="col-md-3 align-sm-right vertical-margin order-xs-fourth col-xs-expand">
<box class="both-offset expand-width">
<p>
<h3>{% t 'stats-caption' %}</h3>
<p>({% t 'stats_last' %} {{ site.data.period }} {% t 'stats_days' %})</p>
</p>

{% for header in site.data.headers %}

<p>
{% assign hvalue = header.value | plus: 0 %}
{% assign statname = 'stats_' | append: header.name %}
{{ hvalue | round }} {% t statname %}
</p>
<p class="sub">
{% if site.jekyll_ga.compare_period %}
(
{% t 'stats_last' %} {{ site.data.period }} {% t 'stats_days' %}:
{% if header.value_perc != "∞" %}
{% assign perc = header.value_perc | plus: 0 %}

{% if header.name != "bounceRate" %}
{% if perc > 0 %}
<i class="fas fa-arrow-up color-green"></i>
{% elsif perc == 0 %}
<i class="fas fa-equals"></i>
{% elsif perc < 0 %}
<i class="fas fa-arrow-down color-red"></i>
{% endif %}
{% else %}
{% if diff < 0 %}
<i class="fas fa-arrow-up color-green"></i>
{% elsif diff == 0 %}
<i class="fas fa-equals"></i>
{% elsif diff > 0 %}
<i class="fas fa-arrow-down color-red"></i>
{% endif %}
{% endif %}

{{ perc | round }} % |

{% assign diff = header.diff_value %}
{% if diff > 0 %}+{% endif %}
{{ diff | round }}{% if header.name == "bounceRate" %}%{% endif %} {% t 'stats_last_period' %}
{% else %}
∞ %
{% endif %}
)
{% endif %}
</p>

{% endfor %}
</box>
</div>
```

**Note:** Keep in mind that this snippets make use of [jekyll-language-plugin](https://github.com/uta-org/jekyll-language-plugin), by this reason you'll need to replace all the `{% t ... %}` must be replaced by its equivalents:


```yaml
#########
# Stats #
#########

stats-caption: 'Statistics'
stats_pageviews: "views"
stats_bounceRate: " % bounce rate"
stats_sessions: "sessions"
stats_users: "visitors"
stats_newUsers: "new visitors"
stats_last: 'last'
stats_days: 'days'
stats_last_period: 'than last period'
```

This displays a box with the different metrics selected in your `metrics` configuration parameter:

![](https://i.gyazo.com/3105ff73fc023c5cf3506b9adcd63577.png)

I use this for any post:

```html
{% if page.stats.pageviews != blank %}
{% assign hvalue = header.value | plus: 0 %}
{{ hvalue | round }} views

{% if site.jekyll_ga.compare_period %}
(
last {{ site.data.period }} days:
{% if page.stats.pageviews_perc != "∞" %}
{% assign perc = page.stats.pageviews_perc | plus: 0 %}

{% if perc > 0 %}
<i class="fas fa-arrow-up color-green"></i>
{% elsif perc == 0 %}
<i class="fas fa-equals"></i>
{% elsif perc < 0 %}
<i class="fas fa-arrow-down color-red"></i>
{% endif %}

{{ perc | round }} % |

{% assign diff = page.stats.diff_pageviews %}
{% if diff > 0 %}+{% endif %}
{{ diff | round }} than last period
{% else %}
∞ %
{% endif %}
)
{% endif %}
.
{% endif %}
```

It only displays `xx visits (percentage % | difference between two ranges)`.

## Issues

Having issues? Just report in [the issue section](/issues). **Thanks for the feedback!**

## Contribute

Fork this repository, make your changes and then issue a pull request. If you find bugs or have new ideas that you do not want to implement yourself, file a bug report.

## Donate

Become a patron, by simply clicking on this button (**very appreciated!**):

[![](https://c5.patreon.com/external/logo/become_a_patron_button.png)](https://www.patreon.com/z3nth10n)

... Or if you prefer a one-time donation:

[![](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://paypal.me/z3nth10n)

## Copyright

Copyright (c) 2019 z3nth10n (United Teamwork Association).

License: GNU General Public License v3.0
27 changes: 27 additions & 0 deletions jekyll-ga-v2.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# coding: utf-8
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require "jekyll-ga-v2/version"

Gem::Specification.new do |spec|
spec.name = "jekyll-ga-v2"
spec.summary = "Jekyll Google Analytics integration"
spec.description = "Google Analytics support in Jekyll blog to easily show the statistics on your website"
spec.version = Jekyll::GoogleAnalyticsV2::VERSION
spec.authors = ["z3nth10n"]
spec.email = ["[email protected]"]
spec.homepage = "https://github.com/uta-org/jekyll-ga-v2"
spec.licenses = ["MIT"]

spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(test|spec|features|assets|versions)/!) }
spec.require_paths = ["lib"]

spec.add_dependency "jekyll", "~> 4.0"
spec.add_dependency 'googleauth', '~> 0.10.0'
spec.add_dependency 'google-api-client', '~> 0.34.1'
spec.add_dependency 'chronic', '~> 0.10.2'

spec.add_development_dependency "rake", "~> 11.0"
spec.add_development_dependency "rspec", "~> 3.5"
end
81 changes: 0 additions & 81 deletions jekyll-ga.rb

This file was deleted.

Loading