Track IDRViewer page views and events with PostHog
<!-- PostHog -->
<script>
!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.crossOrigin="anonymous",p.async=!0,p.src=s.api_host.replace(".i.posthog.com","-assets.i.posthog.com")+"/static/array.js",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],Object.defineProperty(u,"toString",{configurable:!0,enumerable:!0,writable:!0,value:function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e}}),Object.defineProperty(u.people,"toString",{configurable:!0,enumerable:!0,writable:!0,value:function(){return u.toString(1)+".people (stub)"}}),o="init capture register register_once register_for_session unregister unregister_for_session getFeatureFlag getFeatureFlagResult isFeatureEnabled reloadFeatureFlags updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures on onFeatureFlags onSessionId getSurveys getActiveMatchingSurveys renderSurvey canRenderSurvey getNextSurveyStep identify setPersonProperties group resetGroups setPersonPropertiesForFlags resetPersonPropertiesForFlags setGroupPropertiesForFlags resetGroupPropertiesForFlags reset get_distinct_id getGroups get_session_id get_session_replay_url alias set_config startSessionRecording stopSessionRecording sessionRecordingStarted captureException loadToolbar get_property getSessionProperty createPersonProfile opt_in_capturing opt_out_capturing has_opted_in_capturing has_opted_out_capturing clear_opt_in_out_capturing debug".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
posthog.init('phc_XXXXXXXXXX', {
api_host: 'https://us.i.posthog.com',
defaults: '2026-08-30'
});
</script>
<!-- End PostHog -->
<!-- IDRViewer PostHog events -->
<script>
(function () {
function trackPageView(page, pagecount) {
posthog.capture('idrviewer_page_view', {
'page_number': page,
'page_count': pagecount,
'document_title': document.title
});
}
IDRViewer.on('ready', function (data) { // The page the document opens on
trackPageView(data.page, data.pagecount);
});
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, data.pagecount);
}
}, 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
posthog.capture('idrviewer_search', {
'search_term': data.searchTerm,
'result_count': data.results.length
});
}, 1500);
});
})();
</script>
<!-- IDRViewer PostHog 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, in the first script, replace phc_XXXXXXXXXX with your PostHog project token and set api_host to where your project is hosted:
| Project hosted on | api_host |
|---|---|
| PostHog Cloud, US | https://us.i.posthog.com |
| PostHog Cloud, EU | https://eu.i.posthog.com |
| Your own server (self-hosted) | Your instance’s address, such as https://posthog.example.com |
The web snippet in your PostHog project settings has both values filled in, so you can paste it in place of the first script.
Things to know:
- PostHog already on the page: add only the second script.
posthogis global, so the two scripts don’t need to be together. - 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 before PostHog’s library has loaded is fine, because the snippet queues each call. - 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 PostHog:
| Event | Sent when | Properties |
|---|---|---|
idrviewer_page_view | The document opens, and each time the reader settles on a page | page_number, page_count, document_title |
idrviewer_search | The reader searches (Complete UI only) | search_term, result_count |
PostHog also adds the page URL, browser and session to each event.
- No setup needed: the properties can be used in insights straight away. A Trends insight on
idrviewer_page_viewbroken down bypage_numbershows how far readers get, and one onidrviewer_searchbroken down bysearch_termshows what they look for. - Searches with no results have a
result_countof0, so you can see what readers looked for and didn’t find. - 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 look for the events in PostHog’s live events.
- Testing from
localhostor127.0.0.1marks you as an internal or test user (withdefaultsof2026-01-30or later, as in the snippet), so your events won’t appear in insights that filter those users out.
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 PostHog records already:
- A
$pageviewwhen the document opens, and a$pageleavewhen the reader leaves. - Nothing when the reader changes page. With
defaultsof2025-05-24or later, PostHog captures a page view whenever the URL path changes through the History API. The viewer only changes the query string (?page=3), so nothing is counted twice.
Recording each viewer page as a page view:
This makes each viewer page a $pageview in PostHog’s web analytics and paths. Make two changes to the snippet.
Add capture_pageview: false to posthog.init:
posthog.init('phc_XXXXXXXXXX', {
api_host: 'https://us.i.posthog.com',
defaults: '2026-08-30',
capture_pageview: false
});
Replace trackPageView with:
function trackPageView(page, pagecount) {
posthog.capture('$pageview', {
'$current_url': location.origin + location.pathname + '?page=' + page,
'page_number': page,
'page_count': pagecount
});
}
- The URL is built from the page number because the viewer may not have updated the address bar yet.
- Turning off
capture_pageviewalso stops$pageleave. - 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 posthog.capture(...) and whatever properties you need. Only ready has to be registered before IDRViewer.setup() runs.