mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-30 07:18:09 +02:00
Implemented Global CSS draft
This commit is contained in:
35
roles/nginx-global-matomo/README.md
Normal file
35
roles/nginx-global-matomo/README.md
Normal file
@@ -0,0 +1,35 @@
|
||||
# Nginx Matomo Tracking Role
|
||||
|
||||
This Ansible role automates the integration of Matomo tracking code into Nginx-served websites. It simplifies the process of adding the Matomo analytics tracking script and image tracker to all your web pages served through Nginx.
|
||||
|
||||
## Features
|
||||
- Automated insertion of Matomo tracking script into the `</head>` tag of HTML pages.
|
||||
- Integration of a noscript image tracker before the `</body>` tag for tracking users with JavaScript disabled.
|
||||
- Configuration to apply changes on every request, ensuring that dynamic content and single-page applications are also tracked.
|
||||
|
||||
## Requirements
|
||||
- Nginx installed on the target server.
|
||||
- Matomo analytics platform set up and accessible.
|
||||
|
||||
## Role Variables
|
||||
- `matomo_domain`: The domain of your Matomo installation.
|
||||
- `domain`: The domain of the website you wish to track.
|
||||
- `matomo_auth_token`: Matomo auth token
|
||||
|
||||
## Dependencies
|
||||
- None. This role is designed to be included in Nginx server block configurations.
|
||||
|
||||
## Example Usage
|
||||
To enable Matomo tracking on your Nginx website, include the role in your playbook and set the required variables.
|
||||
|
||||
```yaml
|
||||
- hosts: webserver
|
||||
roles:
|
||||
- { role: nginx-global-matomo, matomo_domain: 'matomo.example.com', base_domain: 'example.com', matomo_site_id: '1' }
|
||||
```
|
||||
|
||||
## Customization
|
||||
You can customize the tracking script and the noscript image tracker by editing the `matomo-tracking.js.j2` and `matomo.subfilter.conf.j2` templates.
|
||||
|
||||
## Author Information
|
||||
This role was created in 2023 by [Kevin Veen Birkenbach](https://www.veen.world/), providing a seamless way to add Matomo analytics to any website served via Nginx.
|
38
roles/nginx-global-matomo/tasks/main.yml
Normal file
38
roles/nginx-global-matomo/tasks/main.yml
Normal file
@@ -0,0 +1,38 @@
|
||||
- name: Check if site already exists in Matomo
|
||||
uri:
|
||||
url: "https://{{matomo_domain}}/index.php?module=API&method=SitesManager.getSitesIdFromSiteUrl&url=https://{{base_domain}}&format=json&token_auth={{matomo_auth_token}}"
|
||||
method: GET
|
||||
return_content: yes
|
||||
status_code: 200
|
||||
validate_certs: yes
|
||||
register: site_check
|
||||
|
||||
- name: Set fact for site ID if site already exists
|
||||
set_fact:
|
||||
matomo_site_id: "{{ site_check.json[0].idsite }}"
|
||||
when: "(site_check.json | length) > 0"
|
||||
|
||||
- name: Add site to Matomo and get ID if not exists
|
||||
uri:
|
||||
url: "https://{{ matomo_domain }}/index.php"
|
||||
method: POST
|
||||
body: "module=API&method=SitesManager.addSite&siteName={{ base_domain }}&urls=https://{{ base_domain }}&token_auth={{ matomo_auth_token }}&format=json"
|
||||
body_format: form-urlencoded
|
||||
status_code: 200
|
||||
return_content: yes
|
||||
validate_certs: yes
|
||||
register: add_site
|
||||
when: "matomo_site_id is not defined"
|
||||
|
||||
- name: Set fact for site ID if site was added
|
||||
set_fact:
|
||||
matomo_site_id: "{{ add_site.json.value }}"
|
||||
when: "matomo_site_id is not defined"
|
||||
|
||||
- name: Set the Matomo tracking code from a template file
|
||||
set_fact:
|
||||
matomo_tracking_code: "{{ lookup('template', 'matomo-tracking.js.j2') }}"
|
||||
|
||||
- name: Set the tracking code as a one-liner
|
||||
set_fact:
|
||||
matomo_tracking_code_one_liner: "{{ matomo_tracking_code | regex_replace('\\n', '') | regex_replace('\\s+', ' ') }}"
|
@@ -0,0 +1,6 @@
|
||||
# Deactivate CSP header
|
||||
add_header Content-Security-Policy: "";
|
||||
|
||||
# sub filters to integrate matomo tracking code in nginx websites
|
||||
sub_filter '</head>' '<script>{{matomo_tracking_code_one_liner}}</script></head>';
|
||||
sub_filter '</body>' '<noscript><p><img src="//matomo.{{primary_domain}}/matomo.php?idsite={{matomo_site_id}}&rec=1" style="border:0;" alt="" /></p></noscript></body>';
|
15
roles/nginx-global-matomo/templates/matomo-tracking.js.j2
Normal file
15
roles/nginx-global-matomo/templates/matomo-tracking.js.j2
Normal file
@@ -0,0 +1,15 @@
|
||||
var _paq = window._paq = window._paq || [];
|
||||
_paq.push(["setDocumentTitle", document.domain + "/" + document.title]);
|
||||
_paq.push(["setCookieDomain", "*.{{base_domain}}"]);
|
||||
_paq.push(["setDomains", ["*.{{base_domain}}"]]);
|
||||
_paq.push(["enableCrossDomainLinking"]);
|
||||
_paq.push(["trackPageView"]);
|
||||
_paq.push(["trackAllContentImpressions"]);
|
||||
_paq.push(["enableLinkTracking"]);
|
||||
(function() {
|
||||
var u="//{{matomo_domain}}/";
|
||||
_paq.push(["setTrackerUrl", u+"matomo.php"]);
|
||||
_paq.push(["setSiteId", "{{matomo_site_id}}"]);
|
||||
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0];
|
||||
g.async=true; g.src=u+"matomo.js"; s.parentNode.insertBefore(g,s);
|
||||
})();
|
2
roles/nginx-global-matomo/vars/main.yml
Normal file
2
roles/nginx-global-matomo/vars/main.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
matomo_domain: "matomo.{{primary_domain}}"
|
||||
base_domain: "{{ domain | regex_replace('^(?:.*\\.)?(.+\\..+)$', '\\1') }}"
|
Reference in New Issue
Block a user