Skip to content

Commit

Permalink
Merge pull request #96 from uselagoon/matomo
Browse files Browse the repository at this point in the history
Added support for injecting Matomo snippets via ngx_http_sub_module.
  • Loading branch information
Toby Bellwood authored Feb 19, 2021
2 parents 6464470 + 54fc73b commit 1d0e0d3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions images/nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ RUN mkdir -p /app \
&& fix-permissions /var/run/

COPY docker-entrypoint /lagoon/entrypoints/70-nginx-entrypoint
COPY matomo/80-nginx-matomo-config /lagoon/entrypoints/

WORKDIR /app

Expand Down
49 changes: 49 additions & 0 deletions images/nginx/matomo/80-nginx-matomo-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/sh

# Inject Matomo tracking snippets via nginx configuration.

# Single site configuration.
if [ "$MATOMO_URL" ]; then

# Add Matomo tracking code to end of the HEAD section.
cat <<EOF >> /etc/nginx/conf.d/matomo.conf
sub_filter '</head>'
"<!-- Matomo -->
<script type='text/javascript'>
var _paq = window._paq = window._paq || [];
/* tracker methods like 'setCustomDimension' should be called before 'trackPageView' */
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u='${MATOMO_URL}';
_paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', '${MATOMO_SITE_ID:-1}']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})();
</script>
<!-- End Matomo Code -->
</head>";
sub_filter_once on;
EOF

fi

# Tag manager configuration.
if [ "$MATOMO_TAG_MANAGER_URL" ]; then

cat <<EOF >> /etc/nginx/conf.d/matomo.conf
sub_filter '<head>'
"<head>
<!-- Matomo Tag Manager -->
<script type='text/javascript'>
var _mtm = window._mtm = window._mtm || [];
_mtm.push({'mtm.startTime': (new Date().getTime()), 'event': 'mtm.Start'});
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.src='${MATOMO_TAG_MANAGER_URL}'; s.parentNode.insertBefore(g,s);
</script>
<!-- End Matomo Tag Manager -->";
sub_filter_once on;
EOF

fi
18 changes: 18 additions & 0 deletions images/nginx/matomo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Matomo

Allows automatic injection of Matomo tracking snippets via environment varibales using ngx_http_sub_module.
Snippets are only included when one following configurations have been enabled:

## Single site

`MATOMO_URL` (required) - URL to the Matomo instance.
`MATOMO_SITE_ID` (required) - Matomo Site ID.

Both environment variable are required to be set.
Matomo tracking code is injected before the end of the HEAD section of all html reponses.

## Tag manager

`MATOMO_TAG_MANAGER_URL` (required) - URL of the container JS file eg. https://[matomo-url]/js/container_xxxxxxxx.js

Matomo tracking code is injected at the start of the HEAD section of all html reponses.

0 comments on commit 1d0e0d3

Please sign in to comment.