-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #96 from uselagoon/matomo
Added support for injecting Matomo snippets via ngx_http_sub_module.
- Loading branch information
Showing
3 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |