Track IDRViewer page views and events with Matomo
<!-- Matomo -->
<script>
var _paq = window._paq = window._paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function () {
var u = 'https://matomo.example.com/';
_paq.push(['setTrackerUrl', u + 'matomo.php']);
_paq.push(['setSiteId', '1']);
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);
})();
</script>
<!-- End Matomo Code -->
<!-- IDRViewer Matomo events -->
<script>
(function () {
function trackPageView(page) {
_paq.push(['trackEvent', 'IDRViewer', 'Page view', document.title, page]);
}
IDRViewer.on('ready', function (data) { // The page the document opens on
trackPageView(data.page);
});
var THRESHOLD = 1000; // Time (in millis) a page must stay on screen to count as viewed
var lastPage;
IDRViewer.on('pagechange', function (data) { // Every page navigated to afterwards
lastPage = data.page;
setTimeout(function () { // Wait until the page stops changing before sending the event
if (lastPage === data.page) {
trackPageView(data.page);
}
}, THRESHOLD);
});
var searchTimeout;
IDRViewer.on('search', function (data) {
clearTimeout(searchTimeout);
if (!data.searchTerm) { // The search box was cleared
return;
}
searchTimeout = setTimeout(function () { // Wait until the user stops typing
_paq.push(['trackSiteSearch', data.searchTerm, document.title, data.results.length]);
}, 1500);
});
})();
</script>
<!-- IDRViewer Matomo events end -->
Applies to output generated in the idrviewer view mode (the default). For the content and singlefile modes, see View Modes.
Add it before IDRViewer.setup():
Paste the snippet at the end of the generated index.html, immediately before the script that calls IDRViewer.setup(). In the Complete, Clean and Simple viewer UIs that call is a line of its own:
<script src="config.js" type="text/javascript"></script>
<script type="text/javascript">IDRViewer.setup();</script>
In SlideShow it sits inside a larger script alongside a mobile check, so paste the snippet above that whole block.
Then fill in the first script with the values from Administration > Websites > Tracking Code in Matomo:
- Replace
https://matomo.example.com/with the address of your Matomo server. - Replace
'1'with the ID of the website you are tracking.
Things to know:
- Matomo already on the page, or Matomo Cloud: use your own tracking code in place of the first script and keep the second. Matomo Cloud loads
matomo.jsfrom its own CDN, so its tracking code differs. - Position matters:
IDRViewer.setup()firesreadyas it runs and the viewer doesn’t replay events, so a listener added after that line misses the opening page. Firing beforematomo.jshas loaded is fine, because_paqqueues each command. - Reconverting: each conversion writes
index.htmlagain, so re-add the snippet afterwards, or set the viewer UI tocustomand supply your own index file with the snippet already in it.
What this records in Matomo:
| Reader action | Recorded as | Report |
|---|---|---|
| Opens the document, or settles on a page | An event with category IDRViewer, action Page view, the document title as the name and the page number as the value | Behaviour > Events |
| Searches (Complete UI only) | A site search with the term as the keyword, the document title as the category, and the number of results | Behaviour > Site Search |
- Event Names report: lists each document with its pages viewed. The maximum event value is the furthest page any visitor reached. Ignore the total event value, which adds page numbers together.
- Site Search must be enabled for the website under Administration > Websites > Manage. If it is off, Matomo records each search as a page view instead.
- Searches with no results are listed separately, showing what readers looked for and didn’t find. Site searches don’t count as page views.
- Search only exists in the Complete viewer UI. On Clean, Simple and SlideShow the listener registers without error but never reports anything.
- To check it works, open a converted document, move between a few pages and search, then find the visit in Visitors > Visits Log.
When events are sent:
- Pages are counted once they have been on screen for one second (
THRESHOLD), so flicking through ten pages to reach page eleven records one view, not eleven. - Searches are sent 1.5 seconds after the last keystroke. The Complete UI searches on every keypress, so this stops
i,inandinvbeing reported as separate terms. - Clearing the search box sends nothing.
Page views:
What Matomo records already:
- One page view when the document opens, from the
trackPageViewline in the tracking code. - Nothing when the reader changes page. The viewer updates the address bar (
?page=3) through the History API, but unlike GA4, Matomo doesn’t count those updates, so nothing is counted twice. - Several URLs per document: the opening page view usually includes
?page=, so one document can appear asindex.html?page=1,index.html?page=4and so on in Behaviour > Pages. To merge them, addpageto the website’s Excluded Parameters under Administration > Websites > Manage, unless you record each viewer page as a page view (below).
Recording each viewer page as a page view:
This puts each page of the document in Matomo’s page reports, such as Pages, Entry pages, Exit pages and Transitions. Make two changes to the snippet.
Remove this line from the tracking code, or the opening page is counted twice:
_paq.push(['trackPageView']);
Replace trackPageView with:
function trackPageView(page) {
_paq.push(['setCustomUrl', location.origin + location.pathname + '?page=' + page]);
_paq.push(['setDocumentTitle', document.title + ' - page ' + page]);
_paq.push(['trackPageView']);
}
- The URL is built from the page number because the viewer may not have updated the address bar yet.
- Page views then count pages read rather than documents opened, so they no longer compare directly with the rest of your site.
Tracking zoom, layout and other viewer events:
Any event in the IDRViewer JavaScript API works the same way, such as zoomchange, layoutchange, rotationchange and searchresultselected. Attach a listener, read the values from data, and send them with _paq.push(['trackEvent', 'IDRViewer', ...]), for example with the action Zoom and data.zoomType as the name. Only ready has to be registered before IDRViewer.setup() runs.