Skip to content

Instantly share code, notes, and snippets.

@SerkanSipahi
Last active January 6, 2022 13:13
Show Gist options
  • Save SerkanSipahi/10845d194da758c5b2437a55b39f60f4 to your computer and use it in GitHub Desktop.
Save SerkanSipahi/10845d194da758c5b2437a55b39f60f4 to your computer and use it in GitHub Desktop.
matomo for agnostic spa apps
let currentUrl = location.href;
document.addEventListener('DOMContentLoaded', function(){
let previousUrl = '';
const observer = new MutationObserver(function(mutations) {
if (location.href === previousUrl) return;
_paq.push(['setReferrerUrl', currentUrl]);
currentUrl = '/' + location.pathname + location.search;
_paq.push(['setCustomUrl', currentUrl]);
_paq.push(['setDocumentTitle', 'Title: ' + location.pathname + location.search]);
// remove all previously assigned custom variables, requires Matomo (formerly Piwik) 3.0.2
_paq.push(['deleteCustomVariables', 'page']);
_paq.push(['trackPageView']);
// make Matomo aware of newly added content
var content = document.getElementById('content');
_paq.push(['MediaAnalytics::scanForMedia', content]);
_paq.push(['FormAnalytics::scanForForms', content]);
_paq.push(['trackContentImpressionsWithinNode', content]);
_paq.push(['enableLinkTracking']);
previousUrl = location.href;
});
const config = {subtree: true, childList: true};
observer.observe(document, config);
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment