Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Matomo tracking #55

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions layouts/partials/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
{{ if .RSSLink }}
<link href="{{ .RSSLink }}" rel="alternate" type="application/rss+xml">
{{ end }}
{{ if .Site.Params.MatomoUrl }}{{ partial "matomo.html" . }}{{ end }}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move this at the top of body just after the google_analytics in the header.html ? Also lets do the if check inside the template.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do 👍

{{ partial "extra-in-head.html" . }}
</head>
<body>
Expand Down
21 changes: 21 additions & 0 deletions layouts/partials/matomo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{ $siteId := cond (isset .Site.Params "matomositeid") .Site.Params.MatomoSiteId "1" }}
{{ $clientSideDNT := cond (isset .Site.Params "matomoclientsidednt") .Site.Params.MatomoClientSideDNT true }}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets do the DNT check just like in the google analytics template https://raw.githubusercontent.com/gohugoio/hugo/master/tpl/tplimpl/embedded/templates/google_analytics.html and introduce a param like matomoRespectDoNotTrack. If that option is true it should not even include the tracking snippet, just like in the google analytics. Also be sure to check for user DNT setting in browser var dnt = (navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack);.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I see where you're getting at.

However, this specific flag serves to include a JS snippet that prevents the request from being sent at all if the user's DNT setting is turned on.
So the check you're referring to is in the javascript that is included. https://github.com/matomo-org/matomo/blob/6f5b85b6f7d2195362ae03480120f4cef5ec4dc1/js/piwik.js#L6564

Knowing this, would you still add it yourself? :)

<script type="text/javascript">
var _paq = _paq || [];
/* tracker methods like "setCustomDimension" should be called before "trackPageView" */
{{ if .Site.Params.MatomoDomainPrefixed }}_paq.push(["setDocumentTitle", document.domain + "/" + document.title]); {{ end }}
{{ if $clientSideDNT}} _paq.push(["setDoNotTrack", true]); {{ end }}
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function() {
var u="//{{ .Site.Params.MatomoUrl }}/";
_paq.push(['setTrackerUrl', u+'piwik.php']);
_paq.push(['setSiteId', '{{ $siteId }}']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.defer=true; g.src=u+'piwik.js'; s.parentNode.insertBefore(g,s);
})();
</script>
{{ if .Site.Params.MatomoEnableFallback }}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this fallback ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a Matomo specific option, not really a fallback for the option on its own, but if the user has JS disabled this 'fallback' provides a way of tracking it using an img element.

So, a better suggestion for the name?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do people still disable javascript ? I mean most of today pages are JS heavy :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally know a few ¯\_(ツ)_/¯

<noscript><p><img src="//{{.Site.Params.MatomoUrl}}/piwik.php?idsite={{ $siteId }}&amp;rec=1" style="border:0;" alt="" /></p></noscript>
{{ end }}
<!-- End Matomo Code -->