Ajax requests - page tracking

Hello,

I have been trying to get Matomo working on my word press site that uses an ajax theme.

The developer of the theme helped me to get gtag working one page loads using this js snippet:

jQuery(document).on('pjax:complete', function() {
  gtag('send', 'page_view');
});

This works fine. So I have tried to include the following after reading the Matamo guides:

jQuery(document).on('pjax:complete', function() {
  _paq.push(['setDocumentTitle', 'My New Title']);
  _paq.push(['trackPageView']);
});

But page views, other than the initial visit are still not being tracked, so I then tried using Matamo tag manager to create a custom tag with custom triggers. One is a history change trigger and the other a page view trigger event.

Now using this code:

jQuery(document).on('pjax:complete', function() {
window._mtm.push({'event': 'mtm.PageView'});
});

But again this still isn’t working, my js knowledge isn’t great so hoping someone can help me as I would like to make use of Matamo.

Thanks :slight_smile:

Maybe the tracking bootstrap code is missing… or is misconfigured
Do you see any error in your JavaScript console?

Hello,

No errors at all currently.

This is my website: https://ukradio.live

In case this helps at all, currently I have this code:

jQuery(document).on('pjax:complete', function() {
  gtag('send', 'page_view');
 
window._mtm.push({'event': 'mtm.PageView'});
	var currentUrl = location.href;
window.addEventListener('hashchange', function() {
    _paq.push(['setReferrerUrl', currentUrl]);
     currentUrl = '/' + window.location.hash.substr(1);
    _paq.push(['setCustomUrl', currentUrl]);
    _paq.push(['setDocumentTitle', 'My New Title']);

    // 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']);
});

I am hoping to use the tag manager, as it look like using the default setup I would need to set a name for every single page I wanted to track a page view for in this code

But apparently tag manager should only require this:

window._mtm.push({'event': 'mtm.PageView'});

Which I assume will be labelled with the page title/name when it works?

Try to add:

console.log('hashchange');

somewhere in your code, and check this is logged in the JavaScript console, just to be sure the code is well called at hashchange…

Then check on the network console if some page view requests are sent to the Matomo server.

For the other point (MTM), the idea is to not code each event / tracking. The Manager will do if for you. You just have to add the MTM bootstrap code (or maybe your CMS will do it for you with some little configuration), then you manage most of tracking through the MTM interface:

Thanks.

It is very strange, the page views are actually being logged. But they all show that every page view is the root of the website rather than the page actually being viewed.

This is while using tag manager.

I have posted this question, but updated on the wordpress support forum. As I am using the Matomo wp plugin.

Hoping I get some help :pray:

How did you configure the Page View Tag?