Skip to content

Commit 2c7d090

Browse files
authored
Merge pull request #1 from JiscSD/feature/create-plugin
Matomo Plugin Creation
2 parents d82f9ef + 9ac51d8 commit 2c7d090

File tree

17 files changed

+417
-2
lines changed

17 files changed

+417
-2
lines changed

.coveragerc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[report]
2+
omit =
3+
*/site-packages/*
4+
*/python?.?/*
5+
ckan/*

.github/workflows/test.yml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: Tests
2+
on: [push, pull_request]
3+
jobs:
4+
test:
5+
runs-on: ubuntu-latest
6+
container:
7+
# The CKAN version tag of the Solr and Postgres containers should match
8+
# the one of the container the tests run on.
9+
# You can switch this base image with a custom image tailored to your project
10+
image: ckan/ckan-dev:2.11
11+
options: --user root
12+
services:
13+
solr:
14+
image: ckan/ckan-solr:2.11-solr9
15+
postgres:
16+
image: ckan/ckan-postgres-dev:2.11
17+
env:
18+
POSTGRES_USER: postgres
19+
POSTGRES_PASSWORD: postgres
20+
POSTGRES_DB: postgres
21+
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
22+
redis:
23+
image: redis:3
24+
25+
env:
26+
CKAN_SQLALCHEMY_URL: postgresql://ckan_default:pass@postgres/ckan_test
27+
CKAN_DATASTORE_WRITE_URL: postgresql://datastore_write:pass@postgres/datastore_test
28+
CKAN_DATASTORE_READ_URL: postgresql://datastore_read:pass@postgres/datastore_test
29+
CKAN_SOLR_URL: http://solr:8983/solr/ckan
30+
CKAN_REDIS_URL: redis://redis:6379/1
31+
32+
steps:
33+
- uses: actions/checkout@v3
34+
- name: Install requirements
35+
# Install any extra requirements your extension has here (dev requirements, other extensions etc.)
36+
run: |
37+
pip install -r dev-requirements.txt
38+
pip install -e .
39+
- name: Setup extension
40+
# Extra initialization steps
41+
run: |
42+
# Replace default path to CKAN core config file with the one on the container
43+
sed -i -e 's/use = config:.*/use = config:\/srv\/app\/src\/ckan\/test-core.ini/' test.ini
44+
45+
ckan -c test.ini db init
46+
- name: Run tests
47+
run: pytest --ckan-ini=test.ini --cov=ckanext.matomo_js --disable-warnings ckanext/matomo_js

.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
### VisualStudioCode template
2+
.vscode/
3+
4+
# Local History for Visual Studio Code
5+
.history/
6+
7+
### JetBrains template
8+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
9+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
10+
11+
.idea/
12+
13+
# CMake
14+
cmake-build-*/
15+
16+
### VirtualEnv template
17+
# Virtualenv
18+
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
19+
.Python
20+
[Bb]in
21+
[Ii]nclude
22+
[Ll]ib
23+
[Ll]ib64
24+
[Ll]ocal
25+
[Ss]cripts
26+
pyvenv.cfg
27+
.venv
28+
pip-selfcheck.json

MANIFEST.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include README.md
2+
include LICENSE
3+
include requirements.txt
4+
recursive-include ckanext/matomo_js *.html *.json *.js *.less *.css *.mo *.yml
5+
recursive-include ckanext/matomo_js/migration *.ini *.py *.mako

README.md

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,137 @@
1-
# ckanext-matomo-js
2-
CKAN Matomo Plugin using JS File
1+
[![Tests](https://github.com/JiscSD/ckanext-matomo-js/workflows/Tests/badge.svg?branch=main)](https://github.com/JiscSD/ckanext-matomo-js/actions)
2+
3+
# CKAN Matomo Plugin using JS File
4+
5+
This is the matomo tag manager plugin for CKAN.
6+
It includes the necessary JavaScript file to apply matomo tracking via the tag manager.
7+
8+
## Features included
9+
1. Matomo tracking with the flexibility from the tag manager.
10+
11+
12+
13+
## Requirements
14+
15+
This plugin is developed and tested on CKAN version 2.11
16+
17+
Compatibility with core CKAN versions:
18+
19+
| CKAN version | Compatible? |
20+
|-----------------|-------------|
21+
| 2.11 | yes |
22+
23+
24+
## Installation
25+
26+
To install ckanext-matomo_js:
27+
28+
1. Activate your CKAN virtual environment, for example:
29+
```
30+
. /usr/lib/ckan/default/bin/activate
31+
```
32+
2. Clone the source and install it on the virtualenv
33+
```
34+
cd /usr/lib/ckan/default/src
35+
python3 -m pip install -e git+https://github.com/JiscSD/ckanext-matomo-js.git#egg=ckanext-matomo-js
36+
37+
then reset the permissions for the new ckanext-matomo-js directory to 'ckan'
38+
```
39+
3. Add `matomo_js` to the `ckan.plugins` setting in your CKAN
40+
config file (by default the config file is located at
41+
`/etc/ckan/default/ckan.ini`).
42+
43+
4. Restart CKAN. For example if you've deployed CKAN with Apache on Ubuntu:
44+
```
45+
sudo service apache2 reload
46+
```
47+
or if the server is on AWS, called
48+
```
49+
sudo systemctl restart nginx
50+
```
51+
52+
## Updating to a new version
53+
54+
To update ckanext-matomo_js to a new version:
55+
56+
1. Activate your CKAN virtual environment, for example:
57+
```
58+
. /usr/lib/ckan/default/bin/activate
59+
```
60+
2. pull the latest version then install it on the virtualenv
61+
```
62+
cd /usr/lib/ckan/default/src/ckanext-matomo-js
63+
git pull
64+
pip install -e .
65+
66+
then reset the permissions for the ckanext-matomo-js directory to 'ckan'
67+
```
68+
69+
3. Restart CKAN. For example if you've deployed CKAN with Apache on Ubuntu:
70+
```
71+
sudo service apache2 reload
72+
```
73+
or if the server is on AWS, called
74+
```
75+
sudo reboot now
76+
```
77+
78+
## Config settings
79+
80+
In order for the plugin to work, you need to set the url to the tag manager container in the javascript file.
81+
Once the plugin has been installed, go to the matomo.js file and replace the url tag ```.src='<src url>'``` with the url of the tag manager container.
82+
83+
84+
## Developer installation
85+
86+
To install ckanext-matomo_js for development, activate your CKAN virtualenv and
87+
do:
88+
89+
python3 -m pip install -e git+https://github.com/JiscSD/ckanext-matomo-js.git#egg=ckanext-matomo-js
90+
cd ckanext-matomo-js
91+
python3 setup.py develop
92+
python3 -m pip install -r dev-requirements.txt
93+
94+
95+
## Tests
96+
97+
To run the tests, do:
98+
99+
pytest --ckan-ini=test.ini
100+
101+
102+
## Releasing a new version of ckanext-matomo_js
103+
104+
If ckanext-matomo_js should be available on PyPI you can follow these steps to publish a new version:
105+
106+
1. Update the version number in the `setup.py` file. See [PEP 440](http://legacy.python.org/dev/peps/pep-0440/#public-version-identifiers) for how to choose version numbers.
107+
108+
2. Make sure you have the latest version of necessary packages:
109+
110+
pip install --upgrade setuptools wheel twine
111+
112+
3. Create a source and binary distributions of the new version:
113+
114+
python setup.py sdist bdist_wheel && twine check dist/*
115+
116+
Fix any errors you get.
117+
118+
4. Upload the source distribution to PyPI:
119+
120+
twine upload dist/*
121+
122+
5. Commit any outstanding changes:
123+
124+
git commit -a
125+
git push
126+
127+
6. Tag the new release of the project on GitHub with the version number from
128+
the `setup.py` file. For example if the version number in `setup.py` is
129+
0.0.1 then do:
130+
131+
git tag 0.0.1
132+
git push --tags
133+
134+
## License
135+
136+
[Unlicense](https://unlicense.org/)
137+

ckanext/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# encoding: utf-8
2+
3+
# this is a namespace package
4+
try:
5+
import pkg_resources
6+
pkg_resources.declare_namespace(__name__)
7+
except ImportError:
8+
import pkgutil
9+
__path__ = pkgutil.extend_path(__path__, __name__)

ckanext/matomo_js/__init__.py

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
let _mtm = window._mtm = window._mtm || [];
2+
_mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
3+
(function() {
4+
let d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
5+
g.async=true; g.src='<src url>'; s.parentNode.insertBefore(g,s);
6+
})();
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
matomo:
2+
contents:
3+
- js/matomo.js
4+
filters: rjsmin
5+
output: matomo_webassets/%(version)s-matomo.js

ckanext/matomo_js/plugin.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# encoding: utf-8
2+
3+
"""plugin.py
4+
5+
"""
6+
import ckan.plugins as plugins
7+
import ckan.plugins.toolkit as toolkit
8+
9+
10+
class UKDSMatomoJSPlugin(plugins.SingletonPlugin):
11+
plugins.implements(plugins.IConfigurer)
12+
13+
def update_config(self, config):
14+
toolkit.add_template_directory(config, 'templates')
15+
toolkit.add_resource('assets', 'matomo_webassets')

0 commit comments

Comments
 (0)